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

$(function () {



    $(el.supplierState).combobox({
        valueField: 'code',
        textField: 'name',
        limitToList: 'true',
        data: [
            {'name': '注册', 'code': '1'},
            {'name': '潜在', 'code': '2'},
            {'name': '合格', 'code': '6'},
            {'name': '临时', 'code': '4'},
            {'name': '小批量', 'code': '7'},
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
    });
    initDict('supplier_attr')
    //订单类型
    $("#attr").combobox({
        valueField: 'code',
        textField: 'name',
        panelHeight: 'auto',
        editable: false,
        data: getDictList('supplier_attr')
    })

    //采购组
    $("#purchaseGroupCode").combobox({
        valueField: 'purchaseGroupCode',
        textField: 'codeWithDesc',
        limitToList: 'true',
        method: 'POST',
        prompt: '--- 请选择 ---',
        url: g.ctx + 'pc/sys/purchaseGroup/rest/listAll',
    });
    loadGrid();
});


/**
 * 加载数据表格
 */
function loadGrid() {



  //  initDict('supplier_attr')
    var searchHeight = $(el.searchForm).outerHeight() - 10;
    var pager = initGridPager(el.bodyHeight - searchHeight * 2 - 120, 40);

    var columns = [];
    if (type===2){
        columns = [
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'supplierState',
                title: '供应商状态',
                width: '20%',
                align: 'left',
                formatter: function (value) {
                    switch (value) {
                        case 1:
                            return '注册';
                        case 2:
                            return '潜在';
                        case 3:
                            return '正式';
                        case 4:
                            return '临时';
                        case 5:
                            return '退出';
                        case 6:
                            return '合格';
                        case 7:
                            return '小批量';
                        default:
                            return '初始';
                    }
                }
            },
            {
                field: 'username',
                title: '供应商名称',
                width: '40%'
            },
            {
                field: 'accountName',
                title: '供应商账号',
                width: '39%'
            },
        ]
    }else {
        columns = [
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'username',
                title: '用户名称',
                width: '50%'
            },
            {
                field: 'accountName',
                title: '用户账号',
                width: '49%'
            }
        ]
    }

    $(el.tb).datagrid({
        loadMsg: '数据加载中......',
        url: g.ctx + 'pc/message/launch/rest/user/list?type='+type,
        singleSelect: false,
        pagination: true,
        pageSize: 2000,
        pageList: [2000, 3000],
        height: el.bodyHeight - searchHeight - 20,
        rownumbers: true,
        queryParams: {},
        loadFilter: pagerFilter,
        columns: [columns]
    });
}
/**
 * 选择
 * @param call  选择数据的回调
 */
function chooseRow(call) {
    var rows = $(el.tb).datagrid('getSelections');
    if (rows === null || rows === undefined) {
        topWin.layer.msg("请选中一条记录");
        return;
    }
    call(rows);

}

function pagerFilter(data) {
    if (typeof data.length == 'number' && typeof data.splice == 'function')     {   // is array
        data = {
            total: data.length,
            rows: data
        }
    }
    var dg = $(this);
    var opts = dg.datagrid('options');
    var pager = dg.datagrid('getPager');
    pager.pagination({
        onSelectPage: function (pageNum, pageSize) {
            opts.pageNumber = pageNum;
            opts.pageSize = pageSize;
            pager.pagination('refresh', {
                pageNumber: pageNum,
                pageSize: pageSize
            });
            dg.datagrid('loadData', data);
        }
    });
    if (!data.originalRows) {
        data.originalRows = (data.rows);
        el.originalRows = (data.rows);
    }
    var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
    var end = start + parseInt(opts.pageSize);
    data.rows = (data.originalRows.slice(start, end));
    return data;
}