var item_detail = {
    settings: {
        item_id: null,
        itemPrice_ajaxUrl: '',
        itemPrice_formUrl: '',
        itemPrice_formActionUrl: '',
        item_ajaxUrl: '',
        item_formUrl: '',
        item_autocomplete_url: '',
        tax_ajaxUrl: '',
        tax_formUrl: '',
        ave_cost_ajaxUrl: '',
        ave_cost_formUrl: '',
    },
    itemPriceDataList: null,
    itemDataList: null,
    taxDataList: null,
    init: function() {
        item_detail.initDataTable();
        item_detail.bindItemPriceForm();
    },
    bindItemPriceForm: function() {
        if(jQ('.href-item-price-form').length > 0) {

            jQ('.href-item-price-form').unbind('click').bind('click', function(){

                var requestName = 'itemPriceForm',
                    modal = jQ('#modal'),
                    modalContent = jQ('#modal-content');

                global.ajaxCancel(requestName);

                var settings = {
                    url: item_detail.settings.itemPrice_formUrl + '/' + jQ(this).data('action') + '/' + jQ(this).data('id'),
                    type: 'GET',
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open');
                    },
                    success: function(json) {
                        modal.foundation('close');
                        modalContent.removeClass('has-loading').html(json.html);
                        modal.foundation('open');
                        global.bindFormSubmitted();
                        item_detail.bindItemPriceFormAction();
                    }
                };

                global.ajax(settings, requestName);
            });
        }
    },

    bindItemPriceFormAction: function() {

        jQ('.href-item-price-action').unbind('click.itemPriceFormAction').bind('click.itemPriceFormAction', function(){

            var requestName = 'itemPriceFormAction',
                modal = jQ('#modal'),
                modalContent = jQ('#modal-content'),
                _form = jQ('#form-itemPrice'),
                axn = jQ(this).data('axn');
            _form.find('.form-action').val(axn);

            global.formSubmitted();
            global.ajaxCancel(requestName);

            var settings = {
                url: item_detail.settings.itemPrice_formActionUrl,
                type: 'POST',
                data: _form.serialize(),
                success: function(json) {
                    if(json.success) {
                        location.reload();
                    } else {
                        global.formSubmittedReverse();
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                    }
                }
            };

            global.ajax(settings, requestName);
        });
    },
    initDataTable: function() {

        var callBack1 = function() {
            item_detail.bindItemPriceForm()
        };

        item_detail.itemPriceDataList = jQ('#itemPrice-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': item_detail.settings.itemPrice_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.item_id = item_detail.settings.item_id;
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': 3 },
                { 'searchable': false, 'targets': 3 }
            ],
            'orderClasses': false,
            drawCallback: function() {
                callBack1();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack1);
                    }
                }
            }
        });


        let callBackItem = () => {
            jQ('.href-new-item').unbind('click').bind('click', function () {
                const id = jQ(this).data('id'),
                    action = jQ(this).data('action'),
                    modal = jQ('#modal'),
                    modalContent = jQ('#modal-content'),
                    settings = {
                        type: 'GET',
                        url: `${item_detail.settings.item_formUrl}/${action}/${id}`,
                        beforeSend: function() {
                            modalContent.html('').addClass('has-loading');
                            modal.foundation('open');
                        },
                        success: function(json) {
                            if(json.success) {
                                modal.foundation('close');
                                modalContent.removeClass('has-loading').html(json.html);
                                modal.foundation('open');

                                global.bindFormSubmitted();
                                global.autocomplete.bind(item_detail.settings.item_autocomplete_url, '#itemItem_item_description', '#itemItem_item');
                                jQ('#item_form').submit(function (e) {
                                    e.preventDefault();
                                    let settings = {
                                        type: 'POST',
                                        url: jQ('#item_form').attr('action'),
                                        data: jQ('#item_form').serialize(),
                                        success: function(json) {
                                            if(json.success) {
                                                modal.foundation('close');
                                                item_detail.itemDataList.draw();
                                                global.flashMessage('success', json.messages)
                                            } else {
                                                global.flashMessage('error', json.messages)
                                            }
                                        }
                                    };
                                    global.ajax(settings, 'payrollPreview');
                                });
                            } else {
                                location.reload();
                            }
                        }
                    };
                global.ajax(settings, 'payrollPreview');
            });
        };
        item_detail.itemDataList = jQ('#item-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': item_detail.settings.item_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.item_id = item_detail.settings.item_id;
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': jQ('#item-datalist th').length - 1 },
                { 'searchable': false, 'targets': jQ('#item-datalist th').length - 1 }
            ],
            'orderClasses': false,
            drawCallback: function() {
                callBackItem();
            },
        });

        let callBackTax = () => {
            jQ('.href-tax-form').unbind('click').bind('click', function () {
                const id = jQ(this).data('id'),
                    action = jQ(this).data('action'),
                    modal = jQ('#modal'),
                    modalContent = jQ('#modal-content'),
                    settings = {
                        type: 'GET',
                        url: `${item_detail.settings.tax_formUrl}/${action}/${id}`,
                        beforeSend: function() {
                            modalContent.html('').addClass('has-loading');
                            modal.foundation('open');
                        },
                        success: function(json) {
                            if(json.success) {
                                modal.foundation('close');
                                modalContent.removeClass('has-loading').html(json.html);
                                modal.foundation('open');

                                global.bindFormSubmitted();
                                global.autocomplete.bind(item_detail.settings.branch_autocomplete_url, '#item_tax_code_health_facility_description', '#item_tax_code_health_facility');
                                jQ('#tax_form').submit(function (e) {
                                    e.preventDefault();
                                    let settings = {
                                        type: 'POST',
                                        url: jQ('#tax_form').attr('action'),
                                        data: jQ('#tax_form').serialize(),
                                        success: function(json) {
                                            if(json.success) {
                                                modal.foundation('close');
                                                item_detail.taxDataList.draw();
                                                global.flashMessage('success', json.messages)
                                            } else {
                                                global.flashMessage('error', json.messages)
                                            }
                                        }
                                    };
                                    global.ajax(settings, 'payrollPreview');
                                });
                            } else {
                                location.reload();
                            }
                        }
                    };
                global.ajax(settings, 'taxForm');
            });
        };
        item_detail.taxDataList = jQ('#tax-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': item_detail.settings.tax_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.item_id = item_detail.settings.item_id;
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': jQ('#item-datalist th').length - 1 },
                { 'searchable': false, 'targets': jQ('#item-datalist th').length - 1 }
            ],
            'orderClasses': false,
            drawCallback: function() {
                callBackTax();
            },
        });

        let callBackCost = () => {
            jQ('.href-cost-form').unbind('click').bind('click', function () {
                const id = jQ(this).data('id'),
                    action = jQ(this).data('action'),
                    modal = jQ('#modal'),
                    modalContent = jQ('#modal-content'),
                    settings = {
                        type: 'GET',
                        url: `${item_detail.settings.ave_cost_formUrl}/${action}/${id}`,
                        beforeSend: function() {
                            modalContent.html('').addClass('has-loading');
                            modal.foundation('open');
                        },
                        success: function(json) {
                            if(json.success) {
                                modal.foundation('close');
                                modalContent.removeClass('has-loading').html(json.html);
                                modal.foundation('open');

                                global.bindFormSubmitted();
                                global.autocomplete.bind(item_detail.settings.branch_autocomplete_url, '#item_ave_cost_health_facility_description', '#item_ave_cost_health_facility');
                                jQ('#ave_cost_form').submit(function (e) {
                                    e.preventDefault();
                                    let settings = {
                                        type: 'POST',
                                        url: jQ('#ave_cost_form').attr('action'),
                                        data: jQ('#ave_cost_form').serialize(),
                                        success: function(json) {
                                            if(json.success) {
                                                modal.foundation('close');
                                                item_detail.costDataList.draw();
                                                global.flashMessage('success', json.messages)
                                            } else {
                                                global.flashMessage('error', json.messages)
                                            }
                                        }
                                    };
                                    global.ajax(settings, 'itemAveCost');
                                });
                            } else {
                                location.reload();
                            }
                        }
                    };
                global.ajax(settings, 'payrollPreview');
            });
        };
        item_detail.costDataList = jQ('#cost-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': item_detail.settings.ave_cost_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.item_id = item_detail.settings.item_id;
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': jQ('#item-datalist th').length - 1 },
                { 'searchable': false, 'targets': jQ('#item-datalist th').length - 1 }
            ],
            'orderClasses': false,
            drawCallback: function() {
                callBackCost();
            },
        });

        jQ('.content-container').removeClass('has-loading');
        jQ('.content-container-content').removeClass('hide');
    }
};