var clientGridManager,produceGridManager, purchaseGridManager, dutyPersonGridManager,exceptionClientManager = null;var checkbox = true;
$(function () {
    initialize();
    initSerachBox();
    initProduceGrid();
    initClientGrid();
    initPurchaseGrid();
    initDutyPersonGridGrid();
});

function initialize() {
    var mainFileTable = $('#exceptionStateInfoList').fileList();
    mainFileTable.find('table').css({
        borderTopWidth: 0
    });
    $('#exceptionStateInfoList').fileList({
        bizId: $("#attbizId").val()
    });
    $("#dutyDeptName").orgTree({
        filter: "dpt", excludePos: 0, param: {orgKindId: "ogn,dpt"},
        back: {
            text: "#dutyDeptName",
            value: '#dutyDept'
        },
        //manageType: 'taskQuery',
        onChange: function (value, data) {
            $('#dutyDept').val(data.id);
        }
    });
    $("#checkOperatorName").orgTree({
        filter: "psm", excludePos: 0, param: {orgKindId: "ogn,dpt,pos,psm"},
        back: {
            text: "#checkOperatorName",
            value: '#checkOperator'
        },
        //manageType: 'taskQuery',
        onChange: function (value, data) {
            //  alert(data.id+data.name);  alert(data.deptId+data.deptName);
            $('#checkOperator').val(data.id);
            $('#dutyDeptName').val(data.deptName);
            $('#dutyDept').val(data.deptId);
        }
    });
    if ($('#exceptionReasonType').val() == "1") {
        showPurchaseDiv();
    } else if ($('#exceptionReasonType').val() == "2") {
        showProduceDiv();
    } else {
        showClientDiv();
    }
    $('#exceptionReasonType').combox({
        onChange: function (v) {
            $('#customerOrderNo').val("");
            $('#customerName').val("");
            $('#customerNo').val("");
            if (v.value == 1) { //供应商原料问题
                if ($('#exceptionType').val()=="in"){
                    Public.tip("供应商原料问题的异常类型只能是外部！");
                    clearExceptionType();
                    //return false;
                }
                showPurchaseDiv();
            } else {
                if ($('#exceptionType').val()=="out"){
                    Public.tip("生产和客户异常的异常类型只能是内部！");
                    clearExceptionType();
                   // return false;
                }
                if (v.value== "3"){
                    checkbox = true
                    initSerachBox();
                    showClientDiv();
                    UICtrl.setElRequiredFlag("#customerNo", true);
                } else {
                    checkbox = false
                    initSerachBox();
                    UICtrl.setElRequiredFlag("#customerNo", false);
                    showProduceDiv();
                }
                UICtrl.setElHideOrShow("#loseMoney",false);
            }
        }
    });
    $('#exceptionType').combox({
        onChange: function (v) {
            if (v.value == "in") { //内部
                if ($('#exceptionReasonType').val()==1){
                    Public.tip("供应商原料问题的异常类型只能是外部！");
                    clearExceptionType();
                     return false;
                }
            } else {
                if ($('#exceptionReasonType').val()!=""&&$('#exceptionReasonType').val()!=1){
                    Public.tip("生产和客户异常的异常类型只能是内部！");
                    clearExceptionType();
                    return false;
                }
            }
        }
    });
}

function clearExceptionType(){
    $('#exceptionType').val("");
    $('#exceptionType_text').val("");
}

function showProduceDiv() {

    $("#purchaseDiv").hide();
    $("#produceDiv").show();
    $("#producegridDiv").show();
    $("#clientgridDiv").hide();
    try {
        produceGridManager.loadData();
    } catch(e){
        // alert(e.error);
    }
    setPurchaseMustInput(false);
    setProduceMustInput(true);
}

function showClientDiv() {

    $("#purchaseDiv").hide();
    $("#produceDiv").show();
    $("#clientgridDiv").show();
    $("#producegridDiv").hide();
    try {
        clientGridManager.loadData();
    } catch(e){
        // alert(e.error);
    }
    setPurchaseMustInput(false);
    setProduceMustInput(true);
    UICtrl.setElHideOrShow("#loseMoney",false);
}

