function enableTooltips(table) {
    $('.navtable .ui-pg-button').tooltip({
        container : 'body'
    });
    $(table).find('.ui-pg-div').tooltip({
        container : 'body'
    });
}

function initLawALGrid(grid_selector, pager_selector, url, width){
    var grid = jQuery(grid_selector).jqGrid({
        subGrid : false,
        url : url,
        datatype : "json",
        height : '100%',
        width : width,
        colNames : ['今日提交数', '今日付款数','今日提交保费','今日付款保费','本月提交数','本月提交订单的付款数'/*,'本月订单成交率','本月提交保费','本月提交订单的付款保费','本月保费成交率'*/],
        colModel : [
            { name : 'daySubmissionNum',
            index : 'daySubmissionNum',
            width : 150,
            editable : false,
            sortable : false,
        },{	name : 'dayPayNum',
            index : 'dayPayNum',
            width : 150,
            search : false,
            editable : false,
            readonly : true,
            sortable : false,
        },{	name : 'daySubmissionBf',
            index : 'daySubmissionBf',
            width : 150,
            search : false,
            editable : false,
            sortable : false
        },{	name : 'dayPayBf',
            index : 'dayPayBf',
            width : 150,
            editable : false,
            sortable : false,
        },{	name : 'monthSubmissionNum',
            index : 'monthSubmissionNum',
            width : 150,
            editable : false,
            sortable : false,
        },{
            name : 'monthPayNum',
            index : 'monthPayNum',
            width : 180,
            sortable : false,
            editable : false
        }/*,{
            name : 'monthSubmissionRate',
            index : 'monthSubmissionRate',
            width : 150,
            editable : false,
            sortable : false,
        },{
            name : 'monthSubmissionBf',
            index : 'monthSubmissionBf',
            width : 150,
            editable : true,
            sortable : false,
        }, {
            name : 'monthPayBf',
            index : 'monthPayBf',
            width : 190,
            sortable : false,
            editable : false
        }, {
            name : 'monthBfRate',
            index : 'monthBfRate',
            width : 150,
            sortable : false,
            editable : false
        }*/],
        //scroll : 1, // set the scroll property to 1 to enable paging with scrollbar - virtual loading of records
        sortname : "",
        sortorder : "",
        viewrecords : true,
        rowNum : 10,
        rowList : [ 100, 500, 1000 ],
        altRows : true,
        loadComplete : function() {
            var table = this;
            setTimeout(function(){
                styleCheckbox(table);
                updateActionIcons(table);
                updatePagerIcons(table);
                enableTooltips(table);
            }, 0);
        }
        //caption : "用户管理列表",
        //autowidth : true,
        /**
         grouping : true,
         groupingView : {
			 groupField : ['name'],
			 groupDataSorted : true,
			 plusicon : 'fa fa-chevron-down bigger-110',
			 minusicon : 'fa fa-chevron-up bigger-110'
		},
         */
    });
    return grid;
}

