/*
 * Purpose : Server Side Datatables
 * Author  : Sankar Vellaisamy
 * Created On : 10/2/2017 10:53:00
 */
$(document).ready(function () {
    switch (controller) {
        case 'billing_prasadam':
            /* -------MASTERS - User ----------------*/
            controller = base_url + 'index.php/billing_prasadam/datatable';
            columnDef = [
                {"data": "iterator", "name": "iterator"},
                {"data": "rec_no", "name": "rec_no"},
                {"data": "pname", "name": "pname"},
                {"data": "pperiod", "name": "pperiod"},
                {"data": "contact_no", "name": "contact_no"},
                {"data": "ptype", "name": "ptype"},
                {"data": "pamount", "name": "pamount"},
                {"data": "pdate", "name": "pdate"},
                {"data": "Actions", "name": ""},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'customer':
            /* -------MASTERS - Customer ----------------*/
            controller = base_url + 'index.php/customer/datatable';
            columnDef = [
                {"data": "cId", "name": "cId"},
                {"data": "customer", "name": "customer"},
                {"data": "name", "name": "name"},
                {"data": "phone", "name": "phone"},
                {"data": "cusemail", "name": "cusemail"},
                {"data": "status_str", "name": "status_str"},
                {"data": "Actions", "name": ""},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'supplier':
            /* -------MASTERS - supplier ----------------*/
            controller = base_url + 'index.php/supplier/datatable';
            columnDef = [
                {"data": "supplier_id", "name": "supplier_id"},
                {"data": "supplier_name", "name": "supplier_name"},
                {"data": "scontact_contact_name", "name": "scontact_contact_name"},
                {"data": "supplier_phone", "name": "supplier_phone"},
                {"data": "supplier_email", "name": "supplier_email"},
                {"data": "status_str", "name": "status_str"},
                {"data": "Actions", "name": ""},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'brand':
            /* -------MASTERS - Brand ----------------*/
            controller = base_url + 'index.php/brand/datatable';
            columnDef = [
                {"data": "bId", "name": "bId"},
                {"data": "code", "name": "code"},
                {"data": "name", "name": "name"},
                {"data": "status_str", "name": "status_str"},
                {"data": "Actions", "name": ""},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'category':
            /* -------MASTERS - category ----------------*/
            controller = base_url + 'index.php/category/datatable';
            columnDef = [
                {"data": "tId", "name": "tId"},
                {"data": "name", "name": "name"},
                {"data": "created_date", "name": "created_date"},
                {"data": "status_str", "name": "status_str"},
                {"data": "Actions", "name": ""},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'item':
            /* -------MASTERS - item ----------------*/
            controller = base_url + 'index.php/item/datatable';
            columnDef = [
                {"data": "pid", "name": "pid"},
                {"data": "code", "name": "code"},
                {"data": "catname", "name": "catname"},
                {"data": "pname", "name": "pname"},
                {"data": "sell_price", "name": "sell_price"},
                {"data": "cost_price", "name": "cost_price"},
                {"data": "quantity", "name": "quantity"},
                {"data": "Actions", "name": ""},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'employee':
            /* -------MASTERS - Brand ----------------*/
            controller = base_url + 'index.php/employee/datatable';
            columnDef = [
                {"data": "eId", "name": "eId"},
                {"data": "name", "name": "name"},
                {"data": "desgn", "name": "desgn"},
                {"data": "nric", "name": "nric"},
                {"data": "expiry_date", "name": "expiry_date"},
                {"data": "status_str", "name": "status_str"},
                {"data": "Actions", "name": ""},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'expense_type':
            /* -------MASTERS - expense_type ----------------*/
            controller = base_url + 'index.php/expense_type/datatable';
            columnDef = [
                {"data": "tId", "name": "tId"},
                {"data": "name", "name": "name"},
                {"data": "crated_date", "name": "crated_date"},
                {"data": "status_str", "name": "status_str"},
                {"data": "Actions", "name": ""},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'payment_type':
            /* -------MASTERS - payment_type ----------------*/
            controller = base_url + 'index.php/payment_type/datatable';
            columnDef = [
                {"data": "pId", "name": "pId"},
                {"data": "name", "name": "name"},
                {"data": "created", "name": "created"},
                {"data": "status_str", "name": "status_str"},
                {"data": "Actions", "name": ""},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'purchase_orders':
            /* -------MASTERS - purchase_orders ----------------*/
            controller = base_url + 'index.php/purchase_orders/datatable';
            columnDef = [
                {"data": "poId", "name": "poId"},
                {"data": "poDate", "name": "poDate"},
                {"data": "po_no", "name": "po_no"},
                {"data": "supplier", "name": "supplier"},
                {"data": "po_net_amount", "name": "po_net_amount", "sClass": "text-right"},
                {"data": "Actions", "name": "", "sClass": "text-center"},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'goods_receive':
            /* -------MASTERS - payment_type ----------------*/
            controller = base_url + 'index.php/goods_receive/datatable';
            columnDef = [
                {"data": "pId", "name": "pId"},
                {"data": "inv_no", "name": "inv_no"},
                {"data": "pr_invoice_date", "name": "pr_invoice_date"},
                {"data": "supplier", "name": "supplier"},
                {"data": "net", "name": "net", "sClass": "text-right"},
                {"data": "status_str", "name": "status_str"},
                {"data": "Actions", "name": "", "sClass": "text-center"},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'purchase_payments':
            /* -------MASTERS - purchase_payments ----------------*/
            controller = base_url + 'index.php/purchase_payments/datatable';
            columnDef = [
                {"data": "prId", "name": "prId"},
                {"data": "pr_invoice_date", "name": "pr_invoice_date"},
                {"data": "supplier_invoice_no", "name": "supplier_invoice_no"},
                {"data": "supplier", "name": "supplier"},
                {"data": "net", "name": "net", "sClass": "text-right"},
                {"data": "paid_amount", "name": "paid_amount", "sClass": "text-right"},
                {"data": "status_str", "name": "status_str"},
                {"data": "Actions", "name": "", "sClass": "text-center"},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'purchase_returns':
            /* -------MASTERS - purchase_returns ----------------*/
            controller = base_url + 'index.php/purchase_returns/datatable';
            columnDef = [
                {"data": "prId", "name": "prId"},
                {"data": "inv", "name": "inv"},
                {"data": "inv_date", "name": "inv_date"},
                {"data": "pr_return_date", "name": "pr_return_date"},
                {"data": "return_qty", "name": "return_qty"},
                {"data": "Actions", "name": "", "sClass": "text-center"},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'quotation':
            /* -------MASTERS - quotation ----------------*/
            controller = base_url + 'index.php/quotation/datatable';
            columnDef = [
                {"data": "quoId", "name": "quoId"},
                {"data": "quo_no", "name": "quo_no"},
                {"data": "soDate", "name": "soDate"},
                {"data": "name", "name": "name"},
                {"data": "your_reference", "name": "your_reference"},
                {"data": "total_amt", "name": "total_amt", "sClass": "text-right"},
                {"data": "Actions", "name": ""},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'invoice':
            /* -------MASTERS - invoice ----------------*/
            controller = base_url + 'index.php/invoice/datatable';
            columnDef = [
                {"data": "iterator", "name": "iterator"},
                {"data": "invoice_no", "name": "invoice_no"},
                {"data": "invoice_date", "name": "invoice_date"},
                {"data": "name", "name": "name"},
                {"data": "invoice_reference", "name": "invoice_reference"},
                {"data": "payment_status_str", "name": "payment_status_str"},
                {"data": "invoice_amount", "name": "invoice_amount", "sClass": "text-right"},
                {"data": "Actions", "name": ""},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'invoice_payment':
            /* -------MASTERS - invoice_payment ----------------*/
            controller = base_url + 'index.php/invoice_payment/datatable';
            columnDef = [
                {"data": "pId", "name": "pId"},
                {"data": "inv", "name": "inv"},
                {"data": "pay", "name": "pay"},
                {"data": "date", "name": "date"},
                {"data": "name", "name": "name"},
                {"data": "ref", "name": "ref"},
                {"data": "paid_amount", "name": "paid_amount", "sClass": "text-right", "bSearchable": false, "aTargets": [0]},
                {"data": "status_str", "name": "status_str"},
                {"data": "Actions", "name": "", "sClass": "text-center"},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'invoice_return':
            /* -------MASTERS - invoice_return ----------------*/
            controller = base_url + 'index.php/invoice_return/datatable';
            columnDef = [
                {"data": "srId", "name": "srId"},
                {"data": "ret_date", "name": "ret_date"},
                {"data": "inv", "name": "inv"},
                {"data": "inv_date", "name": "inv_date"},
                {"data": "return_qty", "name": "return_qty"},
                {"data": "Actions", "name": "", "sClass": "text-center"},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'delivery_orders':
            /* -------MASTERS - delivery_orders ----------------*/
            controller = base_url + 'index.php/delivery_orders/datatable';
            columnDef = [
                {"data": "dId", "name": "dId"},
                {"data": "inv", "name": "inv"},
                {"data": "date", "name": "date"},
                {"data": "name", "name": "name"},
                {"data": "do_quantity", "name": "do_quantity"},
                {"data": "so_quantity", "name": "so_quantity"},
                {"data": "Actions", "name": "", "sClass": "text-center"},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'inventory':
            /* -------MASTERS - inventory ----------------*/
            controller = base_url + 'index.php/inventory/datatable';
            columnDef = [
                {"data": "bId", "name": "bId"},
                {"data": "branch", "name": "branch"},
                {"data": "code", "name": "code"},
                {"data": "cat", "name": "cat"},
                {"data": "product", "name": "product"},
                {"data": "quantity", "name": "quantity"},
                {"data": "Actions", "name": "", "sClass": "text-center"},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'adjust_stock':
            /* -------MASTERS - adjust_stock ----------------*/
            controller = base_url + 'index.php/adjust_stock/datatable';
            columnDef = [
                {"data": "iId", "name": "iId"},
                {"data": "branch", "name": "branch"},
                {"data": "code", "name": "code"},
                {"data": "cat", "name": "cat"},
                {"data": "product", "name": "product"},
                {"data": "qty", "name": "qty"},
                {"data": "adjust_type_str", "name": "adjust_type_str"},
                {"data": "rem", "name": "rem"},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'transfer_stock':
            /* -------MASTERS - transfer_stock ----------------*/
            controller = base_url + 'index.php/transfer_stock/datatable';
            columnDef = [
                {"data": "tId", "name": "tId"},
                {"data": "bfrom", "name": "bfrom"},
                {"data": "bto", "name": "bto"},
                {"data": "product", "name": "product"},
                {"data": "qty", "name": "qty"},
                {"data": "status_str", "name": "status_str"},
                {"data": "Actions", "name": "", "sClass": "text-center"},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'payslip':
            /* -------MASTERS - payslip ----------------*/
            controller = base_url + 'index.php/payslip/datatable';
            columnDef = [
                {"data": "pId", "name": "pId"},
                {"data": "date", "name": "date"},
                {"data": "emp", "name": "emp"},
                {"data": "net", "name": "net", "sClass": "text-right"},
                {"data": "status_str", "name": "status_str"},
                {"data": "Actions", "name": "", "sClass": "text-center"},
            ];
            colSort = [[0, "desc"]];
            break;

        case 'expense':
            /* -------MASTERS - expense ----------------*/
            controller = base_url + 'index.php/expense/datatable';
            columnDef = [
                {"data": "eId", "name": "eId"},
                {"data": "date", "name": "date"},
                {"data": "emp", "name": "emp"},
                {"data": "net", "name": "net", "sClass": "text-right"},
                {"data": "type", "name": "type"},
                {"data": "status_str", "name": "status_str"},
                {"data": "Actions", "name": "", "sClass": "text-center"},
            ];
            colSort = [[0, "desc"]];
            break;

        default:
            break;
    }

    var responsiveHelper_dt_basic = undefined;
    var breakpointDefinition = {
        tablet: 1024,
        phone: 480
    };
    var top_pagination = '';
    if ($('table.items').length) {
        top_pagination = 'p';
    }

    var oTable = $('#dt_basic').dataTable({
        "sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'f><'col-sm-6 col-xs-12 hidden-xs'l>r" + top_pagination + ">" +
                "t" +
                "<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
        /*"sDom": '<"top"flp<"clear">>rt<"bottom"ifp<"clear">>',
         "sDom": '<"top"<"actions">lfp<"clear">><"clear">rt<"bottom">',*/
        "autoWidth": true,
        'iDisplayLength': 10,
        "preDrawCallback": function () {
            // Initialize the responsive datatables helper once.
            if (!responsiveHelper_dt_basic) {
                responsiveHelper_dt_basic = new ResponsiveDatatablesHelper($('#dt_basic'), breakpointDefinition);
            }
        },
        "rowCallback": function (nRow) {
            responsiveHelper_dt_basic.createExpandIcon(nRow);
        },
        "drawCallback": function (oSettings) {
            responsiveHelper_dt_basic.respond();
        },
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": controller,
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "iDisplayStart ": 20,
        "order": colSort,
        "lengthMenu": [[10,25, 50, 75, 100], [10,25, 50, 75, 100]],
        "columns": columnDef,
        "oLanguage": {
            "sProcessing": "<img src='" + base_url + "assets/images/st-13.gif'>"
        },
        "fnInitComplete": function () {
            //oTable.fnAdjustColumnSizing();
        },
        "fnRowCallback": function (nRow, aData, iDisplayIndex) {
            var oSettings = oTable.fnSettings();
            $("td:first", nRow).html(oSettings._iDisplayStart + iDisplayIndex + 1);
            $($(nRow).find('td')).each(function () {
                if ($(this).html() == '')
                    $(this).html("N/A");
            })
            return nRow;
        },
        'fnServerData': function (sSource, aoData, fnCallback) {
            $.ajax({
                'dataType': 'json',
                'type': 'POST',
                'url': sSource,
                'data': aoData,
                'success': fnCallback
            });
        }
    });
    $('select[name="dt_basic_length"]').addClass('form-control');
    $('input[type="search"]').addClass('form-control');
    $('.fg-button').removeClass('ui-button');
});

