var itemGridManager = null;
$(function () {
    $("#toolBar").toolBar("enable", "forceAbortTask");
    revoke();
    initItemGrid();
    bindEvent();
    var retpo = $("#retpo").val();
    if ("X" == retpo){
        $("#billTitle .subject").html("采购订单退货")
    }
    if ("X0" == retpo){
        $("#billTitle .subject").html("采购订单换货")
    }
});

function bindEvent() {


}

function revoke(){
    var revokeType = $("#orderRevokeType").val();
    if ("update" == revokeType) {
        $("#billTitle .subject").html("撤审修改采购订单")
        $("#revoke").show();
    }else if ("close" == revokeType){
        $("#billTitle .subject").html("撤审关闭采购订单")
        $("#revoke").show();
    }else if ("delete" == revokeType){
        $("#billTitle .subject").html("撤审删除采购订单")
        $("#revoke").show();
    }else {
        $("#revoke").hide();
    }
}

function initItemGrid() {
    itemGridManager = UICtrl.grid('#maingrid', {
            columns: getGridColumns(),
            dataAction: 'server',
            _tableKey: 'sapPurchaseOrder',
            _local: true,
            url: web_app.name + '/sapPurchaseOrder/querySapPurchaseOrderItems.ajax',
            parms: {sapPurchaseOrderId: getId()},
            height: '95%',
            heightDiff: -4,
            // sortName: 'id',
            // sortOrder: 'asc',
            usePager: true,
            rownumbers: true,
            // fixedCellHeight: true,
            onAfterShowData: function (){
                initTotalMoney();
                initTotalNum();
            }
        }
    );
    UICtrl.setSearchAreaToggle(itemGridManager);
}

function getGridColumns() {
    return [
        {display: "项目编号", name: "ebelp", width: "100", align: "left", type: "string"},
        {display: "项目类别", name: "pstyp", width: "100", align: "left", type: "string"},
        {display: "项目类别描述", name: "knttp", width: "100", align: "left", type: "string"},
        {display: "科目分配类别描述", name: "knttx", width: "100", align: "left", type: "string"},
        {display: "物料号", name: "matnr", width: "200", align: "left", type: "string"},
        {display: "物料描述", name: "txz01", width: "300", align: "left", type: "string"},
        {display: "文本", name: "maktxRemZh", width: "300", align: "left", type: "string",},
        {display: "物料组", name: "matkl", width: "100", align: "left", type: "string"},
        {display: "物料组描述", name: "wgbez", width: "100", align: "left", type: "string"},
        {display: "采购订单数量", name: "menge", width: "100", align: "left", type: "string"},
        {display: "采购订单计量单位", name: "meins", width: "100", align: "left", type: "string"},
        {display: "单位描述", name: "msehl", width: "100", align: "left", type: "string"},
        {display: "序列号", name: "sernr", width: "200", align: "left", type: "string"},
        {display: "要求交货日期", name: "eindt", width: "100", align: "left", type: "string"},
        {display: "工厂", name: "werks", width: "100", align: "left", type: "string"},
        {display: "名称", name: "name1", width: "200", align: "left", type: "string"},
        {display: "库存地点", name: "lgort", width: "100", align: "left", type: "string"},
        {display: "库存地点的描述", name: "lgobe", width: "200", align: "left", type: "string"},
        {display: "含税单价", name: "netpr", width: "100", align: "left", type: "string"},
        {display: "运输费", name: "kbetr2", width: "100", align: "left", type: "string"},
        {display: "包装费", name: "kbetr3", width: "100", align: "left", type: "string"},
        {display: "价格基数", name: "peinh", width: "100", align: "left", type: "string"},
        //{display: "税码", name: "mwskz", width: "100", align: "left", type: "string"},
        {display: "进项税率(%)", name: "kbetr", width: "100", align: "left", type: "string"},
        {display: "含税总额", name: "netwr", width: "100", align: "left", type: "string"},
        {display: "退货项目", name: "retpo", width: "100", align: "left", type: "string"},
        {display: "免费项目", name: "umson", width: "100", align: "left", type: "string"},
        {display: "总账科目", name: "sakto", width: "100", align: "left", type: "string"},
        {display: "成本中心", name: "kostl", width: "200", align: "left", type: "string"},
        {display: "主资产号", name: "anln1", width: "200", align: "left", type: "string"},
        {display: "资产次级编号", name: "anln2", width: "100", align: "left", type: "string"}

    ]

}

function initTotalMoney(){
    var _grid = UICtrl.getGridManager('#maingrid');
    if (_grid != null){
        var data = _grid.currentData.Rows;
        var totalMoney = 0;
        $.each(data, function (i, o) {
            var _netwr = o['netwr'] + '';
            totalMoney = MathUtil.add(totalMoney, _netwr);
            $('#totalMoney').val(totalMoney);
        });
    }
}

function initTotalNum(){
    var _grid = UICtrl.getGridManager('#maingrid');
    if (_grid != null){
        var data = _grid.currentData.Rows;
        var totalNum = 0;
        $.each(data, function (i, o) {
            var _menge = o['menge'] + '';
            totalNum = MathUtil.add(totalNum, _menge);
            $('#totalNum').val(totalNum);
        });
    }
}


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

function setId(value) {
    $("#id").val(value);
}