function initLawALGrid11(grid_selector, pager_selector, url, width){
    var grid = jQuery(grid_selector).jqGrid({
        subGrid : false,
        url : url,
        datatype : "json",
        height : '100%',
        width : width,
        colNames : [/*'今日提交数', '今日付款数','今日提交保费','今日付款保费','本月提交数','本月提交订单的付款数',*/'本月订单成交率','本月提交保费','本月提交订单的付款保费','本月保费成交率'],
        colModel : [
            /*{ name : 'daySubmissionNum',
                index : 'daySubmissionNum',
                width : 150,
                editable : false,
                sortable : false,
            },{	name : 'dayPayNum',
                index : 'dayPayNum',
                width : 150,
                search : false,
                editable : false,
                readonly : true,
                sortable : false,
            },{	name : 'daySubmissionBf',
                index : 'daySubmissionBf',
                width : 150,
                search : false,
                editable : false,
                sortable : false
            },{	name : 'dayPayBf',
                index : 'dayPayBf',
                width : 150,
                editable : false,
                sortable : false,
            },{	name : 'monthSubmissionNum',
                index : 'monthSubmissionNum',
                width : 150,
                editable : false,
                sortable : false,
            },{
                name : 'monthPayNum',
                index : 'monthPayNum',
                width : 180,
                sortable : false,
                editable : false
            },*/{
                name : 'monthSubmissionRate',
                index : 'monthSubmissionRate',
                width : 150,
                editable : false,
                sortable : false,
            },{
                name : 'monthSubmissionBf',
                index : 'monthSubmissionBf',
                width : 150,
                editable : true,
                sortable : false,
            }, {
                name : 'monthPayBf',
                index : 'monthPayBf',
                width : 190,
                sortable : false,
                editable : false
            }, {
                name : 'monthBfRate',
                index : 'monthBfRate',
                width : 150,
                sortable : false,
                editable : false
            }],
        //scroll : 1, // set the scroll property to 1 to enable paging with scrollbar - virtual loading of records
        sortname : "",
        sortorder : "",
        viewrecords : true,
        rowNum : 10,
        rowList : [ 100, 500, 1000 ],
        altRows : true,
        loadComplete : function() {
            var table = this;
            setTimeout(function(){
                styleCheckbox(table);
                updateActionIcons(table);
                updatePagerIcons(table);
                enableTooltips(table);
            }, 0);
        }
        //caption : "用户管理列表",
        //autowidth : true,
        /**
         grouping : true,
         groupingView : {
			 groupField : ['name'],
			 groupDataSorted : true,
			 plusicon : 'fa fa-chevron-down bigger-110',
			 minusicon : 'fa fa-chevron-up bigger-110'
		},
         */
    });
    return grid;
}

function initLawALGrid2(grid_selector, pager_selector, url, width){
    var grid = jQuery(grid_selector).jqGrid({
        subGrid : false,
        url : url,
        datatype : "json",
        height : '100%',
        width : width,
        colNames : ['提交数','提交订单的付款数','订单成交率','提交保费','提交订单的付款保费','保费成交率'],
        colModel : [
            { name : 'commitNum',
                index : 'commitNum',
                width : 150,
                editable : false,
                sortable : false,
            },{	name : 'payNum',
                index : 'payNum',
                width : 150,
                search : false,
                editable : false,
                readonly : true,
                sortable : false,
            },{	name : 'dealRate',
                index : 'dealRate',
                width : 150,
                search : false,
                editable : false,
                sortable : false
            },{	name : 'commitBf',
                index : 'commitBf',
                width : 150,
                editable : false,
                sortable : false,
            },{	name : 'payBf',
                index : 'payBf',
                width : 150,
                editable : false,
                sortable : false,
            },{
                name : 'bfDealRate',
                index : 'bfDealRate',
                width : 180,
                sortable : false,
                editable : false
            }],
        //scroll : 1, // set the scroll property to 1 to enable paging with scrollbar - virtual loading of records
        sortname : "",
        sortorder : "",
        viewrecords : true,
        rowNum : 10,
        rowList : [ 100, 500, 1000 ],
        altRows : true,
        loadComplete : function() {
            var table = this;
            setTimeout(function(){
                styleCheckbox(table);
                updateActionIcons(table);
                updatePagerIcons(table);
                enableTooltips(table);
            }, 0);
        }
        //caption : "用户管理列表",
        //autowidth : true,
        /**
         grouping : true,
         groupingView : {
			 groupField : ['name'],
			 groupDataSorted : true,
			 plusicon : 'fa fa-chevron-down bigger-110',
			 minusicon : 'fa fa-chevron-up bigger-110'
		},
         */
    });
    return grid;
}

