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();
        //     }}
    });
    gridManager = UICtrl.grid("#finaCustomerDataListGrid", {
        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: "creationGroup", width: 120, minWidth: 60, type: "string", align: "left"},
            {display: "客户编号", name: "buPartner", width: 120, minWidth: 60, type: "string", align: "left"},
            {display: "业务伙伴角色", name: "partnerRole", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "称谓代码", name: "titleMedi", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "组织名称1", name: "nameOrg1", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "搜索词1", name: "buSort1Txt", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "国家", name: "countryName", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "地区", name: "regionName", 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: "telNumber", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "移动电话号码", name: "mobNumber", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "传真号", name: "faxNumber", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "电子邮件地址", name: "smtpAddr", 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: 'finaCustomer',
        _local: true,
        url: web_app.name + '/finaCustomer/slicedFinaCustomerList.ajax',
        pageSize: 20,
        usePager: true,
        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){
            console.log(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: 'finaCustomerApply',
        text: "财务客户主数据申请",
        url: web_app.name + '/finaCustomer/forwardFinaCustomerApply.job'
    });
}

function doView(id,applyType) {
     var title="财务客户主数据申请详情";
    if (applyType=="-1"){
        title="财务客户主数据删除详情";
    }
    if (applyType=="1"){
        title="财务客户主数据修改详情";
    }
    UICtrl.addTabItem({
        tabid: 'finaCustomer'+id,
        text: title,
        url: web_app.name + '/finaCustomer/showFinaCustomer.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 + '/finaCustomer/forwardFinaCustomerApply.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 + '/finaCustomer/forwardFinaCustomerApply.job?bizId='+result.id + '&flag='+'extend'
    });
}


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