var itemGridManager = null,pstyp=" ",fillinDate="",ZEBKN_NUM=0;
$(function () {
    initItemGrid();
    bindEvent();
});

function bindEvent() {
    $("#bsart").combox({onChange:function(data){
            if ("ZFW" == data.value){
                pstyp = "D";
            }else {
                pstyp = " ";
            }
        }
    })

    fillinDate = $("#fillinDate").val();

}

function initItemGrid() {
    var toolbarOptions = UICtrl.getDefaultToolbarOptions({
        addHandler: function(){
            UICtrl.addGridRow(itemGridManager,{ bnfpo: (itemGridManager.getData().length + 1)*10 ,pstyp:pstyp,badat:fillinDate,erdat:fillinDate});
        },
        deleteHandler: function (){
            var _grid = UICtrl.getGridManager('#maingrid');
            DataUtil.delSelectedRows({
                action: 'sapPurchaseItem/deleteSapPurchaseItem.ajax',
                param: {},
                gridManager: _grid,
                idFieldName: 'id',
                onSuccess: function () {
                    reloadGrid();
                }
            });
        },
    });
        itemGridManager = UICtrl.grid('#maingrid', {
            columns: getGridColumns(),
            dataAction: 'server',
            url: web_app.name + '/sapPurchaseItem/querySapPurchaseItems.ajax',
            parms:{sapPurchaseId:getId()},
            height: '99.6%',
            heightDiff: -4,
            sortName: 'id',
            sortOrder: 'asc',
            checkbox: true,
            usePager: true,
            rownumbers: true,
            enabledEdit: true,
            fixedCellHeight: true,
            selectRowButtonOnly: true,
            //autoApplyNextEditor:false,
            toolbar: toolbarOptions,
        }
    );
    UICtrl.setSearchAreaToggle(itemGridManager);
}

