var itemGridManager = null,fillinDate="";
$(document).ready(function () {
    initialize();
    initItemGrid();
    bindEvent();
});
function initialize() {
    UICtrl.layout("#layout", {leftWidth: 3});
    $('#fileList').fileList({bizId:$("#id").val()});
}

function bindEvent() {
    fillinDate = $("#fillinDate").val();
}

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

    });
        itemGridManager = UICtrl.grid('#maingrid', {
            columns: getGridColumns(),
            dataAction: 'server',
            _tableKey: 'sapPurchaseInfoRecordItem',
            _local: true,
            url: web_app.name + '/sapPurchaseInfoRecordItem/queryUpdateSapPurchaseInfoRecordItems.ajax',
            parms:{sapPurchaseInfoRecordId:getId(),ids:$("#ids").val()},
            height: '94%',
            heightDiff: -4,
            sortName: 'id',
            sortOrder: 'asc',
            checkbox: true,
            usePager: true,
            rownumbers: true,
            enabledEdit: true,
            fixedCellHeight: true,
            selectRowButtonOnly: true,
            //autoApplyNextEditor:false,
            toolbar: toolbarOptions,
            onDblClickRow: function (data, rowindex, rowobj) {
                viewHandler(data);
            },
        }
    );
    UICtrl.setSearchAreaToggle(itemGridManager);
}

