$(function () {
    zjjlTableInit();
    $("#searchBtn").click(function () {
        var zjksrq = $("#zjksrq").val();
        var zjjsrq = $("#zjjsrq").val();
        var zjr = $('#zjr option:selected').val();
        var tsr = $('#tsr option:selected').val();
        var zjzt = $('#zjzt option:selected').val();
        var zjbhg = "";
        if ($('#zjbhg').prop('checked')) {
            zjbhg = "1";
        }

        var Url = "";
        var table = ""
        var selTab = $("#tabid").val();
        if (selTab == "rwtj") {
            Url = configUrl + "/bdczj/queryZjtjxx"
            table = "bdczjtj-grid-table";
        } else if (selTab == "zjlb") {
            Url = configUrl + "/bdczj/queryZjjlxx";
            table = "bdczjjl-grid-table";
        }

        tableReload(table, Url, {
            zjksrq: zjksrq,
            zjjsrq: zjjsrq,
            zjr: zjr,
            tsr: tsr,
            zjzt: zjzt,
            zjbhg: zjbhg
        });
    });

    $("#exportExcel").click(function () {
        var zjksrq = $("#zjksrq").val();
        var zjjsrq = $("#zjjsrq").val();
        var zjr = $('#zjr option:selected').val();
        var zjbhg = "";
        if ($('#zjbhg').prop('checked')) {
            zjbhg = "1";
        }
        var Url;
        var selTab = $("#tabid").val();
        if (selTab == "rwtj") {
            Url = configUrl + "/bdczj/exportZjtjxx?zjksrq=" + zjksrq + "&zjjsrq=" + zjjsrq + "&zjr=" + zjr + "&zjbhg=" + zjbhg;
            table = "bdczjtj-grid-table";
        } else if (selTab == "zjlb") {
            Url = configUrl + "/bdczj/exportZjjlxx?zjksrq=" + zjksrq + "&zjjsrq=" + zjjsrq + "&zjr=" + zjr + "&zjbhg=" + zjbhg;
        }
        location.href = Url
    });


    //时间控件
    $('.date-picker').datepicker({
        autoclose: true,
        todayHighlight: true,
        language: 'zh-CN'
    }).next().on(ace.click_event, function () {
        $(this).prev().focus();
    });

});


function openTask(wiid) {

    $.ajax({
        type: "GET",
        url: configUrl + '/bdczj/getTaskid?wiid=' + wiid,
        dataType: "json",
        success: function (data) {
            if (data.taskid != null && data.taskid != "") {
                var url = portalUrl + '/taskHandle?taskid=' + data.taskid;
                openWin(url, '_task');
            } else {

            }
        },
        error: function () {
        }
    });

}

function ckxq(wtms) {
    if (wtms == null || wtms == '' || wtms == undefined || wtms == "undefined") {
        wtms = '';
    }
    tipInfo(wtms)
}

