/**
 * 对账管理-供应商选择弹框
 *
 * Created by li.kui on 2019/12/10.
 */
'use strict';
var el = {
    supCodes: '#supCodes',
    singleSelect: '#singleSelect',
    restrictType: '#restrictType',
    companyCode: '#companyCode',
    supplierCode: '#supplierCode',
    supplierName: '#supplierName',
    parentFrameName: '#parentFrameName',
    tb: '#tb'
};

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

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



    var singleSelect = $(el.singleSelect).val();
    var supCodes = $(el.supCodes).val();
    initSupDataGrid(singleSelect, supCodes);
});

/**
 * 初始化供应商数据列表
 *
 * @param singleSelect 是否单选 0:否 1:是
 * @param supCodes 已选择的供应商编码
 */
function initSupDataGrid(singleSelect, supCodes) {
    $(el.tb).datagrid({
        loadMsg: DZGL.CGDZDGL_SJYCZRZ,
        url: g.ctx + 'fin/common/rest/chose/supplier/list',
        queryParams: {
            restrictType: $(el.restrictType).val(),
            companyCode: $(el.companyCode).val(),
        },
        singleSelect: singleSelect !== '0',
        pagination: true,
        pageNumber: 1,
        pageSize: 10,
        pageList: [10, 20, 30, 40, 50],
        checkbox: true,
        height: 400,
        idField: 'supplierCode',
        rownumbers: true,
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'supplierCode',
                    title: DZGL.CGDZDGL_GYSBM,
                    width: 120
                },
                {
                    field: 'shortName',
                    title: DZGL.DZGL_GYSJC,
                    width: 150
                },
                {
                    field: 'supplierName',
                    title: DZGL.CGDZDGL_GYSMC,
                    width: 300
                }
            ]
        ],
        onLoadSuccess: function (data) {
            var rows = data.rows;
            if(rows.length === 1){
                $(el.tb).datagrid('selectRow', 0);
            }else {
                var supCodeArray = supCodes.split(',');
                for(var i = 0; i < supCodeArray.length; i++){
                    for(var j = 0; j < rows.length; j++){
                        if(supCodeArray[i] === rows[j].supplierCode){
                            $(el.tb).datagrid('selectRow', j);
                        }
                    }
                }
            }
        },
        onDblClickRow: function (index, row) {
            var parentFrameName = $(el.parentFrameName).val();
            if(parentFrameName){
                var parentPage = self.parent.frames[parentFrameName];
                var result = {};
                result.supplierId = row.id;
                result.supplierCode = row.supplierCode;
                result.supplierName = row.supplierName;
                parentPage.choseSupDialogConfirm(result);
            }

        }
    });
}


/**
 * 查询数据
 */
function searchData() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.supplierCode = $.trim($(el.supplierCode).val());
    queryParams.supplierName = $.trim($(el.supplierName).val());
    queryParams.restrictType = $(el.restrictType).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);
}