define([
    'jquery',
    'mage/translate',
    'Magento_Ui/js/modal/confirm',
    'Magento_Ui/js/modal/modal',
    'Magento_Ui/js/modal/alert',
    'mgt_advance_preview_helper',
],function ($,$t,confirmation,modal,alert,helper) {
    var previewForm = {
        version: '2.5.0',
        upload_url: '',
        items_url: '',
        edit_url: '',
        dell_url: '',
        product_id: 0,
        upload_own_design_url: '',
        uploadMediaPreview : function() {
            console.log("uploadMediaPreview");
            var self = this;
            var url = self.upload_url;
            var loading = 'advance-preview-loading';
            var data = new FormData($('#preview-product-form')[0]);
            helper.uploadImages(url, data, loading, 'product-review-message', function (response) {
                // console.log(response)
                // window.location.reload();
                // window.location.href=window.location.href
                // console.log("upload done", response);
                $('#product-review-message').html(response.message);
                if(response.status === 'success') {
                    $('#preview-product-form-content').css('display','none');
                    $('#preview-product-lists-content').css('display','block');
                    $('#preview-product-lists-content').html(response.html);
                }

            })
        },
        download : function (source){
            console.log(source);
            const fileName = source.split('/').pop();
            var el = document.createElement("a");
            el.setAttribute("href", source);
            el.setAttribute("download", fileName);
            document.body.appendChild(el);
            el.click();
            el.remove();
        },
        uploadOwnDesign : function(type) {
            console.log("uploadOwnDesign",type);
            var self = this;
            var url = self.upload_own_design_url;
            var loading = 'advance-preview-loading';
            let formId = '#form-upload-own-design-pdf';
            if(type === 'ai') {
                formId = '#form-upload-own-design-ai';
            }
            console.log(formId);
            var data = new FormData($(formId)[0]);
            helper.uploadImages(url, data, loading, '', function (response) {
                console.log(response);
            })
        },
        getItems: function() {
            var self = this;
            var url = self.items_url;
            var loading = 'advance-preview-loading';
            var data = {
                product_id: self.product_id,
            };
            console.log("loading iterms")
            $('#product-review-message').css('display','none');
            helper.doRequest(url,data,loading,'',function (response) {

                if(response.status === 'success') {
                    $('#preview-product-lists-content').html(response.html);
                }
            })
        },
        editItem: function(previewId) {
            var self = this;
            var url = self.edit_url;
            var loading = 'advance-preview-loading';
            var data = {
                product_id: self.product_id,
                preview_id: previewId,
            };
            // console.log("loading iterms")
            $('#product-review-message').css('display','none');
            helper.doRequest(url,data,loading,'',function (response) {

                if(response.status === 'success') {
                    $('#preview-product-lists-content').css('display','none');
                    $('#preview-product-form-content').css('display','block');
                    $('#preview-product-form-content').html(response.html);
                }
            })
        },
        dellItem: function(previewId) {
            var self = this;
            var url = self.dell_url;
            var loading = 'advance-preview-loading';
            var data = {
                product_id: self.product_id,
                preview_id: previewId,
            };
            $('#product-review-discount-message').css('display','none');
            helper.doRequest(url,data,loading,'product-review-message',function (response) {
                $('#product-review-message').html(response.message);
                if(response.status === 'success') {
                    $('#preview-product-form-content').css('display','none');
                    $('#preview-product-lists-content').css('display','block');
                    $('#preview-product-lists-content').html(response.html);
                }
            })
        },
        initData : function() {
            var self = this;
            self.upload_url = $('#txt-preview-upload-url').val();
            self.items_url = $('#txt-preview-items-url').val();
            self.edit_url = $('#txt-preview-edit-url').val();
            self.dell_url = $('#txt-preview-dell-url').val();
            self.product_id = $('#txt-product-id').val();
            self.upload_own_design_url = $('#txt-preview-upload-own-design-url').val();
        },
        init: function () {
            var self = this;
            self.initData();
            console.log("OPD loading")
           /* $('#DCV3DAP').click(function () {
                console.log("Click");
            })*/
            var listElement = '#preview-product-lists-content';
          /*  $('#btn-add-preview').click(function () {
                console.log("upload product hihi");
                self.uploadMediaPreview();
            })*/
            $(listElement).on("click",".preview-item-edit",function () {
                var previewId = $(this).attr('preview-id');
                self.editItem(previewId);
                // $('#preview-product-form-content').css('display','block');
            })
            $(listElement).on("click",".preview-item-delete",function () {
                var previewId = $(this).attr('preview-id');

                confirmation({
                    title: $t('confirm'),
                    content: $t('Are you sure Delete Item'),
                    actions: {
                        confirm: function () {
                            self.dellItem(previewId);
                        },
                        cancel: function () {
                            return false;
                        },
                        always: function () {
                            return false;
                        }
                    },
                })
                // $('#preview-product-form-content').css('display','block');
            })
            $('#preview-product-form-content').on("click","#btn-save-preview",function () {
                console.log("Click click");
                self.uploadMediaPreview();
            })
            $('#preview-product-form-content').on("click","#btn-close-preview",function () {
                // console.log("Click click");
                $('#preview-product-form-content').css('display','none');
                $('#preview-product-lists-content').css('display','block');
            })
            $('#btn-add-product-preview').click(function () {
                self.editItem(0);
            })
            $('#txt-design-preview-pdf').change(function () {
                self.uploadOwnDesign('pdf');
            })
            $('#txt-design-preview-ai').change(function () {
                self.uploadOwnDesign('ai');
            })
            $('.preview-download-sample-file').click(function () {
                var source = $(this).attr('custom-url');
                self.download(source);
            })
            setTimeout(function () {
                self.getItems();
            },100)

        }
    }
    return previewForm;
})
