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 previewDiscoutForm = {
        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-discount-form')[0]);
            helper.uploadImages(url, data, loading, 'product-discount-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-discount-review-message').html(response.message);
                    // $('#product-discount-review-message').css('display','block');
                    $('#preview-product-discount-form-content').css('display','none');
                    $('#preview-product-discount-lists-content').css('display','block');
                    $('#preview-product-discount-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-discount-review-message').css('display','none');
            helper.doRequest(url,data,loading,'',function (response) {

                if(response.status === 'success') {
                    $('#preview-product-discount-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,
                discount_id: discountId,
            };
            console.log("Edit ", data);
            $('#product-discount-review-message').css('display','none');
            helper.doRequest(url,data,loading,'',function (response) {

                if(response.status === 'success') {
                    $('#preview-product-discount-lists-content').css('display','none');
                    $('#preview-product-discount-form-content').css('display','block');
                    $('#preview-product-discount-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,
                discount_id: discountId,
            };
            $('#product-review-discount-message').css('display','none');
            helper.doRequest(url,data,loading,'product-review-discount-message',function (response) {

                if(response.status === 'success') {
                    $('#product-discount-review-message').html(response.message);
                    // $('#product-discount-review-message').css('display','block');
                    $('#preview-product-discount-lists-content').html(response.html);
                }
            })
        },
        initData : function() {
            var self = this;
            self.save_url = $('#txt-preview-discount-upload-url').val();
            self.items_url = $('#txt-preview-discount-items-url').val();
            self.edit_url = $('#txt-preview-discount-edit-url').val();
            self.dell_url = $('#txt-preview-discount-dell-url').val();
            self.product_id = $('#txt-product-discount-id').val();
        },
        init: function () {
            var self = this;
            self.initData();
            console.log("Discount Loading loading")
           /* $('#DCV3DAP').click(function () {
                console.log("Click");
            })*/
            var listElement = '#preview-product-discount-lists-content';
          /*  $('#btn-add-preview').click(function () {
                console.log("upload product hihi");
                self.uploadMediaPreview();
            })*/
            $(listElement).on("click",".preview-discount-item-edit",function () {
                var discountId = $(this).attr('discount-id');
                self.editItem(discountId);
                // $('#preview-product-form-content').css('display','block');
            })
            $(listElement).on("click",".preview-discount-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-discount-form-content').on("click","#btn-save-preview-discount",function () {
                console.log("Click save");
                var dataForm = $('#preview-product-discount-form');
                dataForm.mage('validation', {});
                setTimeout(function () {
                    var status = dataForm.validation('isValid');
                    if(status) {
                        self.saveItem();
                    }
                },100)

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

        }
    }
    return previewDiscoutForm;
})
