var gridManager = null;
var orderItemGridManager = null;
var materialGridManager = null;
var  canEdit=true;

$(document).ready(function () {
    initialize();
    loadMaterialGrid();
    loadOrderItemGrid();
    bindEvent();
});
function initialize(){
    UICtrl.layout("#layout", {leftWidth: 3});
 }

/**
 * 流程引擎回调设置打样的id
 * @param value 打样申请的id
 */
function setId(bizId) {
    $("#id").val(bizId);
    var _grid = UICtrl.getGridManager('#orderItemGrid');
    _grid.options.parms.processOutsourceId = bizId;
    var _grid3 = UICtrl.getGridManager('#materialGrid');
    _grid3.options.parms.processOutsourceId = bizId;
    orderItemGridManager.loadData();
    materialGridManager.loadData();
}

/**
 * 流程引擎获取打样申请id的回掉方法。
 * @return 打样申请的id
 */
function getId() {
    var id=$("#id").val() || "";
    return id;
}

// 加载订单行项目表格
function loadOrderItemGrid() {
    var toolbarOptions = null;
    toolbarOptions = UICtrl.getDefaultToolbarOptions({
        // 添加一行表格
        addHandler: function () {
            UICtrl.addGridRow(orderItemGridManager,
                {
                    // 行号加一
                    sequence: (orderItemGridManager.getData().length + 1),
                    kpein: 1,
                    waersTextView: "人民币",
                    waers: "CNY"

                }
            );
        },
        // 删除
        deleteHandler: function () {
            var _grid = UICtrl.getGridManager('#orderItemGrid');
            DataUtil.delSelectedRows({
                action: 'processOutsource/deleteProcessOutsourceOrderItem.ajax',
                param: {},
                gridManager: _grid,
                idFieldName: 'id',
                onSuccess: function () {
                    orderItemGridManager.loadData();
                }
            });
        }
    });
    // 表格
    orderItemGridManager = UICtrl.grid("#orderItemGrid", {
        columns: getOrderColumns(),
        dataAction: "server",
        _tableKey: 'processOutsourceItem',
        _local: true,
        url: web_app.name + '/processOutsource/queryProcessOutsourceOrderItems.ajax',
        parms: {processOutsourceId: getId()},
        pageSize: 20,
        usePager: true,
        toolbar: toolbarOptions,
        enabledEdit: true,
        width: "98%",
        height: "300px",
        heightDiff: -8,
        checkbox: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
    });
    UICtrl.setSearchAreaToggle(orderItemGridManager);
}

