/******************************************************************************
 *   ______                                                                   *
 *  /       \                                                                 *
 * /$$$$$$  | ________  __    __   ______    ______                           *
 * $$ |__$$ |/        |/  |  /  | /      \  /      \                          *
 * $$    $$ |$$$$$$$$/ $$ |  $$ |/$$$$$$  |/$$$$$$  |                         *
 * $$$$$$$$ |  /  $$/  $$ |  $$ |$$ |  $$/ $$    $$ |                         *
 * $$ |  $$ | /$$$$/__ $$ \__$$ |$$ |      $$$$$$$$/                          *
 * $$ |  $$ |/$$      |$$    $$/ $$ |      $$       |                         *
 * $$/   $$/ $$$$$$$$/  $$$$$$/  $$/        $$$$$$$/                          *
 *                                                                            *
 * @PROJECT    : 3D Preview Extension [Magetop.com]                           *
 * @AUTHOR     : Azure - Developer                                            *
 * @COPYRIGHT  : © 2019  Magetop.com                                          *
 * @LINK       : https://Magetop.com                                          *
 * @CREATED    :  10/01/2022                                                  *
 ******************************************************************************/

define([
    'jquery',
    'mage/translate',
    'mage/validation',
    'Magento_Ui/js/modal/confirm',
    'Magento_Ui/js/modal/modal',
    'Magento_Ui/js/modal/alert',
    'mgt_advance_preview_helper',
    'product_preview_pro'
],function ($,$t,validate,confirmation,modal,alert,helper,productPreviewPro) {
    var customerPreviewOrder = {};
    customerPreviewOrder = {
        version: '2.0.0',
        re_upload_design_url: '',
        order_detail_url: '',
        update_status_url: '',
        customer_email: '',
        customer_name: '',
        customer_id: 0,
        order_increment: '',
        order_id: 0,
        uploadImage : function(itemId) {
            var self = this;
            var data = new FormData($('#form-upload-custom-design-item-' + itemId)[0]);
            var url = self.re_upload_design_url;
            // $('#demo-3d-test').html('');

            helper.uploadImages(url, data, 'advance-preview-loading', '', function (response) {
                if(response.status === 'success') {
                    let previewData = response.preview_filed || null;
                    if(previewData) {
                        helper.showLoading('advance-preview-loading')
                        $('#preview-custom-order-preview-item-'+itemId).html('');
                        var elementId = 'preview-custom-order-preview-item-'+itemId;
                        var namePreview = previewData.name_preview;
                        var light = previewData.light;
                        var zoom = previewData.zoom
                        var modelPreview = previewData.model_preview;
                        var productId = previewData.product_id
                        // light = parseInt(light);
                        // zoom = parseInt(zoom);

                        var defaultModel = 'product_id_'+productId+'/'+modelPreview+'/'+namePreview;
                        console.log(elementId,namePreview,light,zoom,defaultModel);
                        // productPreviewPro.productPreviewImage(defaultModel, zoom, light,elementId);
                        setTimeout(function () {
                            productPreviewPro.productPreviewImage(defaultModel, zoom, light,elementId);
                            window.location.href = self.order_detail_url;
                        },100)
                    }
                }
            })
        },
        updateCustomPrintStatus : function(itemId, status,adminUpload,productName,qty,total) {
            var self = this;
            var url = self.update_status_url;
            var loading = 'advance-preview-loading';
            // var isUpdateAdminUpload = 0;

            var data = {
                item_id: itemId,
                status: status,
                admin_upload: adminUpload,
                customer_name: self.customer_name,
                customer_email: self.customer_email,
                product_name: productName,
                txt_order: self.order_increment,
                customer_id: self.customer_id,
                order_id: self.order_id,
                qty: qty,
                total: total,
            };
            console.log(data);
            // $('#product-custom-order-message').css('display','none');
            helper.doRequest(url,data,loading,'product-custom-order-message',function (response) {
                if(response.status === 'success') {
                    $('#preview-project-item-status-' + itemId).html(response.item_status);
                    // console.log(response);
                    setTimeout(function () {
                        window.location.href = self.order_detail_url;
                    },500)
                }
            })
        },
        initData : function() {
            var self = this;
            self.re_upload_design_url = $('#txt-re-upload-design-url').val();
            self.order_detail_url = $('#txt-order-detail-url').val();
            self.update_status_url = $('#txt-update-item-status-url').val();
            self.customer_name = $('#txt-customer-name').val();
            self.customer_id = $('#txt-customer-id').val();
            self.customer_email = $('#txt-customer-email').val();
            self.order_increment = $('#txt-order-increment-id').val();
            self.order_id = $('#txt-order-id').val();
        },
        init: function () {
            console.log("previewCustomOrder LOADER")
            var self = this;
            self.initData();
            /*$('.custom-order-items-image').each(function () {
                var elementId = $(this).attr('id');
                var namePreview = $(this).attr('preview-file-name');
                var light = $(this).attr('preview-light');
                var zoom = $(this).attr('preview-zoom');
                var modelPreview = $(this).attr('model-preview');
                var productId = $(this).attr('product-id');
                light = parseInt(light);
                zoom = parseInt(zoom);


                // console.log(elementId,namePreview,light,zoom,defaultModel);

                if(elementId === undefined || namePreview === undefined || light === undefined || productId === undefined || modelPreview === undefined) {
                    console.log("Model not found");
                }
                else {
                    var defaultModel = 'product_id_'+productId+'/'+modelPreview+'/'+namePreview;
                    productPreviewPro.productPreviewImage(defaultModel, zoom, light,elementId);
                }

            })*/
            $('.btn-preview-upload-custom-design').click(function () {
                var itemId = $(this).attr('item-id');
                $('#txt-preview-upload-custom-design-'+itemId).click();
            })
            $('.txt-preview-upload-custom-design').change(function () {
                var itemId = $(this).attr('item-id');
                self.uploadImage(itemId);
            })
            $('.btn-update-design-item-status').click(function() {
                var itemId = $(this).attr('item-id');
                let status = $(this).attr('item-status');
                // var productName = $(this).attr('product-name');
                 var productName = $(this).attr('project-name');
                var qty = $(this).attr('item-qty');
                var total = $(this).attr('total');
                self.updateCustomPrintStatus(itemId,status,'',productName,qty,total);
            })
            $('.btn-approved-admin-create-design').click(function() {
                var itemId = $(this).attr('item-id');
                let status = $(this).attr('item-status');
                let adminUpload = $(this).attr('admin-file');
                // var productName = $(this).attr('product-name');
                var productName = $(this).attr('project-name');
                var qty = $(this).attr('item-qty');
                var total = $(this).attr('total');
                self.updateCustomPrintStatus(itemId,status,adminUpload,productName,qty,total);
            })
            $('.btn-reject-admin-create-design').click(function() {
                var itemId = $(this).attr('item-id');
                let status = $(this).attr('item-status');
                let adminUpload = $(this).attr('admin-file');
                // var productName = $(this).attr('product-name');
                var productName = $(this).attr('project-name');
                var qty = $(this).attr('item-qty');
                var total = $(this).attr('total');
                self.updateCustomPrintStatus(itemId,status,adminUpload,productName,qty,total);
            })
        }
    }
    return customerPreviewOrder;
})