function getGridColumns() {
    var columns = [];
    columns = [
        {display: "id", name: "id", width: "100", align: "left", type: "string",hide:true},
        {display: "采购信息记录号", name: "infnr", width: "100", align: "left", type: "string"},
        {display: "定价条件号", name: "knumh", width: "100", align: "left", type: "string"},
        {display: "工厂", name: "werks", width: "10", align: "left", type: "string",hide:true},
        {display: "工厂", name: "werksName", width: "200", align: "left", type: "string"},
        {display: "采购组织", name: "ekorg", width: "10", align: "left", type: "string",hide:true},
        {display: "采购组织", name: "ekorgName", width: "200", align: "left", type: "string"},
        {display: "供应商编码", name: "lifnr", width: "100", align: "left", type: "string"},
        {display: "供应商名称1", name: "name1", width: "200", align: "left", type: "string"},
        {display: "供应商物料编码", name: "idnlf", width: "120", align: "left", type: "string"},
        {display: "物料编码", name: "matnr", width: "160", align: "left", type: "string",},
        {display: "物料描述", name: "maktx", width: "200", align: "left", type: "string",},
        {display: "物料组编号", name: "matkl", width: "10", align: "left", type: "string",hide:true},
        {display: "物料组", name: "matklName", width: "160", align: "left", type: "string"},
        {display: "采购信息记录分类编号", name: "esokz", width: "20", align: "left", type: "string",hide:true},
        {display: "采购信息记录分类", name: "esokzTextView", width: "140", align: "left", type: "string"},
        {display: "采购组编号", name: "ekgrp", width: "20", align: "left", type: "string",hide:true},
        {display: "采购组", name: "ekgrpName", width: "200", align: "left", type: "string"},
        {display: "净价", name: "netpr", width: "100", align: "left", type: "string"},
        {display: "运输费", name: "freight", width: "100", align: "left", type: "string"},
        {display: "包装费", name: "kbetr6", width: "100", align: "left", type: "string"},
        {display: "货币码编号", name: "waers", width: "20", align: "left", type: "string",hide:true},
        {display: "货币码", name: "waersName", width: "100", align: "left", type: "string"},
        {display: "价格单位", name: "peinh", width: "100", align: "left", type: "string"},
        {display: "订单价格单位编号", name: "bprme", width: "20", align: "left", type: "string",hide:true},
        {display: "订单价格单位(采购)", name: "bprmeName", width: "90", align: "left", type: "string"},
        {display: "标准采购订单数量", name: "norbm", width: "120", align: "left", type: "string"},
        {display: "基本计量单位", name: "meinsName", width: "100", align: "left", type: "string"  },
        {display: "基本计量单位", name: "meins", width: "40", align: "left", type: "string",hide:"true" },
        {display: "订单单位转换分母", name: "umren", width: "100", align: "left", type: "string"},
        {display: "订单单位转换分子", name: "umrez", width: "100", align: "left", type: "string"},
        {display: "销售/购买税代码", name: "mwskz", width: "40", align: "left", type: "string",hide:"true" },
        {display: "销售/购买税代码", name: "mwskzTextView", width: "150", align: "left", type: "string"},
        {display: "开始生效日期", name: "datab", width: "100", align: "left", type: "string"},
        {display: "有效期至日期", name: "datbi", width: "100", align: "left", type: "string"},
        {display: "销售员", name: "verkf", width: "100", align: "left", type: "string"},
        {display: "电话", name: "telf1", width: "100", align: "left", type: "string"},
        {display: "原产地国代码", name: "urzla", width: "40", align: "left", type: "string",hide:"true"},
        {display: "原产地国", name: "urzlaName", width: 150, minWidth: 60, type: "string", align: "center"},
        {display: "地区", name: "regio", width: 10, minWidth: 60, type: "string", align: "center",hide:"true"},
        {display: "地区", name: "regioName", width: 150, minWidth: 60, type: "string", align: "center"},
        {display: "删除标志", name: "loekz", width: "40", align: "left", type: "string",hide:"true" },
        {display: "删除标志", name: "loekzTextView", width: 150, minWidth: 60, type: "string", align: "center"},
        {display: "排序字段", name: "sortl", width: 150, minWidth: 60, type: "string", align: "center"},
        {display: "最小采购订单数量", name: "minbm", width: "120", align: "left", type: "string"},
        {display: "计划交货时间(天)", name: "aplfz", width: "150", align: "left", type: "string"},
        {display: "基于收货的发票验证", name: "webre", width: "40", align: "left", type: "string",hide:"true" },
        {display: '基于收货的发票验证', name: "webreTextView", width: "100", align: "left", type: "string"},
        {display: "价格有效至", name: "prdat", width: "100", align: "left", type: "string",hide:true,
            //editor: {type: 'date', required: false}
            render: function (item){
                return item.prdat = item.datbi
            }
        },
        {display: "等级类型", name: "stfkz", width: "40", align: "left", type: "string",hide:"true" },
        {display: "等级类型", name:"stfkzTextView", width: "200", align: "left", type: "string",},
        {display: "条件等级计量单位", name: "konms", width: "100", align: "left", type: "string"},
        {display: "等级数量1", name: "kstbm", width: "100", align: "left", type: "string"},
        {display: "等级金额1", name: "kbetr", width: "100", align: "left", type: "string"},
        {display: "等级数量2", name: "kstbm2", width: "100", align: "left", type: "string"},
        {display: "等级金额2", name: "kbetr2", width: "100", align: "left", type: "string"},
        {display: "等级数量3", name: "kstbm3", width: "100", align: "left", type: "string"},
        {display: "等级金额3", name: "kbetr3", width: "100", align: "left", type: "string"},
        {display: "等级数量4", name: "kstbm4", width: "100", align: "left", type: "string"},
        {display: "等级金额4", name: "kbetr4", width: "100", align: "left", type: "string"},
        {display: "等级数量5", name: "kstbm5", width: "100", align: "left", type: "string"},
        {display: "等级金额5", name: "kbetr5", width: "100", align: "left", type: "string"},
        {display: "备注", name: "remark", width: "300", align: "left", type: "string",}
    ]
    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;
        }
        datas.forEach(data=>{
            if (Public.isNotBlank(data.bprme)
                && Public.isNotBlank(data.meins)
                && (Public.isBlank(data.umren) || Public.isBlank(data.umrez))){
                Public.tip("订单价格单位不为空，单位转换分子、分母必填")
                return false;
            }

        })
        extendedData.sapPurchaseInfoRecordItems = Public.encodeJSONURI(datas);
    }
    return extendedData;
}

//提交前校验
function checkConstraints() {
    var _grid = UICtrl.getGridManager('#maingrid');
    var datas = DataUtil.getGridData({
        gridManager: _grid,
        isAllData: true
    });
    if (datas.length < 1) {
        Public.tip("请添加采购信息记录");
        return false;
    }
    return true;
}

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

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

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

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