function showPurchaseDiv() {
    $("#produceDiv").hide();
   // var row = purchaseGridManager.rows;
   // for (var u = 0; u < row.length; u++) {
       // purchaseGridManager.deleteRow(purchaseGridManager.rows[u]);
   // }
    try {
        purchaseGridManager.loadData();
    } catch(e){
       // alert(e.error);
    }
    $("#purchaseDiv").show();
    setPurchaseMustInput(true);
    setProduceMustInput(false);
}

function setPurchaseMustInput(flag) {
    UICtrl.setElRequiredFlag("#customerNamePur", flag);
    UICtrl.setElRequiredFlag("#customerOrderNoPur", flag);
    UICtrl.setElRequiredFlag("#brandName", flag);

    UICtrl.setElHideOrShow("#loseMoney",true);

}

function setProduceMustInput(flag) {
   if ($('#exceptionReasonType').val() == "3"){
    UICtrl.setElRequiredFlag("#customerNo", flag);
   }
    UICtrl.setElRequiredFlag("#customerOrderNo", false);
    UICtrl.setElHideOrShow("#loseMoney",false);
}

function initSerachBox(){
    //工厂
    $('#werksName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "WERKS"}
        },
        onChange: function (value, data) {
            $('#werks').val(data.WERKS);
            $('#werksName').val(data.NAME1);
        }
    });
    $('input[name="payMoney"]').change(function () {
        if (isNaN(Number($('#payMoney').val()))) {
            Public.errorTip("赔付金额必须输入数字!");
            $('#payMoney').val("");
            countSumChange();
            return false;
        }
        countSumChange();
    })

    $('#customerOrderNo').searchbox({
        type: "system", name: "sapCustomerOrderSelect",
        checkbox:checkbox,
        checkboxIndex:"VBELN",
        textIndex:"NAME1",
        valueIndex:"VBELN",
        getParam: function (item) {
            if (Public.isBlank($('#werksName').val())) {
                Public.tip("请先选择工厂！")
                return false
            }
            if ($('#exceptionReasonType').val()=="3"&&Public.isBlank($('#customerNo').val())) {
                Public.tip("请先输入客户编号！")
                return false
            }
            return {
                werks: $('#werks').val(),
                kunnr: $('#customerNo').val(),
                reasonType:$('#exceptionReasonType').val()
            }
        },
        onChange: function (value, data) {
            if ($('#exceptionReasonType').val()=="3") { //客户异常
                //$('#customerName').val(data.NAME1);
                $('#customerName').val(value.text);
            }
            if ($('#exceptionReasonType').val()=="2"&&data.VBELN!=undefined) { //生产异常
                produceGridManager.addRows({
                    produceNo:data.VBELN,
                    productCode: data.MATNR,
                    productName: data.MAKTX,
                    unitName: data.MEINS,
                    number: data.PSMNG
                });
            }
            $('#customerOrderNo').val(value.value);
        }
    });
    //产品类别
    $('#productTypeName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "MATKL"}
        },
        onChange: function (value, data) {
            $('#productType').val(data.MATKL);
            $('#productTypeName').val(data.WGBEZ);
        }
    });
}

function initProduceGrid() {

    var produceToolbarOptions = UICtrl.getDefaultToolbarOptions({
        addHandler: function () {
            UICtrl.addGridRow(produceGridManager);
        },
        deleteHandler: function () {
            var _grid = UICtrl.getGridManager('#producegrid');
            DataUtil.delSelectedRows({
                action: 'exceptionReport/deleteExceptionReportItem.ajax',
                param: {},
                gridManager: _grid,
                idFieldName: 'id',
                onSuccess: function () {
                    countSum(produceGridManager.rows);
                    reloadGrid();
                }
            });
        },
    });
    produceGridManager = UICtrl.grid('#producegrid', {
        columns: getProcduceGridColumns(),
        dataAction: 'server',
        url: web_app.name + '/exceptionReport/slicedExceptionReportItemList.ajax',
        parms: {exceptionReportId: getId(),exceptionReasonType:"2"},
        height:'auto', // colRowNum*35+90
        heightDiff: -4,
        sortName: 'id',
        sortOrder: 'asc',
        checkbox: true,
        usePager: true,
        rownumbers: true,
        enabledEdit: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
        //autoApplyNextEditor:false,
        toolbar: produceToolbarOptions,
        onAfterEdit: function (item) {
            if (item.column.name == 'rmbMoney') {
                countSum(produceGridManager.rows);
                var money = MathUtil.add(item.record.money, item.value);
                produceGridManager.updateRow(item.record, {totalMoney:money });
            }
        },
        onAfterShowData() {
            countSum(produceGridManager.rows);
            for (var r = 0; r < produceGridManager.rows.length; r++) {
                var money = MathUtil.add(produceGridManager.rows[r].rmbMoney, produceGridManager.rows[r].money);
                produceGridManager.updateRow(produceGridManager.rows[r], {totalMoney:money });
            }
        }
    })
    UICtrl.setSearchAreaToggle(produceGridManager);
}

