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',
],function ($,$t,validate,confirmation,modal,alert,helper) {
    var previewAdditionalForm = {
        version: '2.5.0',
        save_url: '',
        items_url: '',
        edit_url: '',
        dell_url: '',
        product_id: 0,
        saveItem : function() {
            console.log("additional JS ");
            var self = this;
            var url = self.save_url;
            var loading = 'advance-preview-loading';
            var data = new FormData($('#preview-product-additional-form')[0]);
            helper.uploadImages(url, data, loading, 'product-additional-review-message', function (response) {
                // console.log(response)
                // window.location.reload();
                // window.location.href=window.location.href
                // console.log("upload done", response);

                if(response.status === 'success') {
                    $('#product-additional-review-message').html(response.message);
                    $('#preview-product-additional-form-content').css('display','none');
                    $('#preview-product-additional-lists-content').css('display','block');
                    $('#preview-product-additional-lists-content').html(response.html);
                }

            })
        },
        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-additional-review-message').css('display','none');
            helper.doRequest(url,data,loading,'',function (response) {

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

                if(response.status === 'success') {
                    $('#preview-product-additional-lists-content').css('display','none');
                    $('#preview-product-additional-form-content').css('display','block');
                    $('#preview-product-additional-form-content').html(response.html);
                }
            })
        },
        dellItem: function(id) {
            var self = this;
            var url = self.dell_url;
            var loading = 'advance-preview-loading';
            var data = {
                product_id: self.product_id,
                addition_id: id,
            };
            $('#product-review-additional-message').css('display','none');
            helper.doRequest(url,data,loading,'product-review-additional-message',function (response) {

                if(response.status === 'success') {
                    $('#product-additional-review-message').html(response.message);
                    $('#preview-product-additional-lists-content').html(response.html);
                }
            })
        },
        initData : function() {
            var self = this;
            self.save_url = $('#txt-preview-additional-save-url').val();
            self.items_url = $('#txt-preview-additional-items-url').val();
            self.edit_url = $('#txt-preview-additional-edit-url').val();
            self.dell_url = $('#txt-preview-additional-dell-url').val();
            self.product_id = $('#txt-product-additional-id').val();
        },
        init: function () {
            var self = this;
            self.initData();
            console.log("additional Loading loading")
           /* $('#DCV3DAP').click(function () {
                console.log("Click");
            })*/
            var listElement = '#preview-product-additional-lists-content';
          /*  $('#btn-add-preview').click(function () {
                console.log("upload product hihi");
                self.uploadMediaPreview();
            })*/
            $(listElement).on("click",".preview-additional-item-edit",function () {
                var id = $(this).attr('additional-id');
                self.editItem(id);
                // $('#preview-product-form-content').css('display','block');
            })
            $(listElement).on("click",".preview-additional-item-delete",function () {
                var id = $(this).attr('additional-id');
                confirmation({
                    title: $t('confirm'),
                    content: $t('Are you sure Delete Item'),
                    actions: {
                        confirm: function () {
                            self.dellItem(id);
                        },
                        cancel: function () {
                            return false;
                        },
                        always : function () {
                            return false;
                        }
                    },
                });

                // $('#preview-product-form-content').css('display','block');
            })
            $('#preview-product-additional-form-content').on("click","#btn-save-preview-additional",function () {
                console.log("Click save");
                var dataForm = $('#preview-product-additional-form');
                dataForm.mage('validation', {});
                setTimeout(function () {
                    var status = dataForm.validation('isValid');
                    if(status) {
                        self.saveItem();
                    }
                },100)

            })
            $('#preview-product-additional-form-content').on("click","#btn-close-preview-additional",function () {
                $('#preview-product-additional-form-content').css('display','none');
                $('#preview-product-additional-lists-content').css('display','block');
            })
            $('#btn-add-product-additional-preview').click(function () {
                self.editItem(0);
            })
            setTimeout(function () {
                self.getItems();
            },100)

        }
    }
    return previewAdditionalForm;
})