function getOrderColumns(){
    var columns = [];
    columns.push({display: "采购订单", name: "ebeln", width: 120, minWidth: 60, type: "string", align: "left"});
    columns.push({display: "订单行项目号", name: "ebelp", width: 120, minWidth: 60, type: "string", align: "left"});
    columns.push({display: "序号", name: "sequence", width: 50, minWidth: 60, type: "string", align: "left"},);
    columns.push({
        display: "原工单", name: "aufnr", width: 200, minWidth: 60, type: "string", align: "left",
        editor: {
            required: true, type: "select",
            data: {
                type: 'system',
                name: "dictionary",
                getParam: function (item) {
                    return {fieldName: "AUFNR"}
                },
                back: {
                    AUFNR: "aufnr",
                    VORNR: "wempf",
                    LTXA1: "txz01"
                }
            }
        }
    });
    columns.push({
        display: "工序号", name: "wempf", width: 140, minWidth: 60, type: "string", align: "left",
        editor: {
            type: "text", required: true
        }
    },);
    columns.push({
        display: "工序描述", name: "txz01", width: 200, minWidth: 60, type: "string", align: "left",
        editor: {
            type: "text", required: true
        }
    },);
    columns.push({
        display: "下道工序", name: "txz02", width: 200, minWidth: 60, type: "string", align: "left",
        editor: {
            required: true, type: "select",
            data: {
                type: 'system',
                name: "dictionary",
                getParam: function (item) {
                    return {
                        fieldName: "AUFNR",
                        paramValue: item.aufnr
                    }
                },
                back: {
                    LTXA1: "txz02"
                }
            }
        }
    },);
    columns.push({
        display: "补数工单", name: "ablad", width: 140, minWidth: 60, type: "string", align: "left",
        editor: {
            type: "text", required: false
        }
    },);
    columns.push({
        display: "数量", name: "menge", width: 140, minWidth: 60, type: "string", align: "left",
        editor: {
            type: "text", mask: '9999999.999', required: true
        }
    },);
    columns.push({
        display: "印张LOSS", name: "printLoss", width: 140, minWidth: 60, type: "string", align: "left",
        editor: {
            type: "text", mask: '9999999.999', required: true
        }
    },);
    columns.push({
        display: "单位", name: "meinsName", width: 140, minWidth: 60, type: "string", align: "left",
        editor: {
            required: true, type: "select",
            data: {
                type: 'system',
                name: "dictionary",
                getParam: function (item) {
                    return {fieldName: "MEINS"}
                },
                back: {
                    MSEH3: "meins",
                    MSEHL: "meinsName",
                }
            }
        }
    },);
    columns.push({
        display: "需求日期", name: "eeind", width: 140, minWidth: 60, type: "date", align: "left",
        editor: {
            type: "date", required: true
        }
    },);
    columns.push({
        display: "存储地点", name: "lgortName", width: 200, minWidth: 60, type: "string", align: "left",
        editor: {
            required: false, type: "select",
            data: {
                type: 'system',
                name: "dictionary",
                getParam: function (item) {
                    return {
                        fieldName: "LGORT",
                        filterValue: $("#werks").val()
                    }
                },
                back: {
                    LGORT: "lgort",
                    LGOBE: "lgortName"
                }
            },
        }
    },);
    if (typeof(getSubProcUnitId) =='function'&&"outer" == getSubProcUnitId()){
        columns.push({
            display: "单价", name: "kbetr", width: 140, minWidth: 60, type: "string", align: "left",
            editor: {
                type: "text", mask: '9999999.999', required: true
            }
        },);
        columns.push({
            display: "定价基数", name: "kpein", width: 140, minWidth: 60, type: "string", align: "left",
            editor: {
                type: "text", mask: '9999999.999', required: true
            }
        },);
        columns.push({
            display: "供应商", name: "lifnrName", width: 200, minWidth: 60, type: "string", align: "left",
            editor: {
                required: true, type: "select",
                data: {
                    type: 'suppliers',
                    name: "selectSuppliers",
                    getParam: function (item) {
                    },
                    back: {
                        partner: "lifnr",
                        nameOrg1: "lifnrName",
                    }
                }
            }
        },);
        columns.push({
            display: "币别", name: "waersTextView", width: 140, minWidth: 60, type: "string", align: "left",
            editor: {
                type: 'dictionary',
                data: {name: 'waers'},
                textField: 'waersTextView',
                valueField: 'waers',
                required: true,
                render: function (item) {
                    return item.waersTextView;
                }
            },
        },);
        columns.push({
            display: "税码", name: "purchaseMwskzTextView", width: 140, minWidth: 60, type: "string", align: "left",
            editor: {
                type: 'dictionary',
                data: {name: 'purchaseMwskz'},
                textField: 'purchaseMwskzTextView',
                valueField: 'purchaseMwskz',
                required: true,
                render: function (item) {
                    return item.purchaseMwskzTextView;
                }
            },
        });
    }else {
        columns.push({
            display: "单价", name: "kbetr", width: 140, minWidth: 60, type: "string", align: "left",
        },);
        columns.push({
            display: "定价基数", name: "kpein", width: 140, minWidth: 60, type: "string", align: "left",
        },);
        columns.push({
            display: "供应商", name: "lifnrName", width: 200, minWidth: 60, type: "string", align: "left",
        },);
        columns.push({
            display: "币别", name: "waersTextView", width: 140, minWidth: 60, type: "string", align: "left",
        },);
        columns.push({
            display: "税码", name: "purchaseMwskzTextView", width: 140, minWidth: 60, type: "string", align: "left",
        });
    }
    return columns;
}

