var item_detail = {
    settings: {
        item_id: null,
        itemPrice_ajaxUrl: '',
        itemPrice_formUrl: '',
        itemPrice_formActionUrl: '',
    },
    itemPriceDataList: 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);
                    }
                }
            }
        });

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