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 dailyShippingForm = {
        version: '2.5.0',
        save_url: '',
        items_url: '',
        edit_url: '',
        dell_url: '',
        product_id: 0,
        saveItem : function() {
            console.log("uploadMediaPreview");
            var self = this;
            var url = self.save_url;
            var loading = 'advance-preview-loading';
            var data = new FormData($('#preview-product-daily-shipping-form')[0]);
            helper.uploadImages(url, data, loading, 'product-daily-shipping-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-daily-shipping-review-message').html(response.message);
                    // $('#product-daily-shipping-review-message').css('display','block');
                    $('#preview-product-daily-shipping-form-content').css('display','none');
                    $('#preview-product-daily-shipping-lists-content').css('display','block');
                    $('#preview-product-daily-shipping-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-daily-shipping-review-message').css('display','none');
            helper.doRequest(url,data,loading,'',function (response) {

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

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

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

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

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

        }
    }
    return dailyShippingForm;
})