// 加载组件信息表格
function loadMaterialGrid() {
    var toolbarOptions = null;
    toolbarOptions = UICtrl.getDefaultToolbarOptions({
        // 添加一行表格
        addHandler: function () {UICtrl.addGridRow(materialGridManager)},
        // 删除
        deleteHandler: function () {
            var _grid = UICtrl.getGridManager('#materialGrid');
            DataUtil.delSelectedRows({
                action: 'processOutsource/deleteProcessOutsourceMaterial.ajax',
                param: {},
                gridManager: _grid,
                idFieldName: 'id',
                onSuccess: function () {
                    materialGridManager.loadData();
                }
            });
        }
    });
    // 表格
    materialGridManager = UICtrl.grid("#materialGrid", {
        gridManager: null,
        columns: [
            {
                display: "供给物品名称", name: "materialName", width: 300, minWidth: 60, type: "String", align: "left",
                editor: {type: 'text', required: true}
            },
            {
                display: "数量", name: "num", width: 150, minWidth: 60, type: "String", align: "left",
                editor: {
                    required: true, type: "text",mask: '9999999.999'
                }
            },
            {
                display: "计量单位", name: "unitName", width: 100, minWidth: 60, type: "String", align: "left",
                editor: {
                    required: true, type: "select",
                    data: {
                        type: 'system',
                        name: "dictionary",
                        getParam: function (item) {
                            return {fieldName: "MEINS"}
                        },
                        back: {
                            MSEH3: "unit",
                            MSEHL: "unitName",
                        }
                    }
                }
            },
            {
                display: "运输方式", name: "deliveryMethodOutTextView", width: 200, minWidth: 60, type: "String", align: "left",
                editor: {
                    type: 'dictionary',
                    data: {name: 'deliveryMethodOut'},
                    textField: 'deliveryMethodOutTextView',
                    valueField: 'deliveryMethodOut',
                    required: true,
                    render: function (item) {
                        return item.deliveryMethodOutTextView;
                    }
                }
            },
        ],
        dataAction: "server",
        url: web_app.name + '/processOutsource/queryProcessOutsourceMaterials.ajax',
        parms: {processOutsourceId: getId()},
        pageSize: 20,
        usePager: true,
        toolbar: toolbarOptions,
        enabledEdit: canEdit,
        width: "98%",
        height: "300px",
        heightDiff: -8,
        checkbox: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
    });
    UICtrl.setSearchAreaToggle(materialGridManager);
}

//获取数据保存的参数
function getExtendedData(processAction) {
    //回退、打回、转交时，不需要验证必填项是否已经填写；
    if (processAction == ProcessAction.BACK
        || processAction == ProcessAction.REPLENISH
        || processAction == ProcessAction.TRANSMIT) {
        //不验证
        $('#submitForm').attr('check', false);
    }
    var extendedData = {};
    var _grid = UICtrl.getGridManager('#orderItemGrid');
    if (_grid) {
        var datas = DataUtil.getGridData({
            gridManager: _grid,
            isAllData: true
        });
        if (!datas) {
            return false;
        }
        extendedData.orderItems = Public.encodeJSONURI(datas);
    }

    var material_grid = UICtrl.getGridManager('#materialGrid');
    if (material_grid) {
        var datas = DataUtil.getGridData({
            gridManager: material_grid,
            isAllData: true
        });
        if (!datas) {
            return false;
        }
        extendedData.materialItems = Public.encodeJSONURI(datas);
    }
    return extendedData;
}


function checkConstraints(){
    //物料组件信息
    var material_grid = UICtrl.getGridManager('#materialGrid');
    if (material_grid) {
        var datas = DataUtil.getGridData({
            gridManager: material_grid,
            isAllData: true
        });
        if (!datas) {
            return false;
        }
        if (isApplyProcUnit() && datas.length == 0 ){
            Public.tip("物料组件信息必填");
            return false;
        }
    }
    //订单行项目信息
    var orderItem_grid = UICtrl.getGridManager('#orderItemGrid');
    if (orderItem_grid) {
        var datas = DataUtil.getGridData({
            gridManager: orderItem_grid,
            isAllData: true
        });
        if (!datas) {
            return false;
        }
        if (isApplyProcUnit() && datas.length == 0){
            Public.tip("订单行项目信息必填");
            return false;
        }
    }
    return true;
}


function bindEvent(){
    $('#werksName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "WERKS"}
        },
        onChange: function (value, data) {
            $('#werks').val(data.WERKS);
            $('#werksName').val(data.NAME1);
        }
    });
}

function reloadGrid() {
    orderItemGridManager.loadData();
    materialGridManager.loadData();
}