var purchaseOrderGridManager = null;
$(document).ready(function () {
        initUI();
    loadGrid();
    }
);

function initUI() {
    UICtrl.initDefaultLayout();
}

function loadGrid() {
    var toolbarOptions = UICtrl.getDefaultToolbarOptions({
        sapPrintHandler: {
            id: 'sapPrintHandler', text: 'SAP外协单打印', img:'fa-clipboard', click: function(){
                printHandler();
            }
        },
      // revokeApproveHandler: {
      //     id: 'revokeApproveHandler', text: '撤销审批', img: 'fa-trash', click: function () {
      //         var rows = purchaseOrderGridManager.getSelectedRows();
      //         if (rows.length == 0 || rows.length > 1) {
      //             Public.tip("请选择一条已完成的数据！");
      //             return;
      //         }
      //         if (rows[0].status != 3) {
      //             Public.tip("请选择一条已完成的数据！");
      //             return;
      //         }
      //         UICtrl.confirm('确定要把当前单据撤销审批吗?', function () {
      //             Public.ajax(web_app.name + '/sapPurchaseOrder/revokeApproval.ajax', {id: rows[0].id}, function (msg) {
      //                 purchaseOrderGridManager.loadData();
      //             });
      //         });
      //     }
      //   }
        });
    //debugger
    purchaseOrderGridManager = UICtrl.grid('#purchaseOrderGrid', {
            columns: [
                {display: "状态", name: "statusTextView", width: "100", align: "left", type: "string"}
                , {display: "id", name: "id", width: "140", align: "left", type: "string", hide: true}
                , {display: "需求跟踪编号", name: "billCode", width: 150, minWidth: 60, type: "string", align: "center"}
                , {display: "采购订单号码", name: "ebeln", width: 150, minWidth: 60, type: "string", align: "center"}
                , {display: "采购订单类型", name: "bsart", width: 150, minWidth: 60, type: "string", align: "center"}
                , {display: "采购订单类型描述", name: "batxt", width: 150, minWidth: 60, type: "string", align: "center"}
                , {display: "供应商", name: "namel", width: 200, minWidth: 60, type: "string", align: "center"}
                , {display: "sap回执", name: "message", width: 200, minWidth: 60, type: "string", align: "center"}
                , {display: "创建日期", name: "fillinDate", width: 140, minWidth: 60, type: "string", align: "center"}

            ],
            dataAction: 'server',
            url: web_app.name + '/sapPurchaseOrder/slicedSapPurchaseOrderList.ajax?',
            //parms: {requireItemId: $("#prepareItemId").val()},
            toolbar: toolbarOptions,
            width: '99.8%',
            height: '100%',
            checkbox: true,
            pageSize: 100,
            heightDiff: -4,
            usePager: true,
            rownumbers: true,
            fixedCellHeight: true,
            onSuccess: function (data) {
                if (data.error) {
                    Public.tip(data.error);
                }
            }
            ,
            onDblClickRow: function (data, rowindex, rowobj) {
                viewHandler(data.id);
            }
        }
    );
    UICtrl.setSearchAreaToggle(purchaseOrderGridManager);
}

/**
 * 查看采购订单详情
 * @param id 采购订单id
 * @returns {boolean}
 */
function viewHandler(id) {
    if (!id) {
        var row = DataUtil.getSelectedRow(purchaseOrderGridManager);
        if (!row) {
            return row;
        }
        id = row.id;
    }
    UICtrl.addTabItem({
        tabid: 'viewPurchase' + id,
        text: '查看采购订单',
        url: web_app.name + '/sapPurchaseOrder/showSapPurchaseOrderDetail.job?bizId=' + id
    });
}



function reloadPurchaseGrid() {
    var params = $("#queryMainForm").formToJSON();
    UICtrl.gridSearch(purchaseOrderGridManager, params);
}

function query(obj) {
    var param = $(obj).formToJSON();
    UICtrl.gridSearch(purchaseOrderGridManager, param);
}

function resetForm(obj) {
    $(obj).formClean();
}

function printHandler(){
    var row = DataUtil.getUpdateRow(purchaseOrderGridManager);
    if (!row) {
        return;
    }
    var url='/sapPurchaseOrder/print.load';
    Public.openPostWindow(web_app.name+url,{id: row.id});
}

