import request from '@/utils/request'
import { warehouse } from "./warehouse";
import { bldg } from "./bldg";
import { warehouseRoom } from "./warehouseRoom";
import { warehouseArea } from "./warehouseArea";
import { warehouseLogic } from "./warehouseLogic";
import { warehouseLocator } from "./warehouseLocator";
import { selfWarehouseLocator } from "./selfWarehouseLocator"
import { stage } from "./stage";
import { station } from "./station";
import { plat } from "./plat";
import { container } from "./container";
import { container_inbound } from "./container_inbound";
import { warehouseAuth } from "./warehouse-auth";
import { branchAuth } from "./branch-auth";
import { cargoOwnerAuth } from "./cargoowner-auth";
import { supplier } from "./supplier";
import { customer } from "./customer";
import { item } from "./item";
import { itemcuring } from "./itemcuring";
import { itemRelated } from "./itemRelated";
import { delegationParty } from "./delegationParty";

import { workercode } from "./workercode";
import { manufacturerLotNum } from "./manufacturerLotNum";
import { locatorCode } from "./locators";
import { proxiesCode } from "./proxies";

import { diffOption } from "../utils/index";
import { message } from 'ant-design-vue';

let defLayout = { span: 6, labelCol: { span: 8 }, wrapperCol: {} };

/*
type: 1表单(返回id code name的数组)  2表格(返回对象)
required: boolean  是否必填
isMulSelect: boolean 是否多选
remoteBeforeFun: remoteBefore回调
changeFun: change回调   form中入参(modalRowData, form)    table-edit中入参(modalRowData, index, value, record)
layout: 栅格配置
extendConfig: 额外的配置随时加入
   -----search(p1, p2, p3, p4, p5)
   -----mapper: {k1: k2} 将k1映射成k2    示例: cargoOwnerId:  mtlCargoOwnerId
   -----label: string  表单的label
*/

/* 加载字典表 */
diffOption(warehouseRoom.defTableCol).then(res => warehouseRoom.defTableCol = res);
diffOption(warehouseLogic.defTableCol).then(res => warehouseLogic.defTableCol = res);
diffOption(warehouseLocator.defTableCol).then(res => warehouseLocator.defTableCol = res);
diffOption(selfWarehouseLocator.defTableCol).then(res => selfWarehouseLocator.defTableCol = res);
diffOption(warehouseArea.defTableCol).then(res => warehouseArea.defTableCol = res);
diffOption(warehouse.defTableCol).then(res => warehouse.defTableCol = res);
diffOption(container.defTableCol).then(res => container.defTableCol = res);
diffOption(container_inbound.defTableCol).then(res => container_inbound.defTableCol = res);

/* 加载字典表 */

// 仓-----仓库
export function genWarehouseSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, warehouse);
}
// 仓-----建筑物
export function genBldgSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, bldg);
}
// 仓-----库房
export function genWarehouseRoomSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, warehouseRoom);
}
// 仓-----库区
export function genWarehouseAreaSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, warehouseArea);
}
// 仓-----逻辑区
export function genWarehouseLogicSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, warehouseLogic);
}
// 仓-----货位
export function genWarehouseLocatorSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, warehouseLocator);
}
// 上架-----货位(上架实际货位接口)
export function genSelfWarehouseLocator(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, selfWarehouseLocator);
}
// 仓-----月台
export function genPlatSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, plat);
}
// 仓-----集货区
export function genStageSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, stage);
}
// 仓-----工作站
export function genStationSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, station);
}
// 仓-----容器
export function genContainerSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, container);
}

export function genContainerSearchModal_InBound(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, container_inbound);
}

// 仓-----仓库(与账户权限关联)
export function genWarehouseAuthSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, warehouseAuth);
}
// 仓-----OMS仓库(与账户权限关联)
export function genBranchAuthSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, branchAuth);
}
// 仓-----货主(与账户权限关联)
export function genCargoownerAuthSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, cargoOwnerAuth);
}
// 仓、货主-----供应商
export function genSupplierSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, supplier);
}
// 仓、货主-----客户
export function genCustomerSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, customer);
}
// 仓、货主-----商品
export function genItemSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, item);
}
// 养护商品查询
export function genItemSearchModalcuring(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, itemcuring);
}
// 商品查询
export function genItemRelatedSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, itemRelated);
}
// 仓-----受托生产企业
export function genDelegationPartySearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, delegationParty);
}