function initShowDialog() {
    if ($("#datab").val() == null || $("#datab").val() == ''){
        $("#datab").val($("#fillinDate").val().substring(0,10));
    }
    if ($("#datbi").val() == null || $("#datbi").val() == ''){
        $("#datbi").val('9999-12-31');
    }
    // var mainFileTable = $('#compensateLibraryList').fileList();
    // mainFileTable.find('table').css({
    //     borderTopWidth: 0
    // });
}
function  doSaveDefine() {
    var _self = this;
    var returnResult=checkInput();
    var row = itemGridManager.rows;
    var rId="";
    if ($("#matnr").val()==""&&$("#matklName").val()==""){
       Public.tip("物料编码与物料组必须填写一个！");
       return false;
    }
    if ($("#matnr").val()==""&&$("#maktx").val()==""){
        Public.tip("物料描述必须填写！");
        return false;
    }
   // if ($("#matnr").val()==""&&$("#sortl").val()==""){
    //    Public.tip("排序字段必须填写！");
    //    return false;
   // }
    if (MathUtil.compare($("#norbm").val(),$("#minbm").val())<0){
        Public.tip("标准采购订单数量必须大于最小采购订单量！");
        return false;
    }
    if (MathUtil.compare($("#peinh").val(),1) < 0){
        Public.tip("价格单位必须大于等于 1");
        return false;
    }
    if  (returnResult) {
        if (row.length > 0) {
            for (var u = 0; u < row.length; u++) {
                rId = itemGridManager.rows[u].__id;
                if ($("#rowId").val() == rId) {
                    itemGridManager.deleteRow(itemGridManager.rows[u]);
                    break;
                }
            }
        }

        UICtrl.addGridRow(itemGridManager, {
            id: $("#id").val(),
            infnr: $("#infnr").val(),
            werks: $("#werks").val(),
            werksName: $("#werksName").val(),
            ekorg: $("#ekorg").val(),
            ekorgName: $("#ekorgName").val(),
            lifnr: $("#lifnr").val(),
            name1: $("#name1").val(),
            idnlf: $("#idnlf").val(),
            matnr: $("#matnr").val(),
            maktx: $("#maktx").val(),
            matkl: $("#matkl").val(),
            matklName: $("#matklName").val(),
            esokz: $("#esokz").val(),
            esokzTextView: $("#esokz").find("option:selected").text(),
            ekgrp: $("#ekgrp").val(),
            ekgrpName: $("#ekgrpName").val(),
            netpr: $("#netpr").val(),
            freight: $("#freight").val(),
            kbetr6: $("#kbetr6").val(),
            waers: $("#waers").val(),
            ltsnr: $("#ltsnr").val(),
            ltsbz: $("#ltsbz").val(),
            waersName: $("#waersName").val(),
            peinh: $("#peinh").val(),
            bprme: $("#bprme").val(),
            bprmeName: $("#bprmeName").val(),
            norbm: $("#norbm").val(),
            meins: $("#meins").val(),
            meinsName: $("#meinsName").val(),

            umren: $("#umren").val(),
            umrez: $("#umrez").val(),

            mwskz: $("#mwskz").val(),
            mwskzTextView: $("#mwskz").find("option:selected").text(),
            datab: $("#datab").val(),
            datbi: $("#datbi").val(),
            verkf: $("#verkf").val(),
            telf1: $("#telf1").val(),

            urzlaName: $("#urzlaName").val(),
            urzla: $("#urzla").val(),
            regioName: $("#regioName").val(),
            regio: $("#regio").val(),

            loekz: $("#loekz").val(),
            loekzTextView: $("#loekz").find("option:selected").text(),
            sortl: $("#sortl").val(),
            minbm: $("#minbm").val(),
            aplfz: $("#aplfz").val(),

            webre: $("#webre").val(),
            webreTextView: $("#webre").find("option:selected").text(),
            stfkz: $("#stfkz").val(),
            stfkzTextView: $("#stfkz").find("option:selected").text(),
            konms: $("#konms").val(),

            kstbm: $("#kstbm").val(),
            kbetr: $("#kbetr").val(),
            kstbm2: $("#kstbm2").val(),
            kbetr2: $("#kbetr2").val(),
            kstbm3: $("#kstbm3").val(),
            kbetr3: $("#kbetr3").val(),
            kstbm4: $("#kstbm4").val(),
            kbetr4: $("#kbetr4").val(),
            kstbm5: $("#kstbm5").val(),
            kbetr5: $("#kbetr5").val(),
            remark: $("#remark").val(),
            knumh: $("#knumh").val(),
        });

        this.close();
    }
}
function  checkInput() {
    if ($("#werks").val()==""){
      Public.tip("工厂必输！");
      return false;
    }
    if ($("#ekorg").val()==""){
        Public.tip("采购组织必输！");
        return false;
    }
    if ($("#lifnr").val()==""){
        Public.tip("供应商必输！");
        return false;
    }
   /* if ($("#matnr").val()==""){
        Public.tip("物料编码必输！");
        return false;
    }*/
    if ($("#esokz").val()==""){
        Public.tip("记录分类必输！");
        return false;
    }
    if ($("#ekgrp").val()==""){
        Public.tip("采购组必输！");
        return false;
    }
    if ($("#netpr").val()==""){
        Public.tip("净价必输！");
        return false;
    }
    if ($("#waers").val()==""){
        Public.tip("货币码必输！");
        return false;
    }
    if ($("#mwskz").val()==""){
        Public.tip("销售/购买税代码必输！");
        return false;
    }
    if ($("#datab").val()==""){
        Public.tip("生效日期起必输！");
        return false;
    }
    if ($("#datbi").val()==""){
        Public.tip("生效日期止必输！");
        return false;
    }
    if ($("#peinh").val()==""){
        Public.tip("价格单位必输！");
        return false;
    }
    if ($("#bprmeName").val()==""){
        Public.tip("订单价格单位(采购)必输！");
        return false;
    }
    if ($("#mwskz").val()==""){
        Public.tip("销售/购买税代码必输！");
        return false;
    }
    if ($("#umren").val()==""){
        Public.tip("订单单位转换分母必输！");
        return false;
    }
    if ($("#umrez").val()==""){
        Public.tip("订单单位转换分子必输！");
        return false;
    }
    if ($("#aplfz").val()==""){
        Public.tip("计划交货时间(天)必输！");
        return false;
    }
    if ($("#webre").val()==""){
        Public.tip("基于收货的发票验证必输！");
        return false;
    }
    if ($("#stfkz").val()==""){
        Public.tip("等级类型必输！");
        return false;
    }

    return true;
}
function  viewHandler(data){
    var param = JSON.stringify(data);
    var rowId=data.__id;
    var readwriteList = UICtrl.getUIElementPermissions(UIPO.READWRITE, UIPK.DETAIL);
    if ($("#isReadOnly").val()=="true"||($("#statusId").val()==1&&readwriteList.length==0&&TaskKind.isReplenishTask(taskKindId)!=true)) {
      UICtrl.showAjaxDialog({
        title: "查看采购信息记录",
        url: web_app.name + '/sapPurchaseInfoRecord/cancelSapPurchaseInfoRecordDetail.load?id=' + data.id + '&rowId=' + rowId+'&isReadOnly=true',
        param: {
            param
        },
        width: 1200,
        height: 860,
        init: initShowDialog,
        ok:false,
        close: onDialogCloseHandler
      });
  } else {
        UICtrl.showAjaxDialog({
            title: "作废采购信息记录",
            url: web_app.name + '/sapPurchaseInfoRecord/cancelSapPurchaseInfoRecordDetail.load?id=' + data.id + '&rowId=' + rowId,
            param: {
                param
            },
            width: 1200,
            height: 860,
            init: initShowDialog,
            okVal: '确定',
            ok: doSaveDefine,
            close: onDialogCloseHandler
        });
    }
}
// 关闭对话框
function onDialogCloseHandler() {
    //if (refreshFlag) {
        parent.reloadGrid();
      //  refreshFlag = false;
   // }
}