function initClientGrid() {
    produceToolbarOptions=null;
    var clientToolbarOptions = UICtrl.getDefaultToolbarOptions({
        addHandler: function () {
            //如果生产单号/客户单号 不为空，根据单号弹框查询
            var customerOrderNo = $("#customerOrderNo").val();
            if (Public.isNotBlank(customerOrderNo)){
                UICtrl.showAjaxDialog({
                    title: '新增',
                    width: 1200,
                    height: 400,
                    url: web_app.name + '/exceptionReport/chooseExceptionClientDetail.load',
                    param:{customerOrderNo:customerOrderNo},
                    init: function (){
                        initExceptionClient();
                    },
                    ok: function () {
                        var rows = exceptionClientManager.getSelectedRows();
                        for (var i = 0 ;i<rows.length;i++){
                            var row = rows[i];
                            var dates = clientGridManager.getData();
                            if (dates != null && dates.length > 0){
                                for (let j = 0; j < dates.length; j++) {
                                    var data = dates[i];
                                    if (data.produceNo == row.AUFNR){
                                        Public.tip("生产单号："+row.AUFNR+" 已选择，请取消勾选！");
                                        return false;
                                    }
                                }
                            }
                            clientGridManager.addRow({
                                produceNo: row.AUFNR,
                                productCode: row.MATNR,
                                productName: row.MAKTX,
                                number: row.PSMNG,
                                unitName: row.MEINS,
                                currencyName: row.KOEIN,
                                money: row.KWERT,
                                sendDate: row.LFDAT
                            });
                        }
                        this.close();
                    }
                })
            }else {
                UICtrl.addGridRow(clientGridManager);
            }
        },
        deleteHandler: function () {
            var _grid = UICtrl.getGridManager('#clientgrid');
            DataUtil.delSelectedRows({
                action: 'exceptionReport/deleteExceptionReportItem.ajax',
                param: {},
                gridManager: _grid,
                idFieldName: 'id',
                onSuccess: function () {
                    countSum(clientGridManager.rows);
                    reloadGrid();
                }
            });
        },
    });
    clientGridManager = UICtrl.grid('#clientgrid', {
        columns: getClientGridColumns(),
        dataAction: 'server',
        url: web_app.name + '/exceptionReport/slicedExceptionReportItemList.ajax',
        parms: {exceptionReportId: getId(),exceptionReasonType:"2"},
        height:'auto', // colRowNum*35+90
        heightDiff: -4,
        sortName: 'id',
        sortOrder: 'asc',
        checkbox: true,
        usePager: true,
        rownumbers: true,
        enabledEdit: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
        //autoApplyNextEditor:false,
        toolbar: clientToolbarOptions,
        onAfterEdit: function (item) {
            if (item.column.name == 'rmbMoney') {
                countSum(clientGridManager.rows);
                var money = MathUtil.add(item.record.money, item.value);
                clientGridManager.updateRow(item.record, {totalMoney:money });
            }
        },
        onAfterShowData() {
            countSum(clientGridManager.rows);
            for (var r = 0; r < clientGridManager.rows.length; r++) {
                var money = MathUtil.add(clientGridManager.rows[r].rmbMoney, clientGridManager.rows[r].money);
                clientGridManager.updateRow(clientGridManager.rows[r], {totalMoney:money });
            }
        }
    })
    UICtrl.setSearchAreaToggle(clientGridManager);
}

