/**
 * 物料管理首页JS
 *
 * Modify by likui on 2019/11/11.
 */
'use strict';
var el = {
    tb: '#tb',
    bodyHeight: document.documentElement.clientHeight
};

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


/**
 * 加载数据表格
 */
function loadGrid() {
    var searchHeight = $(el.searchForm).outerHeight() - 10;
    var pager = initGridPager(el.bodyHeight - searchHeight * 2 - 120, 40);

    $(el.tb).datagrid({
        loadMsg: '数据加载中......',
        url: g.ctx + 'pc/sys/purchaseGroup/rest/factoryList',
        singleSelect: false,
        method: "GET",
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        height: el.bodyHeight - searchHeight - 20,
        rownumbers: true,
        queryParams: {},
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'deleteFlag',
                title: '数据状态',
                width: 80,
                formatter: function (value) {
                    if (value == 0) {

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

                    } else {
                        return '<a style="color:red;">' + "禁用" + '</a>'
                    }
                }
            },
            {
                field: 'materialGroupCode',
                title: '物料组编码',
                width: 120
            },
            {
                field: 'materialGroupName',
                title: '物料组名称',
                width: 200
            },
            {
                field: 'factoryCode',
                title: '工厂编码',
                width: 120
            },
            {
                field: 'factoryName',
                title: '工厂名称',
                width: 200
            },
            {
                field: 'purchaseGroupCode',
                title: '采购组编码',
                width: 120
            },
            {
                field: 'purchaseGroupName',
                title: '采购组名称',
                width: 200
            },
            {
                field: 'createTime',
                title: '创建日期',
                width: 150,
                formatter: function (value) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD HH:mm:SS');
                    }
                    return '';
                }
            }
        ]],
        toolbar: [
            {
                text: '导入',
                iconCls: 'icon-import',
                handler: function () {
                    importData()
                }
            }, '-',
            {
                text: "导出",
                id: 'btn_print',
                iconCls: 'icon-download',
                handler: function () {
                    topWin.location.href = g.ctx + 'pc/sys/purchaseGroup/rest/factoryExportData?' + searchQueryParams()
                }
            }, '-',
            {
                text: "禁用",
                id: 'btn_delete',
                iconCls: 'icon-remove',
                handler: function () {
                    deleteData(1)
                }
            }, '-',
            {
                text: "启用",
                id: 'btn_delete',
                iconCls: 'icon-remove',
                handler: function () {
                    deleteData(0)
                }
            }
        ]
    });
}

function importData() {
    var a = openImportExcelDialog("static/excel/purchase_group_factory_template.xlsx", '采购组工厂数据导入', function (file) {
        let formData = new FormData();
        formData.append("file", file);
        //取到fileId然后进行数据导入操作
        $.ajax({
            url: g.ctx + 'pc/sys/purchaseGroup/rest/factoryImportData',
            method: "POST",
            contentType: false,
            processData: false,
            data: formData,
            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});
            }
        });
    });
}

/**
 * 禁用/启用
 */
function deleteData(status) {
    var rows = $(el.tb).datagrid("getChecked");
    if (rows.length <= 0) {
        topWin.layer.msg("请选择数据", {icon: 0});
        return
    }
    var title = '确认要' + (status == 1 ? "禁用" : '启用') + '?';
    topWin.layer.confirm(title, function (index) {
        var idArray = rows.map(function (obj) {
            return obj.id; // 提取每个对象的 id 属性值
        });
        var loading = topWin.layer.msg('数据处理中, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
        $.ajax({
            url: g.ctx + 'pc/sys/purchaseGroup/rest/updateFactoryStatus',
            method: "post",
            data: {
                ids: idArray.join(','),
                status: status
            },
            success: function () {
                topWin.layer.close(loading);
                topWin.layer.close(index);
                topWin.layer.msg("操作成功", {icon: 1});
                $(el.tb).datagrid('reload');
            },
            error: function (data) {
                topWin.layer.close(loading);
                topWin.layer.close(index);
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    })
}