function getGridColumns() {
    var columns = [];
    columns = [
        {display: "申购项次号", name: "bnfpo", width: "80", align: "left", type: "string",
            editor: { type: 'spinner', min: 10, max: 200, mask: 'nnn'}
        },
        {display: "项次类别", name: "pstyp", width: "100", align: "left", type: "string"},
        {display: "科目分配类别", name: "knttp", width: "100", align: "left", type: "string",
            editor: {
                required: false, type: "select",
                data: {
                    type: 'system',
                    name: "knttp",
                    getParam: function (item) {
                        return {paramValue: $("#bsart").val()}
                    },
                    back: {
                        knttp: "knttp"
                    }
                },
            }
        },
        {display: "工厂", name: "werks", width: "200", align: "left", type: "string",
            editor: {required: false, type: "text"}
        },
        {display: "工厂名称", name: "name1", width: "100", align: "left", type: "string"},
        {display: "物料编号", name: "matnr", width: "100", align: "left", type: "string",
            /*editor: {
                required: true, type: "select",
                data: {
                    type: 'system',
                    name: "chooseUnit",
                    getParam: function (item) {
                        return {paramValue: item.materNo}
                    },
                    back: {meins: "unit"}
                },
            }*/
        },
        {display: "库存地点", name: "lgort", width: "100", align: "left", type: "string",},
        {display: "库存地点名称", name: "lgobe", width: "100", align: "left", type: "string"},
        {display: "短文本", name: "txz01", width: "100", align: "left", type: "string",
            editor: {required: false, type: "text"}
        },
        {display: "物料组", name: "matkl", width: "100", align: "left", type: "string",
            editor: {required: false, type: "text"}
        },
        {display: "申购数量", name: "menge", width: 150, minWidth: 60, type: "string", align: "center",
            editor: {required: false, type: "text",mask: '9999999.999'}
        },
        {display: "申购数量单位", name: "meins", width: 150, minWidth: 60, type: "string", align: "center",
            editor: {required: false, type: "text"}
        },
        {display: "采购组", name: "ekgrp", width: 150, minWidth: 60, type: "string", align: "center",
            editor: {required: false, type: "text"}
        },
        {display: "申请日期", name: "badat", width: "100", align: "left", type: "string",
            editor: {required: false, type: "date"}
        },
        {display: "创建日期", name: "erdat", width: "100", align: "left", type: "string",
            editor: {required: false, type: "date"}
        },
        {display: "批准日期", name: "frgdt", width: "80", align: "left", type: "string",
            editor: {required: false, type: "date"}
        },
        {display: "要求交货日期", name: "lfdat", width: "100", align: "left", type: "string",
            editor: {required: false, type: "date"}
        },
        {display: "服务主数据编号", name: "srvpos", width: "80", align: "left", type: "string",
            editor: {type: 'text', required: false},
        },
        {display: "服务短文本", name: "ktext1", width: "100", align: "left", type: "string"},
        {display: "服务条目数量", name: "esllMenge", width: "80", align: "left", type: "string",
            //editor: {type: 'text', mask: '9999999.999'},
            render:function(item){
                return item.esllMenge=item.menge;
            }
        },
        {display: "服务计量单位", name: "esllMeins", width: "100", align: "left", type: "string",
            editor: {type: "text", required: false,}
        },
        {display: '定价基数', name: "peinh", width: "100", align: "left", type: "string",
            //editor: {required: true, type: "select"}
            render:function(item){
                return item.peinh=item.preis;
            }
        },
        {display: "服务预估单价", name: "brtwr", width: "100", align: "left", type: "string",
            editor: {type: 'text', mask: '9999999.99'}
        },
        {display: "服务预估金额", name: "netwr", width: "100", align: "left", type: "string",
            render:function(item){
                if (item.brtwr == '' || item.brtwr == null || item.peinh == '' || item.peinh == null){
                    return item.netwr=0;
                }
                return item.netwr=(item.brtwr*item.esllMenge/item.peinh).toFixed(2);
            }
        },
        {display: "创建者", name: "ernam", width: "200", align: "left", type: "string"},
        {display: "申请人", name: "afnam", width: "90", align: "left", type: "string",
            editor: {type: 'text', required: false},
        },
        {display: "评估价格", name: "preis", width: "100", align: "left", type: "string",
            editor: {type: 'text', mask: '9999999.99',required: false}
        },
        {display: "价格基数", name: "ebanPeinh", width: "100", align: "left", type: "string",
            editor: {type: 'text', mask: '9999999',required: false}
        },
        {display: "币种", name: "waers", width: "100", align: "left", type: "string",
            editor: {type: 'text'}
        },
        {display: "评估金额", name: "ebanNetwr", width: "100", align: "left", type: "string",
            render:function(item){
                if (item.ebanPeinh == ''){
                    return item.ebanNetwr=null;
                }
                return item.ebanNetwr=(item.preis*item.menge/item.ebanPeinh).toFixed(2);
            }
            //editor: {type: 'text'}
        },
        {display: "会计分配序号", name: "zebkn", width: "100", align: "left", type: "string",
            editor: {type: 'text'},
            render:function (item){
            //申购项次为消耗性采购时，从1开始步长为1自动编号，最大99
                if (item.knttp != ''){
                    ZEBKN_NUM = ZEBKN_NUM + 1;
                }
                return item.zebkn = ZEBKN_NUM;
            }
        },
        {display: "多重科目分配", name: "vrtkz", width: "100", align: "left", type: "string",
            editor: {type: 'text'},
            render : function (item) {
                var bsart =$("#bsart").val();
                //资产消耗性申购，若申请数量大于1，此处不能为空，必须选择1
                if (bsart == "ZNE1" && item.menge > 1){
                    return item.vrtkz = 1;
                }
            }
        },
        {display: "部分发票", name: "twrkz", width: "100", align: "left", type: "string",
            editor: {type: 'text'},
            render : function (item) {
                var bsart =$("#bsart").val();
                //资产消耗性申购，若申请数量大于1，此处不能为空，必须选择1
                if (bsart == "ZNE1" && item.menge > 1){
                    return item.twrkz = 1;
                }
            }
        },
        {display: "PR分解数量", name: "ekbnMenge", width: "100", align: "left", type: "string",
            editor: {type: 'text'}
        },
        {
            display: "总账科目", name: "sakto", width: "100", align: "left", type: "string",
            editor: {type: 'text'}
        },
        {
            display: "成本中心", name: "kostl", width: "100", align: "left", type: "string",
            editor: {type: 'text'}
        },
        {
            display: "订单号", name: "aufnr", width: "100", align: "left", type: "string",
            editor: {type: 'text'}
        },
        {
            display: "主资产号", name: "anln1", width: "100", align: "left", type: "string",
            editor: {type: 'text'}
        },
        {
            display: "子资产号", name: "anln2", width: "100", align: "left", type: "string",
            editor: {type: 'text'}
        },
        {
            display: "审批处理状态", name: "banpr", width: "100", 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('#maingrid');
    if (_grid) {
        var datas = DataUtil.getGridData({
            gridManager: _grid,
            isAllData: true
        });
        if (!datas) {
            return false;
        }
        extendedData.sapPurchaseItems = Public.encodeJSONURI(datas);
    }
    return extendedData;
}

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

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

function reloadGrid() {
    itemGridManager.loadData();
}

function getGridData(){
    var _grid = UICtrl.getGridManager('#maingrid');
    var datas = DataUtil.getGridData({
        gridManager: _grid,
        isAllData: true
    });
    return datas;
}

function getBsartNum(){
    var datas = this.getGridData();
    datas.forEach(data=>{

    })
}

