define([
    'jquery',
    'Magento_Ui/js/modal/confirm',
    'Magento_Ui/js/modal/modal',
    'Magento_Ui/js/modal/alert',
    'mgt_advance_preview_helper',
],function ($,confirmation,modal,alert,helper) {
    var previewForm = {
        version: '2.5.0',
        upload_url: '',
        items_url: '',
        edit_url: '',
        product_id: 0,
        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);
                }

            })
        },
        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);
                }
            })
        },
        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.product_id = $('#txt-product-id').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');
            })
            $('#preview-product-form-content').on("click","#btn-save-preview",function () {
                console.log("Click click");
                self.uploadMediaPreview();
            })
            $('#btn-add-product-preview').click(function () {
                self.editItem(0);
            })
            setTimeout(function () {
                self.getItems();
            },100)

        }
    }
    return previewForm;
})
