var gridManager = null;
var refreshFlag = false;

$(document).ready(function () {
    initializateUI()
    loadSuppliersDataListGrid();
})
function initializateUI() {
    UICtrl.layout("#layout", {leftWidth: 3});
}
function loadSuppliersDataListGrid() {

    var toolbarOptions = UICtrl.getDefaultToolbarOptions({
        addHandler: function (){
            addHandler()
        },
        updateHandler: showUpdateDialog,
        extendHandler: {id: 'extendHandler', text: '扩展', img: 'fa-music', className: 'btn-gray',click: function () {
                extendHandler();
            }}
        ,
        impExcelDataHandler:{id:'impExcelData',img:'fa-table',text:'导入数据',click:impExcelDataHandler},
       /* testHandler: {id: 'basicInfoMaintenance', text: '基本信息维护', img: 'fa-music', className: 'btn-gray',
            click: function () {

                let result=DataUtil.getUpdateRow(gridManager);
                if(result.status!==3){
                    Public.tip("请选择一条已审核后数据！")
                    return false;
                }

                UICtrl.addTabItem({
                    tabid: 'showNonProdApplyNext'+result.id,
                    text: "非产品/非半成品类物料主数据维护申请详情",
                    url: web_app.name + '/sapNonProdApplyNext/forwardNonProdApplyNext.do?bizId='+result.id
                });
            }
        }*/
    });
    gridManager = UICtrl.grid("#suppliersDataListGrid", {
        columns: [
            {display: "申请单编号", name: "billCode", width: 120, minWidth: 60, type: "string", align: "left"},
            {display: "申请状态", name: "statusTextView", width: 60, minWidth: 60, type: "string", align: "left"},
            {display: "申请日期", name: "fillinDate", width: 120, minWidth: 60, type: "string", align: "left"},
            {display: "操作类型", name: "applyTypeTextView", width: 80, minWidth: 60, type: "string", align: "left"},
            {display: "业务伙伴分组", name: "buGroupTextView", width: 200, minWidth: 60, type: "string", align: "left"},
            {display: "业务伙伴编号", name: "partner", width: 120, minWidth: 60, type: "string", align: "left"},
            {display: "组织名称1", name: "nameOrg1", width: 200, minWidth: 60, type: "string", align: "left"},
            {display: "组织名称2", name: "nameOrg2", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "组织名称3", name: "nameOrg3", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "组织名称4", name: "nameOrg4", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "业务伙伴的搜索词1", name: "buSort1", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "业务伙伴的搜索词2", name: "buSort2", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "付款条件", name: "payConditionTextView", width: 140, minWidth: 60, type: "string", align: "left"},
           // {display: "适用标准", name: "applicableStandard", width: 140, minWidth: 60, type: "string", align: "left"},
            //{display: "基本计量单位", name: "baseUnitTextView", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "国家", name: "land1Name", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "地区", name: "regionName", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "城市", name: "city1Name", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "街道", name: "street", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "邮编", name: "postCode1", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "税收编号", name: "stenr", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "申请人", name: "personMemberName", width: 120, minWidth: 60, type: "string", align: "left"},
            {display: "申请部门", name: "deptName", width: 120, minWidth: 60, type: "string", align: "left"},

        ],
        dataAction: "server",
        _tableKey: 'sapSuppliersData',
        _local: true,
        url: web_app.name + '/sapSuppliersData/slicedSuppliersDataList.ajax',
        pageSize: 20,
        usePager: true,
        sortName: 'fillinDate',
        sortOrder: 'desc',
        toolbar: toolbarOptions,
        width: "100%",
        height: "100%",
        heightDiff: -8,
        checkbox: true,
        async: false,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
        onDblClickRow: function (data, rowindex, rowobj) {
            doView(data.id,data.applyType);
        },
        onAfterShowData: function (data){
        }
    });
    UICtrl.setSearchAreaToggle(gridManager);
}


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

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




