/**
 * 多个采购订单查询条件弹框
 *
 * Created by li.kui on 2020/10/10.
 */
'use strict';
var el = {
    tb: '#tb',
    bodyHeight: document.documentElement.clientHeight
};


/**
 * 文档就绪
 */
$(function () {
    loadDataGird();
});


/**
 * 加载表格
 */
function loadDataGird() {
    $(el.tb).edatagrid({
        loadMsg: '数据加载中......',
        data: [{'poCode': '', 'poItemNo': ''}],
        height: el.bodyHeight - 10,
        singleSelect: true,
        rownumbers: true,
        pagination: false,
        queryParams: {},
        columns: [
            [
                {
                    field: 'poCode',
                    title: '采购订单',
                    width: 140,
                    editor: {
                        type: 'textbox',
                        options: {
                            onChange: function (nv) {
                                handleInputData(nv);
                            }
                        }
                    }
                },
                {
                    field: 'poItemNo',
                    title: '订单行号',
                    width: 80,
                    editor: {type: 'text'}
                }
            ]
        ],
        toolbar: [
            {
                text: '添加',
                iconCls: 'icon-add',
                handler: function () {
                    addItem();
                }
            },
            '-',
            {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    delItem();
                }
            }
        ]
    });
}


/**
 * 添加行
 */
function addItem() {
    $(el.tb).datagrid('appendRow',{
        poCode: '',
        poItemNo: ''
    });
}


/**
 * 删除行
 */
function delItem() {
    var row = $(el.tb).datagrid('getSelected');
    if(!row){
        layer.msg('请选择行');
    }

    var rowIndex = $(el.tb).datagrid('getRowIndex', row);
    $(el.tb).datagrid('deleteRow', rowIndex);
}


/**
 * 处理输入的数据
 *
 * @param data 输入的参数
 */
function handleInputData(data) {
    var list = data.split(' '), poData = [];
    for(var i = 0; i < list.length; i++){
        var item = list[i], obj = {};
        var itemArray = item.split('\t');

        if(itemArray[0] && itemArray[1]){
            obj.poCode = itemArray[0];
            obj.poItemNo = itemArray[1];
            poData.push(obj);
        }
    }

    if(poData.length !== 0){
        var row = $(el.tb).datagrid('getSelected');
        var rowIndex = $(el.tb).datagrid('getRowIndex', row);
        $(el.tb).datagrid('endEdit', rowIndex);
        $(el.tb).datagrid('deleteRow', rowIndex);

        for(var n = 0; n < poData.length; n++){
            $(el.tb).datagrid('insertRow', {
                index: rowIndex,
                row: {
                    poCode: poData[n].poCode,
                    poItemNo: poData[n].poItemNo
                }
            });

            rowIndex++;
        }
    }
}


/**
 * 确认查询参数
 *
 * @param callback 回调函数
 */
function confirmQueryParams(callback) {
    var rows = $(el.tb).datagrid('getRows');
    if(rows.length === 0){
        layer.msg('请添加查询参数');
        return false;
    }

    var isAllEmpty = true, result = [];
    for(var i = 0; i < rows.length; i++){
        $(el.tb).datagrid('endEdit', i);

        var item = rows[i];
        if(item.poCode){
            isAllEmpty = false;
        }

        var obj = {};
        obj.poCode = item.poCode;
        obj.poItemNo = item.poItemNo;
        result.push(obj);
    }

    if(isAllEmpty){
        layer.msg('请添加查询参数, SAP订单号必填');
        return false;
    }

    callback(result);
}