// 工号/员工
export function genWorkercodeSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, workercode);
}

// 批号
export function genManufacturerLotNumSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, manufacturerLotNum);
}

// 商品货位列表
export function genLocatorCodeSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, locatorCode);
}
// 代管库位
export function genProxiesCodeSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig) {
    return genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout || defLayout, extendConfig, proxiesCode);
}

// 根据id code name 查询
export const searchModalItemList = {
    warehouse: warehouse,
    bldg: bldg,
    warehouseRoom: warehouseRoom,
    warehouseArea: warehouseArea,
    warehouseLogic: warehouseLogic,
    warehouseLocator: warehouseLocator,
    selfWarehouseLocator: selfWarehouseLocator,
    stage: stage,
    station: station,
    plat: plat,
    container: container,
    warehouseAuth: warehouseAuth,
    branchAuth: branchAuth,
    cargoOwnerAuth: cargoOwnerAuth,
    supplier: supplier,
    customer: customer,
    item: item,
    delegationParty: delegationParty,
    workercode: workercode,
};
export function querySearchModalItem(searchModalItem, params) {
    let p = {
        method: searchModalItem.remoteUrl.method,
        url: searchModalItem.remoteUrl.url,
    }
    if (searchModalItem.remoteUrl.method == 'get') {
        p.params = params;
    } else {
        p.data = params;
    }
    return request(p);
}