//删除按钮
function deleteHandler(id) {
   // var rows = UICtrl.checkSelectedRows(gridManager);
   /* var rows = gridManager.getSelectedRows();
    var statusFlag="1";
    for (var i=0;i<rows.length;i++){
        if  (rows[i].status!="0"){
            statusFlag="0";
            break;
        }
    }
    if (statusFlag=="0"){
        UICtrl.alert("申请状态下的数据，才能删除！");
        return false;
    } else {
        DataUtil.del({
            action: 'sapSuppliersData/deleteSuppliersData.ajax',
            gridManager: gridManager, idFieldName: 'id',
            onSuccess: function () {
                reloadGrid();
            }
        });
    } */
    let result=DataUtil.getUpdateRow(gridManager);
    if (result.applyType=="-1"){
        Public.tip("已经删除的不能再次操作！")
        return false;
    }
    if(result.status!==3){
        Public.tip("请选择一条审核完成后的数据！")
        return false;
    }
    UICtrl.addTabItem({
        tabid: 'suppliersDataDele'+id,
        text: "供应商主数据删除",
        url: web_app.name + '/sapSuppliersData/forwardSuppliersDataApply.job?bizId='+result.id+"&isDel=1"
    });
}

function addHandler() {
    UICtrl.addTabItem({
        tabid: 'suppliersDataApply',
        text: "供应商主数据维护申请",
        url: web_app.name + '/sapSuppliersData/forwardSuppliersDataApply.job'
    });
}

function doView(id,applyType) {
     var title="供应商主数据申请详情";
    if (applyType=="-1"){
        title="供应商主数据删除详情";
    }
    if (applyType=="1"){
        title="供应商主数据修改详情";
    }
    UICtrl.addTabItem({
        tabid: 'suppliersData'+id,
        text: title,
        url: web_app.name + '/sapSuppliersData/showSuppliersData.job?isReadOnly=true&bizId='+id+"&isDetailPage=true"
    });
}
function initShowDialog() {
    var mainFileTable = $('#compensateLibraryList').fileList();
    mainFileTable.find('table').css({
        borderTopWidth: 0
    });
}
// 进行修改操作
function showUpdateDialog(id) {
       /* UICtrl.showAjaxDialog({
            title: "修改供应商主数据申请",
            url: web_app.name + '/sapSuppliersData/showSuppliersData.load',
            param: {
                id: id
            },
            width: 800,
            init: initShowDialog,
            ok: doSaveDefine,
            start: false,
            stop: false,
            close: onDialogCloseHandler
        }); */
    let result=DataUtil.getUpdateRow(gridManager);
    if (result.applyType=="-1"){
        Public.tip("已经删除的不能再修改！")
        return false;
    }
    if(result.status!==3){
        Public.tip("请选择一条审核完成后数据！")
        return false;
    }
      UICtrl.addTabItem({
        tabid: 'suppliersDataModi'+id,
        text: "供应商主数据修改详情",
        url: web_app.name + '/sapSuppliersData/forwardSuppliersDataApply.job?bizId='+result.id+ '&flag='+'update'
      });
    }


// function next(){
//     UICtrl.addTabItem({
//         tabid: 'showNonProdApplyNext',
//         text: "非产品/非半成品类物料主数据维护申请详情",
//         url: web_app.name + '/sapNonProdApply/forwardNonProdApplyNext.do?isDetailPage=true'
//     });
// }

function extendHandler(){
    let result=DataUtil.getUpdateRow(gridManager);
    if (result.applyType=="-1"){
        Public.tip("已经删除的不能再扩展！")
        return false;
    }
    if(result.status!==3){
        Public.tip("请选择一条审核完成后数据！")
        return false;
    }
    UICtrl.addTabItem({
        tabid: 'suppliersDataModi'+result.id,
        text: "供应商主数据扩展详情",
        url: web_app.name + '/sapSuppliersData/forwardSuppliersDataApply.job?bizId='+result.id + '&flag='+'extend'
    });
}


// 关闭对话框
function onDialogCloseHandler() {
    if (refreshFlag) {
        reloadGrid();
        refreshFlag = false;
    }
}

function impExcelDataHandler(){
    ExcelImpManager.showImpDialog('导入', 'suppliersData', Public.getUUID());
}