(function ($) {
    "use strict";


    $('body').on('click', '.js-view-tracking-code', function () {
        const $this = $(this);
        const saleId = $this.attr('data-sale-id');
        const orderId = $this.attr('data-product-order-id');

        const path = `/panel/store/purchases/${saleId}/getProductOrder/${orderId}`;

        $.get(path, function (result) {
            if (result && result.order) {

                const html = `
                    <div>
                        <div class="section-title">
                            <h3 class="section-title__heading font-20 text-dark mb-24">${viewTrackingCodeModalTitleLang}</h3>
                        </div>

                        <div class="mt-16 w-100">
                            <div class="">
                                <span class="font-weight-500 mr-4">${trackingCodeLang} :</span>
                                <span class="font-14 text-gray-500">${result.order.tracking_code}</span>
                            </div>

                            <div class="mt-16">
                                <span class="font-weight-500 mr-4">${addressLang} :</span>
                                <span class="font-14 text-gray-500">${result.order.address}</span>
                            </div>
                        </div>

                        <div class="mt-32 d-flex align-items-center justify-content-end">
                            ${
                    (result.shipping_tracking_url && result.shipping_tracking_url !== '' && result.shipping_tracking_url !== 'null') ?
                        `
                                            <div class="mr-20">
                                                <a href="${result.shipping_tracking_url}" class="btn btn-primary btn-sm">${shippingTrackingUrlLang}</a>
                                            </div>
                                           `
                        : ''
                }

                            <button type="button" class="btn btn-danger btn-sm close-swl">${closeLang}</button>
                        </div>
                    </div>
                `;

                Swal.fire({
                    html: html,
                    showCancelButton: false,
                    showConfirmButton: false,
                    customClass: {
                        content: 'p-0 text-left',
                    },
                    width: '40rem',
                });
            }
        });
    });

    $('body').on('click', '.js-got-the-parcel', function () {
        const $this = $(this);
        const saleId = $this.attr('data-sale-id');
        const orderId = $this.attr('data-product-order-id');

        const path = `/panel/store/purchases/${saleId}/productOrder/${orderId}/setGotTheParcel`;

        const html = `
                    <div class="p-16">
                        <div class="section-title">
                            <h3 class="section-title__heading font-20 text-dark mb-24">${gotTheParcelLang}</h3>
                        </div>

                        <div class="mt-16 w-100">
                            <p class="">${gotTheParcelConfirmTextLang}</p>
                        </div>

                        <div class="mt-32 d-flex align-items-center justify-content-end">
                            <button type="button" id="saveGotTheParcel" data-action="${path}" class="btn btn-sm btn-primary">${confirmLang}</button>
                            <button type="button" class="btn btn-danger btn-sm close-swl ml-8">${closeLang}</button>
                        </div>
                    </div>
                `;

        Swal.fire({
            html: html,
            showCancelButton: false,
            showConfirmButton: false,
            customClass: {
                content: 'p-0 text-left',
            },
            width: '32rem',
        });
    });

    $('body').on('click', '#saveGotTheParcel', function (e) {
        e.preventDefault();
        const $this = $(this);
        const path = $this.attr('data-action');

        $this.addClass('loadingbar primary').prop('disabled', true);

        $.get(path, function (result) {
            if (result && result.code === 200) {
                Swal.fire({
                    icon: 'success',
                    html: '<h3 class="font-20 text-center text-dark py-24">' + gotTheParcelSaveSuccessLang + '</h3>',
                    showConfirmButton: false,
                    width: '25rem',
                });

                setTimeout(() => {
                    window.location.reload();
                }, 500);
            } else {
                Swal.fire({
                    icon: 'error',
                    html: '<h3 class="font-20 text-center text-dark py-24">' + gotTheParcelSaveErrorLang + '</h3>',
                    showConfirmButton: false,
                    width: '25rem',
                });
            }
        });
    });



})(jQuery);