function genSearchModal(type, required, isMulSelect, remoteBeforeFun, changeFun, layout, extendConfig, config) {
    let tableConfig = {
        columns: [...config.defTableCol],
        hasRowSelectCheck: true,
        rowSelectType: isMulSelect ? 2 : 1,
    };
    if (typeof remoteBeforeFun == 'function') {
        tableConfig.remoteBefore = remoteBeforeFun;
    }
    let fuzzyQueryBuff = null; // { value: value, type: 1|2, form: form, record: record }
    let searchConfig = {
        type: type == 1 ? 'searchModal' : 'search',
        ...config.defSearchConfig,
        ...layout,
        remoteUrl: config.remoteUrl,
        required: required,
        toolBar: config.defToolBar,
        tableConfig: tableConfig,
        on: {
            search(p1, p2, p3, p4, p5) {
                if (extendConfig && typeof extendConfig.search == 'function') {
                    extendConfig.search(p1, p2, p3, p4, p5);
                }
            }
        }
    };
    if (extendConfig && extendConfig.enableFuzzyQuery) {
        searchConfig.targetClick = false;
        searchConfig.on.blur = function (event) {
            if (!fuzzyQueryBuff) return;
            let key = searchConfig.targetValueKey;
            // mapper key
            if (extendConfig.mapper && extendConfig.mapper[key]) {
                key = extendConfig.mapper[key];
            }
            let remote = { ...config.remoteUrl };
            let p = {
                pageNum: 1, pageSize: 15,
                [key]: fuzzyQueryBuff.value // 未映射
            };
            if (Array.isArray(extendConfig.enableFuzzyQuery) && fuzzyQueryBuff.type == 2) {
                extendConfig.enableFuzzyQuery.forEach(k => {
                    p[k] = fuzzyQueryBuff.record[k];
                });
            }
            if (remote.method == 'post') { remote.data = p; }
            else { remote.params = p; }
            request(remote).then(res => {
                // return
                if (!res.success) {
                    return
                }
                if (res.data.list.length == 0) {
                    // message.warning('没有匹配到数据');
                } else {
                    // message.warning('成功匹配到数据');
                    // let data = res.data.list[0];
                    if (fuzzyQueryBuff.type == 1) {
                        let val = {};
                        config.defTableCol.forEach(item => {
                            let key1 = item.dataIndex;
                            let key2 = item.dataIndex;
                            if (config.mapper) {
                                key2 = config.mapper[key1];
                            }
                            val[key2] = data[key1];
                        });
                        // console.log('val', val)
                        if (extendConfig && extendConfig.mapper) {
                            let nval = {};
                            /* 映射配置--开始 */
                            Object.keys(val).map(k => {
                                let k1 = extendConfig.mapper[k];
                                if (!!k1) nval[k1] = val[k];
                                else nval[k] = val[k];
                            });
                            /* 映射配置--结束 */
                            // console.log('nval', nval)
                            // fuzzyQueryBuff.form.setFieldsValue(nval);
                        } else {
                            // fuzzyQueryBuff.form.setFieldsValue(val);
                        }
                        if (typeof changeFun == 'function') {
                            changeFun([data], fuzzyQueryBuff.form);
                        }
                    } else {
                        fuzzyQueryBuff.record[key] = data[key];
                        if (typeof changeFun == 'function') {
                            changeFun([data], fuzzyQueryBuff.index, data[key], fuzzyQueryBuff.record);
                        }
                    }

                }
                fuzzyQueryBuff = null;
            });
        }
    }
    if (extendConfig && extendConfig.label) {
        searchConfig.label = extendConfig.label;
        searchConfig.title = extendConfig.label
    }
    if (extendConfig && extendConfig.disabled){
        searchConfig.disabled = extendConfig.disabled;
    }
    if (type == 1) {
        searchConfig.on.change = function (event, value, modalRowData, form) {
            if (!modalRowData) fuzzyQueryBuff = { value: value, type: 1, form: form };
            if (!value) {
                modalRowData = null
            }
            let val = {};
            config.defTableCol.forEach(item => {
                let key1 = item.dataIndex;
                let key2 = item.dataIndex;
                if (config.mapper) {
                    key2 = config.mapper[key1];
                }
                if (modalRowData){
                    val[key2] = isMulSelect ? modalRowData.map(t=>t[key1]).join(',') : modalRowData[0][key1];
                }else{
                    val[key2] = '';
                }
            });
            if (extendConfig && extendConfig.mapper) {
                let nval = {};
                /* 映射配置--开始 */
                Object.keys(val).map(k => {
                    let k1 = extendConfig.mapper[k];
                    if (!!k1) nval[k1] = val[k];
                    else nval[k] = val[k];
                });
                /* 映射配置--结束 */
                form.setFieldsValue(nval);
            } else {
                form.setFieldsValue(val);
            }
            if (typeof changeFun == 'function') {
                changeFun(modalRowData, form,event);
            }
        }
        // extendConfig.mapper
        if (extendConfig && !!extendConfig.mapper) {
            let newk = extendConfig.mapper[searchConfig.name]
            if (newk) searchConfig.name = newk;
        }
        return [
            ...config.items.map(t => {
                let item = { ...t };
                if (extendConfig && !!extendConfig.mapper && !!extendConfig.mapper[t.name]) item.name = extendConfig.mapper[t.name];
                return item;
            }),
            searchConfig
        ]
    } else if (type == 2) {
        searchConfig.on.change = function ({ modalRowData, index, value, record }) {
            if (!modalRowData) fuzzyQueryBuff = { value: value, type: 2, record: record, index: index };
            if (typeof changeFun == 'function') {
                changeFun(modalRowData, index, value, record);
            }
        };
        searchConfig.dataIndex = searchConfig.name;
        searchConfig.key = searchConfig.name;
        searchConfig.targetValueKey = searchConfig.name;
        return searchConfig;
    }
}




/* function genBaseConfig(name, key, rowKey, targetValueKey) {
    let config = {};
    config.items = [
        { hidden: true, type: 'input', name: key + 'Id', label: 'ID', value: '' },
        { hidden: true, type: 'input', name: key + 'Code', label: 'CODE', value: '' },
    ];
    config.defSearchConfig = {
        name: key + 'Name', label: name, title: name, value: '',
        destroyOnClose: true, targetClick: true,
        rowKey: rowKey, targetValueKey: targetValueKey,
    }
    config.defToolBar = {

    };
    config.defTableCol = [
        { dataIndex: key + 'Id', title: 'ID', hidden: true },
        { dataIndex: key + 'Name', title: name + '名称' },
        { dataIndex: key + 'Code', title: name + '编码' }
    ]
    return config;
} */