function initExceptionClient(){
    var toolbarOptions = UICtrl.getDefaultToolbarOptions({

    });
    exceptionClientManager = UICtrl.grid('#maingrid', {
        columns: getExceptionClientColumns(),
        dataAction: 'server',
        url: web_app.name + '/exceptionReport/slicedExceptionClientList.ajax',
        parms: {
            werks: $("#werks").val(),
            kunnr: $("#customerNo").val(),
            vbeln: $("#customerOrderNo").val(),
            reasonType: $('#exceptionReasonType').val()
        },
        height:'400', // colRowNum*35+90
        heightDiff: -4,
        sortName: 'id',
        sortOrder: 'asc',
        checkbox: true,
        usePager: true,
        rownumbers: true,
        enabledEdit: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
        //autoApplyNextEditor:false,
        toolbar: toolbarOptions,
    })
    UICtrl.setSearchAreaToggle(exceptionClientManager);
}

function getExceptionClientColumns(){
    var columns = [];
    columns.push({
        display: "客户单号", name: "BSTNK", width: "160", align: "left", type: "string",
    });
    columns.push({
        display: "生产单号", name: "AUFNR", width: "160", align: "left", type: "string",
    });
    columns.push(
        {display: "产品编码", name: "MATNR", width: "160", align: "left", type: "string"
        } );
    columns.push(
        {display: "产品名称", name: "MAKTX", width: "250", align: "left", type: "string"
        } );
    columns.push(
        {display: "数量", name: "PSMNG", width: "60", align: "left", type: "string"
        } );
    columns.push(
        {display: "单位", name: "MEINS", width: "60", align: "left", type: "string"
        } );
    columns.push(
        {display: "币别", name: "KOEIN", width: "60", align: "left", type: "string"
        } );
    columns.push(
        {display: "金额", name: "KWERT", width: "60", align: "left", type: "string"
        } );
    columns.push(
        {display: "出货日期", name: "LFDAT", width: "140", align: "left", type: "date"
        } );
    return columns;
}

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

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

function countSumChange() {
    if ($('#exceptionReasonType').val() == "1") {
        countSum(purchaseGridManager.rows);
    } else if ($('#exceptionReasonType').val() == "2") {
        countSum(produceGridManager.rows);
    } else {
        countSum(clientGridManager.rows);
    }
}

function getClientGridColumns() {
    var columns = [];
     columns.push({
            display: "生产单号", name: "produceNo", width: "160", align: "left", type: "string",
            editor: {
                required: false, type: "select",
                data: {
                    type: 'system',
                    name: "sapCustomerProduceNo",
                    getParam: function (item) {
                        if (Public.isBlank($('#customerOrderNo').val())) {
                            Public.tip("请先输入客户单号！")
                            return false;
                        }
                        return {
                            werks: $("#werks").val(),
                            kunnr: $("#customerNo").val(),
                            vbeln: $("#customerOrderNo").val(),
                            reasonType: $('#exceptionReasonType').val()
                        }
                    },
                    back: {
                        AUFNR: "produceNo",
                        MATNR: "productCode",
                        MAKTX: "productName",
                        PSMNG: "number",
                        MEINS: "unitName",
                        KOEIN: "currencyName",
                        KWERT: "money",
                        LFDAT: "sendDate"
                    }
                }
            }
        });

    columns.push(
        {display: "产品编码", name: "productCode", width: "160", align: "left", type: "string"
           ,editor: {type: 'text'}
        } );
    columns.push(
        {display: "产品名称", name: "productName", width: "200", align: "left", type: "string"
           ,editor: {type: 'text'}
        } );
    columns.push(
        {display: "数量", name: "number", width: "100", align: "left", type: "string"
            ,editor: {type: "text",mask: '9999999.999'}
        } );
    columns.push(
        {display: "单位", name: "unitName", width: "80", align: "left", type: "string"
          ,editor: {type: "text"}
        } );
    columns.push(
        {display: "币别", name: "currencyName", width: "100", align: "left", type: "string"
             ,//editor: {type: "text"}
            editor: {
                required: false, type: "select",
                data: {
                    type: 'system',
                    name: "dictionary",
                    getParam: function (item) {
                        return {fieldName: "WAERS"}
                    },
                    back: {
                        WAERS: "currency",
                        KTEXT: "currencyName",
                    }
                },
            }
        } );
    columns.push(
        {display: "金额", name: "money", width: "100", align: "left", type: "string"
         ,editor: {type: "text",mask: '9999999.999'}
        } );
    columns.push(
        {
            display: "折算金额", name: "rmbMoney", width: "100", align: "left", type: "string",
            editor: {type: "text", mask: '9999999.999'}
        } );
    columns.push(
        {display: "出货日期", name: "sendDate", width: "140", align: "left", type: "date"
         ,editor: {type: "date"}
        } );
    return columns;
}
function getProcduceGridColumns() {
    var columns = [];
    columns.push({
        display: "生产单号", name: "produceNo", width: "160", align: "left", type: "string",
        editor: {type: 'text'}
    });

    columns.push(
        {display: "产品编码", name: "productCode", width: "160", align: "left", type: "string"
            ,editor: {type: 'text'}
        },
        {display: "产品名称", name: "productName", width: "200", align: "left", type: "string"
            ,editor: {type: 'text'}
        },
        {display: "数量", name: "number", width: "100", align: "left", type: "string"
            ,editor: {type: "text",mask: '9999999.999'}
        },
        {display: "单位", name: "unitName", width: "80", align: "left", type: "string"
            ,editor: {type: "text"}
        },
        {display: "币别", name: "currencyName", width: "100", align: "left", type: "string",
            editor: {
                required: false, type: "select",
                data: {
                    type: 'system',
                    name: "dictionary",
                    getParam: function (item) {
                        return {fieldName: "WAERS"}
                    },
                    back: {
                        WAERS: "currency",
                        KTEXT: "currencyName",
                    }
                },
            }
            //,editor: {type: "text"}
        },
        {display: "金额", name: "money", width: "100", align: "left", type: "string"
            ,editor: {type: "text",mask: '9999999.999'}
        },
        {
            display: "折算金额", name: "rmbMoney", width: "100", align: "left", type: "string",
            editor: {type: "text", mask: '9999999.999'}
        },
        // {
        //     display: "总金额", name: "totalMoney", width: "100", align: "left", type: "string"
        //  },
        {display: "出货日期", name: "sendDate", width: "140", align: "left", type: "date"
            ,editor: {type: "date"}
        }
    );
    return columns;
}

