<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>
        Order History
    </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">Order Panding & Rejected</h4>
                <div class="table-responsive">
                    <table class="table table-borderless table-striped table-hover shadow" id="datatable">
                        <thead>
                            <tr>
                                <th>Order ID</th>
                                <th>Customer Mobile</th>
                                <th>Shipping Address</th>
                                <th>Total Item</th>
                                <th>Total Amount</th>
                                <th>Order Request</th>
                                <th>Status</th>
                                <th>Details</th>

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

                                <?php foreach ($pending as $order) : ?>
                                    <tr>
                                        <td>#<?= $order->invoice_id ?></td>
                                        <td><?= $order->customer_mob ?></td>
                                        <td><?= $order->adress ?></td>
                                        <td><?= $order->total_item ?>/=</td>
                                        <td>৳ <?= number_format($order->total_sales, 2) ?></td>
                                        <td>
                                            <?= nice_date($order->created_at, 'd-m-y h:i:a') ?>
                                        </td>
                                        <td>

                                            <span class="badge <?php if ($order->status == 2) {
                                                                    echo 'badge-danger';
                                                                } elseif ($order->status == 1) {
                                                                    echo 'badge-success';
                                                                } else {
                                                                    echo 'badge-info';
                                                                } ?>">

                                                <?php if ($order->status == 1) : ?>
                                                    Accepted
                                                <?php elseif ($order->status == 2) : ?>
                                                    Rejected
                                                <?php else : ?>
                                                    Pending
                                                <?php endif; ?>
                                            </span>

                                        </td>
                                        <td><a class="badge badge-info" href="<?= base_url('OrderCtrl/marchant_order_details/' . $order->order_id) ?>">Details</a>
                                        </td>

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

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<br><br><br>
<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">Admin Order Approved</h4>
                <div class="table-responsive">
                    <table class="table table-borderless table-striped table-hover shadow" id="datatable">
                        <thead>
                            <tr>
                                <th>Order ID</th>
                                <th>Customer Mobile</th>
                                <th>Shipping Address</th>
                                <th>Total Item</th>
                                <th>Total Amount</th>
                                <th>Order Request</th>
                                <th>Status</th>
                                <th>Details</th>
                                <th>Delivery Proof</th>

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

                                <?php foreach ($approved as $order) : ?>
                                    <tr>
                                        <td>#<?= $order->invoice_id ?></td>
                                        <td><?= $order->customer_mob ?></td>
                                        <td><?= $order->adress ?></td>
                                        <td><?= $order->total_item ?>/=</td>
                                        <td>৳ <?= number_format($order->total_sales, 2) ?></td>
                                        <td>
                                            <?= nice_date($order->created_at, 'd-m-y h:i:a') ?>
                                        </td>
                                        <td>

                                            <span class="badge <?php if ($order->status == 2) {
                                                                    echo 'badge-danger';
                                                                } elseif ($order->status == 1) {
                                                                    echo 'badge-success';
                                                                } else {
                                                                    echo 'badge-info';
                                                                } ?>">

                                                <?php if ($order->status == 1) : ?>
                                                    Accepted
                                                <?php elseif ($order->status == 2) : ?>
                                                    Rejected
                                                <?php else : ?>
                                                    Pending
                                                <?php endif; ?>
                                            </span>

                                        </td>
                                        <td><a class="badge badge-info" href="<?= base_url('OrderCtrl/marchant_order_details/' . $order->order_id) ?>">Details</a>
                                        </td>
                                        <td>
                                            <?php if ($order->marchant_status == 0) : ?>
                                                <a style="cursor: pointer;" onclick="edit_data_set(this)" data-toggle="modal" data-target="#editModal" data-invoice_id="<?= $order->invoice_id ?>" data-order_id="<?= $order->order_id ?>" class="badge badge-primary">Delivery Proof
                                                </a>
                                            <?php elseif ($order->marchant_status == 1) : ?>
                                                <span class="badge badge-warning">On Checking</span>
                                            <?php else : ?>
                                                <span class="badge badge-warning">Complicated</span>
                                            <?php endif ?>

                                        </td>

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

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

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

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

            <!-- Modal body -->
            <div class="modal-body">
                <input type="hidden" id="order_id">
                <input type="hidden" id="invoice_id">
                <h6><i class="mdi mdi-image"></i>Delivery Slip</h6>
                <input type="file" onchange="uploadImage(this)" name="logo" id="image" />
                <br> <br>
                <img src="" id="logo_id" width="100px" height="80px">
                <br><br>
                <h6><i class="mdi mdi-message-outline"></i> Note</h6>
                <textarea id="note_id" cols="30" rows="3" class="form-control" placeholder="Write Note....."></textarea>


                <div id="b_result" class="text-center"></div>
            </div>

            <!-- Modal footer -->
            <div class="modal-footer">
                <button onclick="deliveryInfo()" type="button" class="btn btn-info">Submit </button>
                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
            </div>

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

<script>
    function edit_data_set(e) {
        $("#order_id").val($(e).data('order_id'));
        $("#invoice_id").val($(e).data('invoice_id'));
        $("#note_id").val($(e).data('note'));
    }

    function uploadImage(event) {
        $("#logo_id").attr("src", '');
        var image = document.getElementById('logo_id');
        var file = document.getElementById('image');
        image.src = URL.createObjectURL(file.files[0]);

    }

    function deliveryInfo() {

        let image = $("#image").prop('files')[0];

        let order_id = $("#order_id").val();
        let invoice_id = $("#invoice_id").val();
        let note = $("#note_id").val();
        let marchant_id = "<?= $this->session->userdata('user_id') ?>";

        var formData = new FormData();
        formData.append('note', note);
        formData.append('invoice_id', invoice_id);
        formData.append('image', image);
        formData.append('order_id', order_id);
        formData.append('marchant_id', marchant_id);
        if (image) {
            $.ajax({
                type: "post",
                url: "https://myadmin.mali-mart.com/api/delivery/proof",
                data: formData,
                contentType: false,
                processData: false,
                dataType: "json",
                success: function(response) {
                    if (response.success === true) {
                        alert("Your delivery slip successfully submitted !!");
                        window.location.reload();
                    } else {
                        alert("sometimes wrong");
                    }

                }
            });
        } else {
            alert('Please upload your delivery slip !!');
        }

    }
</script>