var gridManager = null, refreshFlag = false, _roleId = null, purchaseSelectedList = [];
$(document).ready(function () {
        initUI();
        initGrid();

    }
);

function initUI() {
    UICtrl.initDefaultLayout();
}

function initGrid() {
    var toolbarOptions = UICtrl.getDefaultToolbarOptions({
       // addHandler: addHandler
        addProduceHandler: {id: 'produceException', text: '添加', img: 'fa-music', className: 'btn-gray',
            click: function () {
                UICtrl.addTabItem({
                    tabid: 'addExceptionReportProduce',
                    text: '新增生产异常报告',
                    url: web_app.name + '/exceptionReport/addExceptionReportDetail.job?billType=produce'
                });
            }
        }
       /* addPurchaseHandler: {id: 'purchaseException', text: '添加采购异常单', img: 'fa-building-o', className: 'btn-gray',
            click: function () {
                UICtrl.addTabItem({
                    tabid: 'addExceptionReportPurchase',
                    text: '新增采购异常报告',
                    url: web_app.name + '/exceptionReport/addExceptionReportDetail.job?billType=purchase'
                });
            }
        },
        addSupplierHandler: {id: 'supplierException', text: '添加供应商异常单', img: 'fa-book', className: 'btn-gray',
            click: function () {
                UICtrl.addTabItem({
                    tabid: 'addExceptionReportSupplier',
                    text: '新增供应商异常报告',
                    url: web_app.name + '/exceptionReport/addExceptionReportDetail.job?billType=supplier'
                });
            }
        }*/
    });
    gridManager = UICtrl.grid('#maingrid', {
            columns: [
                {display: "状态", name: "statusTextView", width: "60", align: "left", type: "string"}
                , {display: "id", name: "id", width: "100", align: "left", type: "string", hide: true}
                , {display: "报告编号", name: "billCode", width: 150, minWidth: 60, type: "string", align: "center"}
                , {display: "异常类型", name: "exceptionTypeTextView", width: 80, minWidth: 60, type: "string", align: "center"}
               /*, {display: "单据类型", name: "billType", width: 80, minWidth: 60, type: "string", align: "center",
                    render: function (item) {
                       if (item.billType=="supplier"){
                            return "供应商";
                        } else  if (item.billType=="purchase"){
                            return "采购";
                       } else  if (item.billType=="outgoing"){
                           return " 外发";
                        } else {
                            return "生产";
                        }
                    }
                } */
                , {display: "异常原因分类", name: "exceptionReasonTypeTextView", width: 100, minWidth: 60, type: "string", align: "center"}

                , {display: "呈报日期", name: "reportDate", width: 100, minWidth: 60, type: "date", align: "center"}
                , {display: "异常日期", name: "exceptionDate", width: 100, minWidth: 60, type: "date", align: "center"}
               // , {display: "异常标题", name: "exceptionTitle", width: 200, minWidth: 60, type: "date", align: "center"}
               // , {display: "客户编号", name: "customerName", width: 200, minWidth: 60, type: "date", align: "center"}
                , {display: "客户名称", name: "customerName", width: 200, minWidth: 60, type: "string", align: "center"}
                , {display: "客户单号", name: "customerOrderNo", width: 180, minWidth: 60, type: "string", align: "center"}
                , {display: "紧急程度", name: "urgencyTextView", width: 80, minWidth: 60, type: "string", align: "center"}
                , {display: "改善措施", name: "dealMethod", width: 200, minWidth: 60, type: "string", align: "center"}
                , {display: "异常原因", name: "exceptionReason", width: 200, minWidth: 60, type: "string", align: "center"}
                , {display: "责任部门", name: "dutyDeptName", width: 120, minWidth: 60, type: "string", align: "center"}
                , {display: "责任人", name: "checkOperatorName", width: 100, minWidth: 60, type: "string", align: "center"}
                , {display: "处理结果", name: "dealResult", width: 200, minWidth: 60, type: "string", align: "center"}
                //, {display: "备注", name: "remark", width: 200, minWidth: 60, type: "date", align: "center"}
                , {display: "创建日期", name: "fillinDate", width: 150, minWidth: 60, type: "dateTime", align: "center"}
                , {display: "创建部门", name: "deptName", width: 120, minWidth: 60, type: "string", align: "center"}
                , {display: "创建人", name: "personMemberName", width: 100, minWidth: 60, type: "string", align: "center"}
            ],
            dataAction: 'server',
            url: web_app.name + '/exceptionReport/slicedExceptionReportList.ajax?',
            //parms: {requireItemId: $("#prepareItemId").val()},
            toolbar: toolbarOptions,
            width: '99.8%',
            height: '100%',
            pageSize: 100,
            heightDiff: -4,
            //sortName: 'lastModifiedDate',
            //sortOrder: 'desc',
            checkbox: true,
            usePager: true,
            rownumbers: true,
            enabledEdit: false,
            fixedCellHeight: true,
            selectRowButtonOnly: true,
            enableObjectBag: true,
        allowAdjustColWidth: true,
            onSuccess: function (data) {
                if (data.error) {
                    Public.tip(data.error);
                }
            }
            ,
            onDblClickRow: function (data, rowindex, rowobj) {
                viewHandler(data.id);
            },
            onAfterShowData: function (currentData) {

            }
        }
    );
    UICtrl.setSearchAreaToggle(gridManager);
}


/**
 * 查看品质异常报告详情
 * @param id 品质异常报告id
 * @returns {boolean}
 */
function viewHandler(id) {
    if (!id) {
        var row = DataUtil.getSelectedRow(gridManager);
        if (!row) {
            return row;
        }
        id = row.id;
    }
    UICtrl.addTabItem({
        tabid: 'viewExceptionReport' + id,
        text: '查看品质异常报告',
        url: web_app.name + '/exceptionReport/showExceptionReportDetail.job?isReadOnly=true&bizId=' + id
    });
}

//添加按钮
function addHandler() {
    UICtrl.showAjaxDialog({
        title: '新增',
        width: 400,
        url: web_app.name + '/exceptionReport/chooseExceptionDetail.load',
        ok: insertHandler
    });
}

function insertHandler() {
    var _self = this;
    var billType = $("#billType").val();
    var title="采购/外发";
    if (billType=="produce"){
        title="生产";
    }
    if (billType==""){
         Public.errorTip("请选择异常单类型！");
        return false;
    }
    UICtrl.addTabItem({
        tabid: 'addException'+billType,
        text: '新增异常单('+title+")",
        url: web_app.name + '/exceptionReport/addExceptionReportDetail.job?billType='+billType
    });
    _self.close();
}

function deleteHandler() {
    var rows = DataUtil.getObjectBagData("maingrid");
    if (!rows || rows.length < 1) {
        Public.tip('common.warning.nochoose');
        return;
    }
    var ids = [];
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        if (row.status != 0) {
            Public.errorTip('只能删除草稿状态的数据!');
            return;
        }
        ids.push(row.id);
    }
    if (!ids) {
        return;
    }
    Public.ajax(web_app.name + '/exceptionReport/deleteExceptionReport.ajax',
        {ids: $.toJSON(ids)},
        function () {
            reloadGrid();
        });
}

function reloadGrid() {
    var params = $("#queryMainForm").formToJSON();
    UICtrl.gridSearch(gridManager, params);
}

function query(obj) {
    var param = $(obj).formToJSON();
    UICtrl.gridSearch(gridManager, param);
}

function resetForm(obj) {
    $(obj).formClean();
}