function initDutyPersonGridGrid() {
    var toolbarOptions = UICtrl.getDefaultToolbarOptions({
        addHandler: function () {
            UICtrl.addGridRow(dutyPersonGridManager);
        },
        deleteHandler: function () {
            var _grid = UICtrl.getGridManager('#dutyPersonGrid');
            DataUtil.delSelectedRows({
                action: 'exceptionReport/deleteExceptionReportDutyPerson.ajax',
                param: {},
                gridManager: _grid,
                idFieldName: 'id',
                onSuccess: function () {
                    reloadGrid();
                }
            });
        },
    });
    dutyPersonGridManager = UICtrl.grid('#dutyPersonGrid', {
            columns: getDutyPersonGridColumns(),
            dataAction: 'server',
            url: web_app.name + '/exceptionReport/slicedExceptionReportDutyPersonList.ajax',
            parms: {exceptionReportId: getId()},
            height: 'auto',
            heightDiff: -4,
            sortName: 'id',
            sortOrder: 'asc',
            checkbox: true,
            usePager: true,
            rownumbers: true,
            enabledEdit: true,
            fixedCellHeight: true,
            selectRowButtonOnly: true,
            //autoApplyNextEditor:false,
            toolbar: toolbarOptions,
        }
    );
    UICtrl.setSearchAreaToggle(dutyPersonGridManager);
}
/*
function initPurchaseToolBar(){
     purchaseToolbarOptions = UICtrl.getDefaultToolbarOptions({
        addHandler: function () {
            UICtrl.addGridRow(purchaseGridManager);
        },
        deleteHandler: function () {
            var _grid = UICtrl.getGridManager('#purchasegrid');
            DataUtil.delSelectedRows({
                action: 'exceptionReport/deleteExceptionReportItem.ajax',
                param: {},
                gridManager: _grid,
                idFieldName: 'id',
                onSuccess: function () {
                    countSum(purchaseGridManager.rows);
                    reloadGrid();
                }
            });
        },
    });
} */

