<div class="page-header">
    <h3 class="page-title">
        <span class="page-title-icon bg-gradient-info text-white mr-2">
            <i class="fa fa-cubes"></i>
        </span>
        Product Return Request
    </h3>
</div>


<div class="row">
    <div class="col-md-12 grid-margin stretch-card p-0 m-0">

        <div class="card border10 shadow">
            <div class="card-body p-3">
                <h4 class="card-title">Product Return</h4>
                <div class="table-responsive">
                    <table class="table table-borderless table-striped table-hover shadow" id="datatable">
                        <thead>
                            <tr>
                                <th>Order ID</th>
                                <th>Image</th>
                                <th>Prouct Name</th>
                                <th>Return Reasion</th>
                                <th>Node</th>
                                <th>Return Request</th>
                                <th>Status</th>
                                <th>Customer</th>
                                <th>return</th>

                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($pdt_return)) : ?>

                                <?php foreach ($pdt_return as $pdt) : ?>
                                    <tr>
                                        <td>#<?= $pdt->invoice_id ?></td>
                                        <td><img src="https://myadmin.mali-mart.com/images/products/<?= $pdt->thumb ?>" width="100px" height="80px" /></td>
                                        <td><?= $pdt->name ?></td>
                                        <td><?= $pdt->reasion_type ?></td>
                                        <td><?= $pdt->additional_cmnt ?></td>
                                        <td>
                                            <?= nice_date($pdt->created_at, 'd-m-y h:i:a') ?>
                                        </td>
                                        <td>
                                            <span class="badge <?php if ($pdt->status == 2) {
                                                                    echo 'badge-danger';
                                                                } elseif ($pdt->status == 1) {
                                                                    echo 'badge-success';
                                                                } else {
                                                                    echo 'badge-info';
                                                                } ?>">

                                                <?php if ($pdt->status == 0) : ?>
                                                    Return Req
                                                <?php elseif ($pdt->status == 2) : ?>
                                                    Return
                                                <?php else : ?>
                                                    Accepted
                                                <?php endif; ?>
                                            </span>

                                        </td>
                                        <td>
                                            <button class="badge badge-info" style="cursor: pointer;" data-user_id="<?= $pdt->user_id ?>" data-marcent_id="<?= $pdt->marcent_id - 1000 ?>" onclick="contactUser(this)" data-toggle="modal" data-target="#customer">Customer</button>
                                        </td>
                                        <td>
                                            <?php if ($pdt->status == 0) : ?>
                                                <button style="cursor: pointer;" class="badge badge-success" onclick="returnAccepted(this)" data-id="<?= $pdt->id ?>">Return Approved <i class="" id="loading"></i></button>

                                            <?php endif ?>
                                        </td>

                                    </tr>
                                <?php endforeach; ?>
                            <?php endif ?>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>


<div class="col-12 modal fade" id="customer">
    <div class="modal-dialog">
        <div class="modal-content">

            <!-- Modal Header -->
            <div class="modal-header">
                <h4 class="modal-title"> <i class="mdi mdi-coin"></i><b>Customer Contact Information</b></h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- Modal body -->
            <div class="modal-body">
                <div id="contact_info">

                </div>
            </div>

            <!-- Modal footer -->
            <div class="modal-footer">

                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
            </div>

        </div>
    </div>
</div>

<br><br><br>


<script>
    function contactUser(e) {

        let user_id = e.getAttribute('data-user_id');

        let marcent_id = e.getAttribute('data-marcent_id');

        var formData = new FormData();
        formData.append('marcent_id', marcent_id);
        formData.append('user_id', user_id);

        $.ajax({
            type: "post",
            url: "https://work.mali-mart.com/OrderCtrl/marcentUserInfo",
            data: formData,
            contentType: false,
            processData: false,
            dataType: "json",
            success: function(response) {
                $("#customer").modal("show");


                let display = document.getElementById('contact_info');
                display.innerHTML = '';
                display.innerHTML = '<div>' +
                    '<div style="padding:5px">' +
                    'Customer Name : ' + response.user.name + '<br>' +
                    'Customer Mobile : 0' + response.user.mobile_num + '<br>' +
                    '</div>' +
                    '</div>'
            }
        })

    }

    function returnAccepted(e) {
        if (confirm('Are you sure accepted your return product')) {

            let id = e.getAttribute('data-id');
            let loading = document.getElementById('loading');
            loading.className = 'fa fa-refresh fa-spin';
            e.setAttribute("disabled", true);

            $.ajax({
                type: "get",
                url: "https://myadmin.mali-mart.com/api/marcent-return-accept?id=" + id,
                contentType: false,
                processData: false,
                dataType: "json",
                success: function(response) {
                    if (response === 1) {
                        alert('Product return accepted successfully !!!');
                        window.location.reload();

                    }
                }
            })


        }
    }
</script>