var itemGridManager = null;
$(function () {
    initItemGrid();
    bindEvent();
});

function bindEvent() {


}

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();
            }
        }
    );
    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: "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: "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: "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 getId() {
    return $('#id').val();
}

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



