{% put scripts %}

<script>
    function formatDate(date) {
        var d = new Date(date),
            month = '' + (d.getMonth() + 1),
            day = '' + d.getDate(),
            year = d.getFullYear();

        hour = d.getHours();
        minute = d.getMinutes();

        if (month.length < 2)
            month = '0' + month;
        if (day.length < 2)
            day = '0' + day;

        var date = [day, month, year].join('.');
        var time = [hour, minute].join(':');

        var result = date + " / " + time;

        return result;
    }

    getOrder(`{{sellerOrderId}}`);
    // console.log("qqq");
    function getOrder(sellerOrderId) {
        let formData = new FormData()
        formData.append('user', "romanah_")
        formData.append('password', "bt110226$$")
        formData.append('order_id', sellerOrderId)

        $(document).ready(function () {
            $.ajax({
                url: `https://nurgul.com.tm/app/api/seller/order`,
                type: 'POST',
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                beforeSend: function () {
                     $('#order_content').hide();
                     $('#loader_seller_order').show();
                },
                success: function (data, textStatus, xhr) {
                     $('#loader_seller_order').hide();
                     $('#order_content').show();


                    console.log(data.data);

                    var orderItems = data.items.data;
                    var order = data.order;

                    $('#order_status').html(order.status);
                    $('#order_total').html(order.grand_total + " TMT");
                    $('#order_customer').html(order.order.customer_first_name);
                    $('#order_no').html("#Sargyt "+order.order.id);
                    // $('#order_phone').html(order.order.customer.phone);
                    $('#order_date').html(formatDate(order.created_at));
                    // $('#order_address').html(order.order.shipping_address[0].address1);


                    for (var item of orderItems) {
                        $('#order_items').append(`
                            <tr>
                                <td class="!py-4">
                                    <div class="flex items-center">
                                        <div class="w-10 h-10 image-fit zoom-in">
                                            <img alt="`+ item.item.name +`"
                                                class="rounded-lg border-2 border-white shadow-md tooltip"
                                                src="`+ item.item.product.images[0].url +`" title="`+ item.item.name +`">
                                        </div>
                                        <a href="" class="font-medium whitespace-nowrap ml-4">`+ item.item.name +`</a>
                                    </div>
                                </td>
                                <td class="text-center">`+item.item.base_price+` TMT</td>
                                <td class="text-center">`+item.item.qty_ordered+`</td>
                                <td class="text-center">`+item.item.base_total+` TMT</td>
                            </tr>
                        `);
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    console.log('Error in Operation');
                }
            });
        });
    }
</script>
{% endput %}
