/**
 * 对账模块-选择物料数据(外协盘点)JS
 *
 * Created by li.kui on 2020/11/20.
 */
'use strict';
var el = {
    factoryCode: '#factoryCode',
    matCodes: '#matCodes',
    singleSelect: '#singleSelect',
    materialCode: '#materialCode',
    materialDesc: '#materialDesc',
    mgCodeName: '#mgCodeName',
    searchForm: '#searchForm',
    tb: '#tb',
    bodyHeight: document.documentElement.clientHeight
};

$(function () {
    $(el.materialCode).keydown(function(e) {
        if (e.keyCode === 13) {
            searchData();
        }
    });

    $(el.materialDesc).keydown(function(e) {
        if (e.keyCode === 13) {
            searchData();
        }
    });

    var factoryCode = $(el.factoryCode).val(), matCodes = $(el.matCodes).val(), singleSelect = $(el.singleSelect).val();
    initMaterialDataGrid(factoryCode, matCodes, singleSelect);
});

/**
 * 初始化供应商数据列表
 *
 * @param factoryCode 工厂编码
 * @param matCodes 已选择的物料编码
 * @param singleSelect 是否单选 0:否 1:是
 */
function initMaterialDataGrid(factoryCode, matCodes, singleSelect) {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);

    $(el.tb).datagrid({
        loadMsg: '数据加载中......',
        emptyMsg: '暂无数据',
        url: g.ctx + 'fin/common/rest/chose/material/list',
        singleSelect: singleSelect !== '0',
        height: el.bodyHeight - searchHeight,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        idField: 'materialCode',
        rownumbers: true,
        queryParams: {
            factoryCode: factoryCode
        },
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'materialCode',
                    title: '物料编码',
                    width: 90
                },
                {
                    field: 'materialDesc',
                    title: '物料描述',
                    width: 360
                },
                {
                    field: 'mgCode',
                    title: '物料组编码',
                    width: 90
                },
                {
                    field: 'mgName',
                    title: '物料组名称',
                    width: 180
                },
                {
                    field: 'materialUnit',
                    title: '单位',
                    width: 70
                }
            ]
        ],
        onLoadSuccess: function (data) {
            var rows = data.rows;
            if(rows.length === 1){
                $(el.tb).datagrid('selectRow', 0);
            }else {
                var matCodeArray = matCodes.split(',');
                for(var i = 0; i < matCodeArray.length; i++){
                    for(var j = 0; j < rows.length; j++){
                        if(matCodeArray[i] === rows[j].materialCode){
                            $(el.tb).datagrid('selectRow', j);
                        }
                    }
                }
            }
        }
    });
}


/**
 * 查询数据
 */
function searchData() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.factoryCode = $(el.factoryCode).val();
    queryParams.materialCode = $.trim($(el.materialCode).val());
    queryParams.materialDesc = $.trim($(el.materialDesc).val());
    queryParams.mgCodeName = $.trim($(el.mgCodeName).val());
    $(el.tb).datagrid('load');
}


/**
 * 确认选择
 */
function confirmChose(callback) {
    var singleSelect = $(el.singleSelect).val(), result = [];
    if (singleSelect === '0') {
        var rows = $(el.tb).datagrid('getSelections');
        result = result.concat(rows);
    } else {
        var row = $(el.tb).datagrid('getSelected');
        result.push(row);
    }
    callback(result);
}