function initNavButtons(grid_selector, pager_selector, contextPath){
    // navButtons
    jQuery(grid_selector).jqGrid('navGrid', pager_selector,  { // navbar options
        edit : false,
        editicon : 'ace-icon fa fa-pencil blue',
        add : false,
        addicon : 'ace-icon fa fa-plus-circle purple',
        del : false,
        delicon : 'ace-icon fa fa-trash-o red',
        search : false,
        searchicon : 'ace-icon fa fa-search orange',
        refresh : true,
        refreshicon : 'ace-icon fa fa-refresh blue',
        view : false,
        viewicon : 'ace-icon fa fa-search-plus grey'
    }, {
        // edit record form
        // closeAfterEdit: true,
        // width: 700,
        recreateForm : true,
        beforeShowForm : function(e) {
            var form = $(e[0]);
            form.closest('.ui-jqdialog').find('.ui-jqdialog-titlebar').wrapInner('<div class="widget-header" />')
            style_edit_form(form);
        },
        errorTextFormat: function (response) {
            var result = eval('('+response.responseText+')');
            return result.message;
        }
    });

    jQuery(grid_selector).jqGrid('navButtonAdd', pager_selector,{
        caption : "",
        title : "导出Excel",
        buttonicon : "ace-icon fa fa-file-excel-o green",
        onClickButton : function () {
            var keys = [], ii = 0, rows = "";
            var ids = $(grid_selector).getDataIDs(); // Get All IDs
            var row = $(grid_selector).getRowData(ids[0]); // Get First row to get the labels
            var label = $(grid_selector).jqGrid('getGridParam','colNames');
            for(var k in label){
                rows = rows + label[k] + "\t"; // output each Column as tab delimited
            }
            for (var k in row) {
                keys[ii++] = k; // capture col names
            }
            rows = rows + "\n"; // Output header with end of line
            var tIds = {"orderId":true,"createDate":true,"real_name":false,"idCard":true,"phone":true,"typeO":false,"lowType":false,
                "amount":false,"tax":false,"income":false,"points":false,"money":false,"taxhj":false,"gs":false,"remark":false};
            for (var i = 0; i < ids.length; i++) {
                row = $(grid_selector).getRowData(ids[i]); // get each row
                for (var j = 0; j < keys.length; j++){
                    if(tIds[keys[j]]){
                        rows = rows + '="' + row[keys[j]] + '"\t'; // output each Row as tab delimited
                    }else{
                        rows = rows + row[keys[j]] + '\t'; // output each Row as tab delimited
                    }
                }
                rows = rows + "\n"; // output each row with end of line
            }
            rows = rows + "\n"; // end of line at the end
            var form = "<form name='csvexportform' action='" + contextPath + "/fs/report/getExcel?fileName=lawyerAccountLow' method='post'>";
            form = form + "<input type='hidden' name='csvBuffer' value='" + encodeURIComponent(rows) + "'>";
            form = form + "</form><script>document.csvexportform.submit();</sc" + "ript>";
            OpenWindow = window.open('', '');
            OpenWindow.document.write(form);
            OpenWindow.document.close();
        }
    });
}

// it causes some flicker when reloading or navigating grid
// it may be possible to have some custom formatter to do this as the grid is being created to prevent this
// or go back to default browser checkbox styles for the grid
function styleCheckbox(table) {
    /**
     * $(table).find('input:checkbox').addClass('ace') .wrap('<label />') .after('<span class="lbl align-top" />') $('.ui-jqgrid-labels th[id*="_cb"]:first-child')
     * .find('input.cbox[type=checkbox]').addClass('ace') .wrap('<label />').after('<span class="lbl align-top" />');
     */
}

// unlike navButtons icons, action icons in rows seem to be hard-coded
// you can change them like this in here if you want
function updateActionIcons(table) {
    /**
     * var replacement = { 'ui-ace-icon fa fa-pencil' : 'ace-icon fa fa-pencil blue', 'ui-ace-icon fa fa-trash-o' : 'ace-icon fa fa-trash-o red', 'ui-icon-disk' : 'ace-icon fa fa-check green', 'ui-icon-cancel' :
	 * 'ace-icon fa fa-times red' }; $(table).find('.ui-pg-div span.ui-icon').each(function(){ var icon = $(this); var $class = $.trim(icon.attr('class').replace('ui-icon', '')); if($class in replacement)
	 * icon.attr('class', 'ui-icon '+replacement[$class]); })
     */
}

// replace icons with FontAwesome icons like above
function updatePagerIcons(table) {
    var replacement = {
        'ui-icon-seek-first' : 'ace-icon fa fa-angle-double-left bigger-140',
        'ui-icon-seek-prev' : 'ace-icon fa fa-angle-left bigger-140',
        'ui-icon-seek-next' : 'ace-icon fa fa-angle-right bigger-140',
        'ui-icon-seek-end' : 'ace-icon fa fa-angle-double-right bigger-140'
    };
    $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function() {
        var icon = $(this);
        var $class = $.trim(icon.attr('class').replace('ui-icon', ''));

        if ($class in replacement)
            icon.attr('class', 'ui-icon ' + replacement[$class]);
    })
}