//jqGrid初始化
function zjjlTableInit() {
    var grid_selector = "#bdczjjl-grid-table";
    var pager_selector = "#bdczjjl-grid-pager";
    $(window).on('resize.jqGrid', function () {
        $(grid_selector).jqGrid('setGridWidth', $(".page-content").width());
    });

    var parent_column = $(grid_selector).closest('[class*="col-"]');
    $(document).on('settings.ace.jqGrid', function (ev, event_name, collapsed) {
        if (event_name === 'sidebar_collapsed' || event_name === 'main_container_fixed') {
            $(grid_selector).jqGrid('setGridWidth', parent_column.width());
        }
    });

    jQuery(grid_selector).jqGrid({
        datatype: "json",
        height: 'auto',
        jsonReader: {id: 'WIID'},
        colNames: ['受理编号', '业务类型', '创建人', '审批人', '推送人', '质检人', '质检开始日期', '质检结束日期', '质检状态', '质检结果', 'WIID', 'WTMS', '操作'],
        colModel: [{
            name: 'XMBH',
            index: 'XMBH',
            width: '10%',
            sortable: false,
            formatter: function (cellvalue, options, rowObject) {
                return '<a style="font-size: 12px;" href=\"javascript:openTask(\'' + rowObject.WIID + '\')\">' + cellvalue + '</a>';
            }
        },
            {name: 'SQLXMC', index: 'SQLXMC', width: '10%', sortable: false},
            {name: 'CJR', index: 'CJR', width: '10%', sortable: false},
            {name: 'SPR', index: 'SPR', width: '10%', sortable: false},
            {name: 'TSR', index: 'TSR', width: '10%', sortable: false},
            {name: 'ZJR', index: 'ZJR', width: '10%', sortable: false},
            {
                name: 'ZJKSRQ',
                index: 'ZJKSRQ',
                width: '10%',
                sortable: false,
                formatter: function (cellvalue, options, rowObject) {
                    if (!cellvalue) {
                        return "";
                    }
                    var data = new Date(cellvalue).Format("yyyy-MM-dd");
                    return data;
                }
            },
            {
                name: 'ZJJSRQ',
                index: 'ZJJSRQ',
                width: '10%',
                sortable: false,
                formatter: function (cellvalue, options, rowObject) {
                    if (!cellvalue) {
                        return "";
                    }
                    var data = new Date(cellvalue).Format("yyyy-MM-dd");
                    return data;
                }
            },

            {
                name: 'ZJZT',
                index: 'ZJZT',
                width: '5%',
                sortable: false,
                formatter: function (cellvalue, options, rowObject) {
                    if (cellvalue == 2) {
                        return "退回";
                    } else if (cellvalue == 1) {
                        return "通过";
                    }
                    return "质检中";
                }
            },
            {name: 'ZJBHG', index: 'ZJBHG', width: '5%', sortable: false},
            {name: 'WIID', index: 'WIID', width: '0%', sortable: false, hidden: true},
            {name: 'WTMS', index: 'WTMS', width: '0%', sortable: false, hidden: true},
            {
                name: 'mydy',
                index: '',
                width: '5%',
                sortable: false,
                formatter: function (cellvalue, options, rowObject) {
                    return '<div title="详情"  style="float:left;margin-left:8px;cursor:pointer;" class="ui-pg-div ui-inline-edit" id="handle" name="handle" onclick="ckxq(\'' + rowObject.WTMS + '\')" onmouseover="jQuery(this).addClass(\'ui-state-hover\');" onmouseout="jQuery(this).removeClass(\'ui-state-hover\');"><a>详情</a></div>'
                }
            }
        ],
        viewrecords: true,
        rowNum: 10,
        rowList: [10, 20, 30],
        pager: pager_selector,
        // pagerpos: "left",
        altRows: false,
        multiboxonly: false,
        rownumbers: true,
        rownumWidth: 50,
        // multiselect: true,
        loadComplete: function () {
            var table = this;
            setTimeout(function () {
                updatePagerIcons(table);
                enableTooltips(table);
                var replacement =
                    {
                        'ui-icon ui-icon-plus': 'ace-icon fa fa-plus 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]);
                })
            }, 0);

            var jqData = $(grid_selector).jqGrid("getRowData");
            $.each(jqData, function (index, data) {
                debugger;
                console.dir("data.ZJZT:" + data.ZJZT);
                var cellVal = " ";
                if (data.ZJZT == '质检中') {
                    cellVal = ' ';
                } else {
                    if (data.ZJBHG == 1) {
                        cellVal = '不合格';
                    } else {
                        cellVal = '合格';
                    }
                }
                console.dir("cellVal:" + cellVal);
                $(grid_selector).setCell(data.WIID, "ZJBHG", cellVal);
            })
        },
        onSelectRow: function (rowid, status) {

        },
        onSelectAll: function (rowids, status) {

        },
        editurl: "",
        caption: "",
        autowidth: true
    });
}

//jqGrid初始化
function zjjlTjTableInit() {
    var grid_selector = "#bdczjtj-grid-table";
    var pager_selector = "#bdczjtj-grid-pager";
    $(window).on('resize.jqGrid', function () {
        $(grid_selector).jqGrid('setGridWidth', $(".page-content").width());
    });

    var parent_column = $(grid_selector).closest('[class*="col-"]');
    $(document).on('settings.ace.jqGrid', function (ev, event_name, collapsed) {
        if (event_name === 'sidebar_collapsed' || event_name === 'main_container_fixed') {
            $(grid_selector).jqGrid('setGridWidth', parent_column.width());
        }
    });

    jQuery(grid_selector).jqGrid({
        datatype: "json",
        height: 'auto',
        jsonReader: {id: 'PRO_ID'},
        colNames: ['质检人', '质检数量'],
        colModel: [
            {name: 'ZJR', index: 'ZJR', width: '50%', sortable: false},
            {name: 'ZJSL', index: 'ZJSL', width: '50%', sortable: false}
        ],
        viewrecords: true,
        rowNum: 10,
        rowList: [10, 20, 30],
        pager: pager_selector,
        // pagerpos: "left",
        altRows: false,
        multiboxonly: false,
        rownumbers: true,
        rownumWidth: 50,
        // multiselect: true,
        loadComplete: function () {
            var table = this;
            setTimeout(function () {
                updatePagerIcons(table);
                enableTooltips(table);
                var replacement =
                    {
                        'ui-icon ui-icon-plus': 'ace-icon fa fa-plus 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]);
                })
            }, 0);

            var jqData = $(grid_selector).jqGrid("getRowData");
            $.each(jqData, function (index, data) {

            })
        },
        onSelectRow: function (rowid, status) {

        },
        onSelectAll: function (rowids, status) {

        },
        editurl: "",
        caption: "",
        autowidth: true
    });
}
