/**
 * 物料管理首页JS
 *
 * Modify by likui on 2019/11/11.
 */
'use strict';
var el = {
    firstLevelPurchaseCode: '#firstLevelPurchaseCode',
    firstLevelPurchaseName: '#firstLevelPurchaseName',
    secondLevelPurchaseCode: '#secondLevelPurchaseCode',
    secondLevelPurchaseName: '#secondLevelPurchaseName',
    searchForm: '#searchForm',
    materialCode: '#materialCode',
    materialName: '#materialName',
    sourcePurchaseGroupCode: '#sourcePurchaseGroupCode',
    materialGroupCode: '#materialGroupCode',
    materialGroupName: '#materialGroupName',
    factory: '#factory',
    xbjFlag: '#xbjFlag',
    tb: '#tb',
    frmExcel: '#frmExcel',
    syncSapDialog: null,
    bodyHeight: document.documentElement.clientHeight
};

$(function () {
    loadGrid();
});


/**
 * 加载数据表格
 */
function loadGrid() {
    var searchHeight = $(el.searchForm).outerHeight() - 10;
    var pager = initGridPager(el.bodyHeight - searchHeight * 2 - 120, 40);
    var toolbar = [
        {
            text: '导入',
            iconCls: 'icon-import',
            handler: function () {
                dataImport();
            }
        }, '-',
        {
            text: "导出",
            id: 'btn_print',
            iconCls: 'icon-download',
            handler: function () {
                exportVData();

            }
        }, '-',
        {
            text: "禁用",
            id: 'btn_delete',
            iconCls: 'icon-remove',
            handler: function () {
                deleteData(2);

            }
        }, '-',
        {
            text: "启用",
            id: 'btn_delete',
            iconCls: 'icon-remove',
            handler: function () {
                deleteData(1);

            }
        }
    ]
    if (select === 1) {
        toolbar = []
    }
    $(el.tb).datagrid({
        loadMsg: '数据加载中......',
        url: g.ctx + 'pc/sys/purchaseCategory/rest/list?inCodes=' + inCodes + "&notInCodes=" + notInCodes,
        singleSelect: true,
        pagination: true,
        // pageSize: pager.pageSize,
        // pageList: pager.pageList,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 40, 50],
        height: el.bodyHeight - searchHeight - 20,
        rownumbers: true,
        queryParams: {},
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'status',
                title: '数据状态',
                width: '8%',
                formatter: function (value) {
                    if (value == 1) {

                        return '<a style="color:green;">' + "正常" + '</a>'

                    } else {
                        return '<a style="color:red;">' + "禁用" + '</a>'
                    }
                }
            },
            {
                field: 'firstLevelPurchaseCode',
                title: '一级采购编码',
                width: '8%',
            },
            {
                field: 'firstLevelPurchaseName',
                title: '一级采购名称',
                width: '15%',
            },
            {
                field: 'secondLevelPurchaseCode',
                title: '二级采购编码',
                width: '8%',
            },
            {
                field: 'secondLevelPurchaseName',
                title: '二级采购名称',
                width: '15%',
            },
            {
                field: 'createTime',
                title: '创建日期',
                width: '12%',
                formatter: function (value) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD HH:mm:SS');
                    }
                    return '';
                }
            }, {
                field: 'createUserName',
                title: '创建人名称',
                width: '8%',
            }

        ]],
        toolbar: toolbar
    });
}

/**
 * 禁用/启用
 */
function deleteData(status) {
    var rows = $(el.tb).datagrid("getSelections");
    if (rows.length == 0) {
        topWin.layer.msg("请选择数据", {icon: 0});
        return;
    }
    var loading = topWin.layer.msg('数据处理中, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
    $.ajax({
        url: '/pc/sys/purchaseCategory/rest/disable',
        method: "post",
        data: {
            id: rows[0].id,
            status: status
        },
        success: function () {
            topWin.layer.close(loading);
            topWin.layer.msg("操作成功", {icon: 1});
            $(el.tb).datagrid('reload');
        },
        error: function (data) {
            topWin.layer.close(loading);
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
        }
    });

}

/**
 * 导出入库凭证数据
 */
function exportVData() {
    layer.confirm('确定导出?', function (index) {
        document.getElementById("searchForm").action = '/product/purchase/category/export';
        document.getElementById("searchForm").submit();
        layer.close(index)
    });


}

/**
 * 数据导入
 */
function dataImport() {

    // var a = openImportDialog("purchase_category_export",'采购品类导入',function(fileId){
    //     //取到fileId然后进行数据导入操作
    //     $.ajax({
    //         url: g.ctx + 'pc/sys/dict/item/importData',
    //         method: "POST",
    //         data: {fileId: fileId, dictTypeId: $('#id').val()},
    //         success: function (data) {
    //             topWin.layer.alert("导入成功", {icon: 1}, function (idx) {
    //                 topWin.layer.close(idx);
    //                 a.dialog('close');
    //                 $('#tb').datagrid('reload');
    //             });
    //         },
    //         error: function (data) {
    //             topWin.layer.alert(data.responseJSON.message, {icon: 2});
    //         }
    //     });
    // });
}

/**
 * 导出excel
 */
function exportExcel() {
    layer.confirm('确定导出?', function (index) {
        document.getElementById("frmExcel").action = g.ctx + 'product/purchase/category/export.do';
        document.getElementById("frmExcel").submit();
        layer.close(index)
    });
}


/**
 * 同步ERP物料
 */
function syncSap() {
    var url = g.ctx + 'product/syncUI';
    el.syncSapDialog = openDialog('syncSap', url, {
        title: '同步ERP物料主数据信息',
        width: 500,
        height: 500,
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.syncSapDialog.dialog('destroy');
        },
        buttons: [{
            text: '同步',
            handler: function () {
                topWin.frames['syncSap'].syncSapResult(function () {
                    $(el.tb).datagrid('load');
                    el.syncSapDialog.dialog('close');
                });
            }
        }, {
            text: '关闭',
            handler: function () {
                el.syncSapDialog.dialog('close');
            }
        }]
    });
}


/**
 * 数据查询
 */
function materialSearch() {
    var queryParams = {};
    queryParams.firstLevelPurchaseCode = $.trim($(el.firstLevelPurchaseCode).val());
    queryParams.firstLevelPurchaseName = $.trim($(el.firstLevelPurchaseName).val());
    queryParams.secondLevelPurchaseCode = $.trim($(el.secondLevelPurchaseCode).val());
    queryParams.secondLevelPurchaseName = $.trim($(el.secondLevelPurchaseName).val());
    $(el.tb).datagrid('options').queryParams = queryParams;
    $(el.tb).datagrid('load');
}


/**
 * 数据查询重置
 */
function materialSearchReset() {
    $(el.firstLevelPurchaseCode).val('');
    $(el.firstLevelPurchaseName).val('');
    $(el.secondLevelPurchaseCode).val('');
    $(el.secondLevelPurchaseName).val('');
    materialSearch();
}


/**
 * 选择物料
 * @param callback  选择物料数据的回调
 */
function chooseRow(callback) {
    var row = $(el.tb).datagrid('getSelected');
    callback(row);
}