import request from '@/utils/request'
import { genContainerSearchModal } from "../../SearchModal";
import { upcloud_base_wh } from "@/config/serviceSettings";

// 容器占用
function getContainers(parameter) {
    return request({
        method: 'post',
        url: upcloud_base_wh + '/container/getPageList',
        data: parameter
    })
}
let clickIndex = -1;
export function genCloumnContainer(self) {
    return {
        /* 统一searchmodal配置--开始 */
        ...genContainerSearchModal(2, false, false, function (res, p) {
            p.data.warehouseId = self.detailData.main.warehouseId;
            p.data.containerStatus = '10';
            return p;
        }, null),
        /* 统一searchmodal配置--结束 */
        remoteUrl: { url: upcloud_base_wh + '/container/getPageList', method: 'post' },
        title: '容器编码',
        dataIndex: 'containerCode',
        key: 'containerCode',
        targetValueKey: 'containerCode',
        required: false,
        rowKey: 'containerId',
        width: 150,
        targetClick: false,
        on: {
            blur(event) {
                let code = event.target.value||'';
                code = code.trim();
                if (!code) return;
                let clickIndex = +Array.from(event.target.parentElement.parentElement.classList).find(t=>t.indexOf('containerCode')!=-1).replace('containerCode','')
                getContainers({ pageSize:1000, containerStatus: '10', containerCode: code, warehouseId: self.mainlist[0].warehouseId }).then(res => {
                    if (!res.success) {
                        this.$exception(res)
                        self.queryRefFromName('list').data[clickIndex].containerCode = '';
                        return;
                    }
                    if (!Array.isArray(res.data.list) || res.data.list.length === 0) {
                        self.queryRefFromName('list').data[clickIndex].containerCode = '';
                        self.$message.warn('容器不存在或已占有');
                        return;
                    }
                    //根据输入的值匹配接口返回值，如果没有匹配到一模一样的值就取返回的第一个值赋给当前容器号
                    if (res.data.list.every(tt => tt.containerCode != code)){
                        let item = res.data.list[0];
                        self.queryRefFromName('list').data[clickIndex].containerCode = item.containerCode;
                    }
                });
            }
        }
    }
}