var supplier = {
    settings: {
        ajaxUrl: '',
        status: '',
        approvalFormUrl: '',
        approvalSaveUrl: '',
    },
    init: function() {
        supplier.initDataTable();
    },
    initDataTable: function() {

        const callBack = function() {
            jQ('.anchor-approval-form').unbind('click').bind('click', supplier.approvalForm);
            global.askContinue();
        };

        supplier.dataList = jQ('#supplier-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': supplier.settings.ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.status = jQ('#status-filter').val();
                }
            },
            'columnDefs': [
                { 'orderable': false, 'targets': 8 },
                { 'searchable': false, 'targets': 8 }
            ],
            drawCallback: callBack,
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack);
                    }
                }
            }
        });

        if(supplier.settings.status != null || supplier.settings.status != '') {
            jQ("#status-filter option").filter(function() {
                return this.text == supplier.settings.status;
            }).attr('selected', true);
            supplier.dataList.draw();
        }



        jQ('#status-filter').on('change', function(){
            supplier.dataList.draw();
        });

        jQ('.content-container').removeClass('has-loading');
        jQ('.content-container-content').removeClass('hide');
    },
    approvalForm: function() {

        const _self = jQ(this),
            modal = jQ('#modal'),
            ajaxName = 'SUPPLIER_APPROVAL_FORM';

        modal.on('closed.zf.reveal', function(){
            global.ajaxCancel(ajaxName);
        });

        // _self.addClass('action-button-loading');

        global.ajaxCancel(ajaxName);

        modal.html('')
            .addClass('has-loading-white')
            .foundation('open');

        global.ajax({
            url: supplier.settings.approvalFormUrl,
            type: 'GET',
            data: {
                id: _self.data('id')
            },
            success: function(response) {
                if(!response.success) {
                    location.reload();
                    return;
                }
                modal.foundation('close')
                    .removeClass('has-loading-white')
                    .html(response.html)
                    .foundation('open');
                //_self.removeClass('action-button-loading');
                //jQ('#modal').html(response.html).foundation('open');
                jQ('.btn-supplier-approval-axn').unbind('click.approvalSave').bind('click.approvalSave', supplier.approvalSave);
                jQ("#supplierApprovalForm_approvalHistory-datalist").DataTable({});
            },
            error: function(xhr, txt) {
                modal.foundation('close');
                //_self.removeClass('action-button-loading');
                if('abort' !== txt) {
                    alert('Something went wrong, please try again.');
                }
            }
        }, ajaxName);
    },
    approvalSave: function() {

        if(!confirm('Are you sure you want to continue?')) {
            return;
        }

        jQ('#modal input[name="form[axn]"]').val(jQ(this).data('axn'));

        global.ajax({
            url: supplier.settings.approvalSaveUrl,
            type: 'POST',
            data: jQ('#frm-supplier-approval').serialize(),
            beforeSend: function() {
                global.formSubmitted();
            },
            success: function(response) {
                if(!response.success) {
                    location.reload();
                    return;
                }
                supplier.dataList.draw();
                jQ('#modal').foundation('close');
                global.formSubmittedReverse();
                global.countNotif();
                alert(response.msg);
            },
            error: function(xhr, txt) {
                if('abort' !== txt) {
                    alert('Something went wrong, please try again.');
                }
                global.formSubmittedReverse();
            }
        }, 'SUPPLIER_APPROVAL_SAVE');
    }
};