function initPurchaseGrid() {
   var purchaseToolbarOptions = UICtrl.getDefaultToolbarOptions({
        addHandler: function () {
            UICtrl.addGridRow(purchaseGridManager);
        },
        deleteHandler: function () {
            var _grid = UICtrl.getGridManager('#purchasegrid');
            DataUtil.delSelectedRows({
                action: 'exceptionReport/deleteExceptionReportItem.ajax',
                param: {},
                gridManager: _grid,
                idFieldName: 'id',
                onSuccess: function () {
                    countSum(purchaseGridManager.rows);
                    reloadGrid();
                }
            });
        },
    });

    purchaseGridManager = UICtrl.grid('#purchasegrid', {
        columns: getPurchaseGridColumns(),
        dataAction: 'server',
        url: web_app.name + '/exceptionReport/slicedExceptionReportItemList.ajax',
        parms: {exceptionReportId: getId(),exceptionReasonType:"1"},
        height: 'auto',
        heightDiff: -2,
        sortName: 'id',
        sortOrder: 'asc',
        checkbox: true,
        usePager: true,
        rownumbers: true,
        enabledEdit: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
        //autoApplyNextEditor:false,
        toolbar: purchaseToolbarOptions,
        onAfterEdit: function (item) {
            if (item.column.name == 'rmbMoney') {
                countSum(purchaseGridManager.rows);
                var money = MathUtil.add(item.record.money, item.value);
                purchaseGridManager.updateRow(item.record, {totalMoney:money });
            }
        },
        onAfterShowData() {
            countSum(purchaseGridManager.rows);
            for (var r = 0; r < purchaseGridManager.rows.length; r++) {
                var money = MathUtil.add(purchaseGridManager.rows[r].rmbMoney, purchaseGridManager.rows[r].money);
                purchaseGridManager.updateRow(purchaseGridManager.rows[r], {totalMoney:money });
            }
        }
    })
    UICtrl.setSearchAreaToggle(purchaseGridManager);
}


function getPurchaseGridColumns() {
    var columns = [];
    columns = [
        {
            display: "采购单号", name: "produceNo", width: "160", align: "left", type: "string",
           // editor: {type: 'text', required: true}
            editor: {
                required: false, type: "select",
                data: {
                    type: 'system',
                    name: "dictionary",
                    getParam: function (item) {
                        return {fieldName: "EKKO"}
                    },
                    back: {
                        EBELN: "produceNo"
                        // MSEHL: "unitName"
                    }
                }
            }
        },
        // {display: "产品类别", name: "productType", width: "160", align: "left", type: "string"
        //     ,editor: {type: 'text',required:true}
        // },
        {
            display: "产品名称", name: "productName", width: "200", align: "left", type: "string"
            // , editor: {type: 'text', required: true}
            ,editor: {
                required: true, type: "select",
                data: {
                    type: 'system',
                    name: "sapMaterialSelect",
                    getParam: function (item) {
                        return {
                           // filterValue:item.company
                        }
                    },
                    back: {
                        MATNR: "productCode",
                        MAKTX: "productName",
                      //  EKGRP: "ekgrp",
                        MEINS: "unit",
                        MSEHL: "unitName"

                    }
                }
            }
        },
        {
            display: "产品编码", name: "productCode", width: "160", align: "left", type: "string"
            //, editor: {type: 'text', required: true}
        },
        {
            display: "数量", name: "number", width: "100", align: "left", type: "string"
            , editor: {type: "text", mask: '9999999.999'}
        },
        {
            display: "单位", name: "unitName", width: "80", align: "left", type: "string"
            // ,editor: {type: "text"}
            , editor: {
                required: true, type: "select",
                data: {
                    type: 'system',
                    name: "dictionary",
                    getParam: function (item) {
                        return {fieldName: "MEINS"}
                    },
                    back: {
                        MSEH3: "unit",
                        MSEHL: "unitName"
                    }
                }
            }
        },
        {
            display: "币别", name: "currencyName", width: "100", align: "left", type: "string",
            editor: {
                required: false, type: "select",
                data: {
                    type: 'system',
                    name: "dictionary",
                    getParam: function (item) {
                        return {fieldName: "WAERS"}
                    },
                    back: {
                        WAERS: "currency",
                        KTEXT: "currencyName",
                    }
                },
            }
            //editor: {type: "text"}
        },
        {
            display: "损失金额", name: "money", width: "100", align: "left", type: "string"
            , editor: {type: "text", mask: '9999999.999'}
        },
        {
            display: "罚款金额", name: "rmbMoney", width: "100", align: "left", type: "string",
            editor: {type: "text", mask: '9999999.999'}
        },
        {
            display: "损失金额", name: "totalMoney", width: "100", align: "left", type: "string"
         }
        // {
        //     display: "出货日期", name: "sendDate", width: "140", align: "left", type: "date"
        //     , editor: {type: "date"}
        // }
    ]
    return columns;
}


