import {genBldgSearchModal, genWarehouseAuthSearchModal, genWarehouseRoomSearchModal} from '@/views/kzyWMS/SearchModal'
import {remote2} from './remote'
import {baseWmsWarehouseRoomMst_infoWithExpand} from "@/views/kzyWMS/basedata/warehouse/api/api_warehouse";

let grid = {span: 8, labelCol: {span: 8}}

export const formItems = function (self) {
  return [
    {type: 'input', name: 'dcCode', label: '所属仓配中心编码', value: '', hidden: true},
    {type: 'input', name: 'dcName', label: '所属仓配中心', value: '', disabled: true},
    // 仓库
    ...genWarehouseAuthSearchModal(
      1,
      true,
      false,
      null,
      function (rowData, form) {
        // 清空
        if (!rowData) {
          form.setFieldsValue({
            dcId: '', dcCode: '', dcName: '',
            warehouseId: '', warehouseCode: '', warehouseName: '',
            bldgName: '', bldgCode: '', bldgId: '',
            warehouseRoomId: '', warehouseRoomCode: '', warehouseRoomName: '', warehouseAreaCategory: '',
            warehouseWorkingMethod: ''
          })
          return
        }
        form.setFieldsValue({
          dcId: rowData ? rowData[0].dcId : '',
          dcCode: rowData ? rowData[0].dcCode : '',
          dcName: rowData ? rowData[0].dcName : '',
          warehouseId: rowData ? rowData[0].warehouseId : '',
          warehouseCode: rowData ? rowData[0].warehouseCode : '',
          warehouseName: rowData ? rowData[0].warehouseName : '',
          bldgName: '',
          bldgCode: '',
          bldgId: '',
          warehouseRoomId: '',
          warehouseRoomCode: '',
          warehouseRoomName: '',
          warehouseWorkingMethod: ''
        })
      },
      grid,
      null
    ),
    // 建筑物
    ...genBldgSearchModal(
      1,
      true,
      false,
      function (vnode, params) {
        let vals = self.$refs.formModel.$refs.formTarget.form.getFieldsValue()
        params.params.warehouseId = vals.warehouseId
        return params
      },
      function (rowData, form) {
        if (!rowData) {
          form.setFieldsValue({
            bldgName: '', bldgCode: '', bldgId: '',
            warehouseRoomId: '', warehouseRoomCode: '', warehouseRoomName: '', warehouseAreaCategory: '',
            warehouseWorkingMethod: ''
          })
          return
        }
        form.setFieldsValue({
          dcId: rowData ? rowData[0].dcId : '',
          dcCode: rowData ? rowData[0].dcCode : '',
          dcName: rowData ? rowData[0].dcName : '',
          warehouseId: rowData ? rowData[0].warehouseId : '',
          warehouseCode: rowData ? rowData[0].warehouseCode : '',
          warehouseName: rowData ? rowData[0].warehouseName : '',
          bldgId: rowData ? rowData[0].bldgId : '',
          bldgCode: rowData ? rowData[0].bldgCode : '',
          bldgName: rowData ? rowData[0].bldgName : '',
          warehouseRoomId: '',
          warehouseRoomCode: '',
          warehouseRoomName: '',
          warehouseWorkingMethod: ''
        })
      },
      grid,
      null
    ),
    // 库房
    ...genWarehouseRoomSearchModal(
      1,
      true,
      false,
      function (vnode, params) {
        let vals = self.$refs.formModel.$refs.formTarget.form.getFieldsValue()
        params.params.warehouseId = vals.warehouseId
        params.params.bldgId = vals.bldgId
        return params
      },
      function (rowData, form) {
        if (!rowData) {
          form.setFieldsValue({
            warehouseRoomName: '',
            warehouseRoomId: '',
            warehouseRoomCode: '',
            warehouseAreaCategory: '',
          })
          self.formData = {
            ...self.formData,
            warehouseRoomName: '',
            warehouseRoomId: '',
            warehouseRoomCode: '',
            warehouseAreaCategory: '',
            warehouseWorkingMethod: '',
          }
        }
        const {
          dcId = '', dcName = '', dcCode = '',
          bldgName = '', bldgCode = '', bldgId = '',
          warehouseId = '', warehouseName = '', warehouseCode = '',
          warehouseRoomName = '', warehouseRoomId = '', warehouseRoomCode = '',
          warehouseRoomType: warehouseAreaCategory = ''
        } = rowData?.[0] || {}
        form.setFieldsValue({
          dcId, dcName, dcCode,
          bldgName, bldgCode, bldgId,
          warehouseId, warehouseName, warehouseCode,
          warehouseRoomName, warehouseRoomId, warehouseRoomCode,
          warehouseAreaCategory
        })
        if (!warehouseRoomId) {
          self.formData = {
            ...self.formData,
            dcId, dcName, dcCode,
            warehouseId, warehouseName, warehouseCode,
            warehouseRoomName, warehouseRoomId, warehouseRoomCode, warehouseAreaCategory,
            warehouseWorkingMethod: '',
          }
          return
        }
        baseWmsWarehouseRoomMst_infoWithExpand({id: warehouseRoomId}).then((res) => {
          const warehouseWorkingMethod = res.data.profil.warehouseWorkingMethod
          self.formData = {
            ...self.formData,
            dcId, dcName, dcCode,
            warehouseId, warehouseName, warehouseCode,
            warehouseRoomName, warehouseRoomId, warehouseRoomCode, warehouseAreaCategory,
            warehouseWorkingMethod,
          }
        })
      },
      grid,
      null
    ),
    {type: 'input', name: 'warehouseAreaCode', label: '库区编码', value: '', required: true},
    {type: 'input', name: 'warehouseAreaName', label: '库区名称', value: '', required: true},
    {type: 'select', name: 'warehouseAreaType', label: '库区类型', value: '', ...remote2('areatype'), required: true},
    {
      type: 'select',
      name: 'warehouseAreaCategory',
      label: '库别',
      value: '',
      ...remote2('wms_storeroom'),
      required: true
    },
    {type: 'select', name: 'pickMode', label: '拣货方式', value: '', ...remote2('wms_pickmode')},
    {type: 'input', name: 'splitGroup', label: '拆零分组', value: ''},
    {
      type: 'select',
      name: 'cell01',
      label: '暂存区',
      value: '',
      options: [
        {title: '医药行业冷藏品', value: '1'},
        {title: '中药材', value: '2'},
        {title: '中药饮片', value: '3'}
      ]
    },
    {type: 'input', name: 'floorNum', label: '所在楼层', value: ''},
    {type: 'input', name: 'warehouseAreaDistance', label: '作业距离(米)', value: ''},
    {type: 'switch', name: 'isEnable', label: '是否启用', value: ''},
    {type: 'number', name: 'attribute1', label: '容器体积', value: ''},
    {type: 'input', name: 'attribute2', label: '预留字段2', value: ''},
    {type: 'input', name: 'attribute3', label: '预留字段3', value: ''},
    {type: 'input', name: 'attribute4', label: '预留字段4', value: ''},
    {type: 'input', name: 'attribute5', label: '预留字段5', value: ''},
    {type: 'input', name: 'attribute6', label: '预留字段6', value: ''},
    {type: 'input', name: 'attribute7', label: '预留字段7', value: ''},
    {type: 'input', name: 'attribute8', label: '预留字段8', value: ''},
    {type: 'input', name: 'attribute9', label: '预留字段9', value: ''},
    {type: 'input', name: 'attribute10', label: '预留字段10', value: ''}
  ]
}

export const addItems1 = [
  {
    type: 'select',
    name: 'warehouseLayoutType',
    label: '仓库布局类型',
    options: [
      {title: '平面', value: '平面'},
      {title: '立体', value: '立体'},
      {title: '自动', value: '自动'}
    ]
  },
  {
    type: 'select',
    name: 'warehouseWorkingMethod',
    label: '拣货方式',
    ...remote2('wms_pickmode')
  },
  {
    type: 'input',
    name: 'attribute1',
    label: '预留字段1'
  },
  {
    type: 'input',
    name: 'attribute2',
    label: '预留字段2'
  },
  {
    type: 'input',
    name: 'attribute3',
    label: '预留字段3'
  },
  {
    type: 'input',
    name: 'attribute4',
    label: '预留字段4'
  }
]
