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

$(function () {

    $(el.factoryCode).combobox({
        url: g.ctx + 'pc/sys/factory/rest/listAll',
        editable: false,
        valueField: 'code',
        textField: 'codeWithDesc',
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        onLoadSuccess: function () { //加载完成后,设置选中第一项
            var val = $(this).combobox("getData");
            for (var item in val[0]) {
                if (item === "code") {
                    $(this).combobox("select", val[0][item]);
                    loadGrid();
                }
            }
        }
    });

    // 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/product/rest/list',
        singleSelect: single === 1,
        pagination: true,
        pageSize: pager.pageSize,
        title: "<span style='color: red'>*请输入筛选条件，点击查询查找物料</span>",
        pageList: pager.pageList,
        height: el.bodyHeight - searchHeight - 20,
        rownumbers: true,
        queryParams: {},
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'materialCode',
                title: '物料编码',
                width: '10%'
            },
            {
                field: 'materialName',
                title: '物料名称',
                width: '25%',
                formatter: function (value) {
                    return '<span title="' + value + '">' + value + '</span>';

                }
            },
            {
                field: 'materialGroupCode',
                title: '物料组编码',
                width: '10%',
            },
            {
                field: 'materialGroupName',
                title: '物料组名称',
                width: '15%',
            },
            // {
            //     field: 'materialSpec',
            //     title: '规格',
            //     width: 100
            // },
            // {
            //     field: 'productName',
            //     title: '品名',
            //     width: 100
            // },
            // {
            //     field: 'brand',
            //     title: '品牌',
            //     width: '100'
            // },
            {
                field: 'basisUnit',
                title: '物料基本单位',
                width: '10%',
            },
            {
                field: 'orderUnit',
                title: '采购单位',
                width: '10%',
            },
            {
                field: 'factoryCode',
                title: '工厂编码',
                width: '9%',
            },
            {
                field: 'reqNum',
                title: '样品需求数量',
                width: '9%',
            }
        ]]
    });
}

/**
 * 数据查询
 */
function materialSearch() {
    var queryParams = {};
    queryParams.priceFlag = priceFlag;
    queryParams.purchaseGroupFlag = purchaseGroupFlag;
    queryParams.materialCode =  $.trim($(el.materialCode).val()).split(' ').join('|');
    queryParams.materialName = $.trim($(el.materialName).val());
    queryParams.materialGroupCode = $.trim($(el.materialGroupCode).val());
    queryParams.materialGroupName = $.trim($(el.materialGroupName).val());
    queryParams.factoryCode = $(el.factoryCode).combobox('getValue');
    $(el.tb).datagrid('options').queryParams = queryParams;
    $(el.tb).datagrid('options').url = g.ctx + 'pc/sys/product/rest/list';
    $(el.tb).datagrid('load');
}


/**
 * 数据查询重置
 */
function materialSearchReset() {
    $(el.materialCode).val('');
    $(el.materialName).val('');
    $(el.materialGroupCode).val('');
    $(el.materialGroupName).val('');
    // $(el.factory).combobox('setValue', '');

    materialSearch();
}

$(function () {
    document.addEventListener("keydown", function (event) {
        if (event.key === 'Enter') {
            materialSearch();
        }
    })
})
/**
 * 选择物料
 * @param call  选择物料数据的回调
 */
function chooseRows(call) {
    if (single) {
        var rows = $(el.tb).datagrid('getChecked');
        if (rows.length > 1) {
            topWin.layer.msg("请选中一条记录");
            return;
        }
        call(rows[0]);
    } else {
        var rows = $(el.tb).datagrid('getSelections');
        if (rows === null || rows === undefined) {
            topWin.layer.msg("请选中一条记录");
            return;
        }
        call(rows);
    }

}