function countSum(zrow) {
    //var row=itemGridManager.rows;
    // var zrow=itemGridManager.rows;
    var money = 0;
    for (var r = 0; r < zrow.length; r++) {
        money = MathUtil.add(zrow[r].rmbMoney, money);
    }
    money = MathUtil.add($('#payMoney').val(), money);
    // for  (var x=0;x<row.length;x++){
    //  money = MathUtil.add(row[x].dutyMoney,money);
    // }
    $('#loseMoney').val(money);
}

function getDutyPersonGridColumns() {
    var columns = [];
    columns = [
        {
            display: "工号/客户编码", name: "personNo", width: "180", align: "left", type: "string",
            editor: {type: 'text'}
        },
        {
            display: "姓名/客户", name: "personName", width: "180", align: "left", type: "string",
            editor: {type: 'text'}
        },
        {
            display: "部门/客户名称", name: "deptName", width: "200", align: "left", type: "string",
            editor: {type: 'text'}
        },
        {
            display: "定责比例（%）", name: "dutyRate", width: "100", align: "left", type: "string",
            editor: {type: "text", mask: '999.99'}
        },
        {
            display: "个人责任比例（%）", name: "personDutyRate", width: "100", align: "left", type: "string",
            editor: {type: "text", mask: '999.99'}
        },
        {
            display: "责任金额", name: "dutyMoney", width: "100", align: "left", type: "string",
            editor: {type: "text", mask: '9999999.999'}
        },
        {
            display: "备注", name: "dutyRemark", width: "300", align: "left", type: "string",
            editor: {type: "text"}
        },
    ]
    return columns;
}

//获取数据保存的参数
function getExtendedData(processAction) {
//回退、打回、转交时，不需要验证必填项是否已经填写；
    if (processAction == ProcessAction.BACK
        || processAction == ProcessAction.REPLENISH
        || processAction == ProcessAction.TRANSMIT) {
//不验证
        $('#submitForm').attr('check', false);
    }
    var extendedData = {};
    var _grid = UICtrl.getGridManager('#producegrid');
    if ($('#exceptionReasonType').val() == "1") { //供应商材料问题
        _grid = UICtrl.getGridManager('#purchasegrid');
    }
    if ($('#exceptionReasonType').val() == "3") { //客户异常
        _grid = UICtrl.getGridManager('#clientgrid');
    }
    if (_grid) {
        var datas = DataUtil.getGridData({
            gridManager: _grid,
            isAllData: true
        });
        if (!datas) {
            return false;
        }
        extendedData.exceptionReportItems = Public.encodeJSONURI(datas);
    }

    var _dutyPersonGrid = UICtrl.getGridManager('#dutyPersonGrid');
    if (_dutyPersonGrid) {
        var datas2 = DataUtil.getGridData({
            gridManager: _dutyPersonGrid,
            isAllData: true
        });
        if (!datas2) {
            return false;
        }
        extendedData.personList = Public.encodeJSONURI(datas2);
    }
    return extendedData;
}

function getId() {
    return $('#id').val();
}

function setId(value) {
    $("#id").val(value);
    var _grid = UICtrl.getGridManager('#producegrid');
    _grid.options.parms.exceptionReportId = value;

    var _grid = UICtrl.getGridManager('#clientgrid');
    _grid.options.parms.exceptionReportId = value;

    var _purchasegrid = UICtrl.getGridManager('#purchasegrid');
    _purchasegrid.options.parms.exceptionReportId = value;

    var _dutyPersonGrid = UICtrl.getGridManager('#dutyPersonGrid');
    _dutyPersonGrid.options.parms.exceptionReportId = value;
}

function reloadGrid() {
    clientGridManager.loadData();
    produceGridManager.loadData();
    purchaseGridManager.loadData();
    dutyPersonGridManager.loadData();
    countSumChange();
}

function printPage() {
    Public.openPostWindow(web_app.name + '/PDFViewer/PDFViewer.do', {
        id: $("#id").val(),
        applicationType: "exceptionReport"
    });
}

function isNumber(val) {
    var regPos = /^\+?[1-9][0-9]*$/;
    // var regNeg = /^\-[1-9][0-9]*$/; // 负整数
    if (regPos.test(val)) {
        return true;
    } else {
        return false;
    }
}
