import moment from 'moment'
import qs from 'qs'
import { remote } from './remote'
// import { grid } from './common'
import {
  genWarehouseSearchModal,
  genBldgSearchModal,
  genWarehouseRoomSearchModal,
  genWarehouseAreaSearchModal,
  genWarehouseLogicSearchModal
} from '@/views/kzyWMS/SearchModal'

let dcId = null
let warehouseId = null //2.仓库id
let bldgId = null //3.建筑物id
let warehouseRoomId = null //4.库房（库别）
let warehouseAreaId = null //5.库区
let warehouseLogicId = null //6.逻辑区
const baseUrl = '/api/upcloud-base-common'
const api = {
  queryDictList: baseUrl + '/dict/listbytype',

  queryList: baseUrl + '/dicttype/page',
  queryItem: baseUrl + '/dicttype',

  addItem: baseUrl + '/dicttype/add',
  uploadItem: baseUrl + '/dicttype/update',

  deleteList: baseUrl + '/dicttype/delete'
}
const items = [
  /* ...genwarehouse({
    ...grid,
    required: true,
	span:6,
	labelCol:{span:6}
  }), */
  // 仓库
  ...genWarehouseSearchModal(
    1,
    true,
    false,
    null,
    function(rowData, form) {
      if (!rowData) {
        form.setFieldsValue({
          warehouseId: '',
          warehouseCode: '',
          warehouseName: ''
        })
      } else {
        form.setFieldsValue({
          warehouseId: rowData[0].warehouseId,
          warehouseCode: rowData[0].warehouseCode,
          warehouseName: rowData[0].warehouseName
        })
      }
    },
    { span: 8, labelCol: { span: 6 } }
  ),
  ...genBldgSearchModal(1, false, false, null, null, { span: 8, labelCol: { span: 6 } }),
  ...genWarehouseAreaSearchModal(1, false, false, null, null, { span: 8, labelCol: { span: 6 } }),
  ...genWarehouseLogicSearchModal(1, false, false, null, null, { span: 8, labelCol: { span: 6 } }),
  {
    type: 'select',
    name: 'warehouseAreaCategory',
    label: '库别',
    value: '',
    span: 8,
    labelCol: { span: 6 },
    autoLoad: true,
    remoteUrl: { url: api.queryDictList + '?dicttypeCodes=wms_storeroom', method: 'post' },
    replaceFields: {
      value: 'dictCode',
      label: 'dictName'
    }
  },
  {
    type: 'select',
    options: [
      {
        value: '0',
        label: '整件'
      },
      {
        value: '1',
        label: '散件'
      }
    ],
    name: 'warehouseLocatorType',
    label: '存储分类',
    span: 8,
    labelCol: { span: 6 }
    // ...grid
  },

  {
    type: 'input',
    name: 'warehouseLocatorName',
    label: '货位名称',
    span: 8,
    labelCol: { span: 6 }
    // ...grid
  },
  {
    type: 'input',
    name: 'warehouseLocatorCode',
    label: '货位编码',
    span: 8,
    labelCol: { span: 6 }
    // ...grid
  },
  {
    type: 'number',
    name: 'floorNum',
    label: '楼层',
    span: 8,
    labelCol: { span: 6 }
    // ...grid
  },
  {
    type: 'input',
    name: 'locatorLaneNum',
    label: '巷道',
    span: 8,
    labelCol: { span: 6 }
    // ...grid
  },
  {
    span: 8,
    labelCol: { span: 6 },
    type: 'buttonGroup',
    style: { textAlign: 'right' },
    options: [
      {
        value: '查询',
        icon: 'search',
        btnType: 'primary',
        style: 'margin-left:50px;margin-right:10px;',
        htmlType: 'submit'
      },
      {
        value: '重置',
        name: 'ccz',
        icon: 'redo',
        on: {
          click(event, form) {
            form.resetFields()
          }
        }
      }
    ]
  }
]

const extendItems = [
  {
    type: 'input',
    name: 'attribute11',
    label: '预留字段1',
    disabled: true
  },
  {
    type: 'input',
    name: 'attribute12',
    label: '预留字段2',
    disabled: true
  },
  {
    type: 'input',
    name: 'attribute13',
    label: '预留字段3',
    disabled: true
  },
  {
    type: 'input',
    name: 'attribute14',
    label: '预留字段4',
    disabled: true
  },
  {
    type: 'input',
    name: 'attribute15',
    label: '预留字段5',
    disabled: true
  },
  {
    type: 'input',
    name: 'segment11',
    label: '预留字段6',
    disabled: true
  },
  {
    type: 'input',
    name: 'segment12',
    label: '预留字段7',
    disabled: true
  },
  {
    type: 'input',
    name: 'segment13',
    label: '预留字段8',
    disabled: true
  },
  {
    type: 'input',
    name: 'segment14',
    label: '预留字段9',
    disabled: true
  },
  {
    type: 'input',
    name: 'segment15',
    label: '预留字段10',
    disabled: true
  }
]

const modalItems = [
  { type: 'input', name: 'dcId', label: '仓配中心id', hidden: true },
  {
    name: 'dcName',
    label: '仓配中心名称',
    type: 'searchModal',
    remoteUrl: { url: '/api/upcloud-base-wh/wmsdc/page', method: 'get' },
    rowKey: 'dcId',
    targetValueKey: 'dcName',
    targetClick: true,
    required: true,
    disabled: true,
    toolBar: {
      items: [
        { type: 'input', name: 'dcCode', label: '仓配中心编码', value: '' },
        { type: 'input', name: 'dcName', label: '仓配中心名称', value: '' }
      ]
    },
    tableConfig: {
      columns: [
        {
          dataIndex: 'dcCode',
          title: '仓配中心编码'
        },
        {
          dataIndex: 'dcName',
          title: '仓配中心名称'
        }
      ]
    },
    on: {
      ok(event, value, rowData, form) {
        dcId = rowData[0].dcId
        form.setFieldsValue({
          dcId: rowData[0].dcId
        })
      }
    }
  },
  // 仓库
  ...genWarehouseSearchModal(
    1,
    true,
    false,
    null,
    function(rowData, form) {
      if (!rowData) {
        warehouseId = ''
        form.setFieldsValue({
          dcId: '',
          dcCode: '',
          dcName: '',
          warehouseId: '',
          warehouseCode: '',
          warehouseName: ''
        })
      } else {
        warehouseId = rowData[0].warehouseId
        form.setFieldsValue({
          dcId: rowData[0].dcId,
          dcCode: rowData[0].dcCode,
          dcName: rowData[0].dcName,
          warehouseId: rowData[0].warehouseId,
          warehouseCode: rowData[0].warehouseCode,
          warehouseName: rowData[0].warehouseName
        })
      }
    },
    { span: 8, labelCol: { span: 6 } }
  ),
  /* { type: 'input', name: 'warehouseId', label: '仓库id', hidden: true },
  {
    type: 'searchModal',name: 'warehouseName',label: '仓库名称',required: true,remoteUrl: { url: '/api/upcloud-base-wh/warehouse/page', method: 'get' },rowKey: 'warehouseId',targetValueKey: 'warehouseName',targetClick: true,
    toolBar: {
      items: [
        {type: 'input',name: 'warehouseCode',label: '仓库编码',value: ''},
        {type: 'input',name: 'warehouseName',label: '仓库名称',value: ''},
        {type: 'input',name: 'dcName',label: '仓配名称',value: ''}
      ]
    },
    tableConfig: {
      columns: [{dataIndex: 'warehouseCode',title: '仓库编码'},
        {dataIndex: 'warehouseName',title: '仓库名称'},
        {dataIndex: 'dcName',title: '仓配名称'}
      ]
    },
    on: {
      ok(event, value, rowData, form) {
        dcId = rowData[0].dcId;
        warehouseId = rowData[0].warehouseId;
        form.setFieldsValue({
          warehouseId: rowData[0].warehouseId,
          dcId: rowData[0].dcId,
          dcName: rowData[0].dcName,
          dcCode: rowData[0].dcCode
        })
      },
      change(event, value, rowData, form) {
        if (!value) {
          dcId = null;
          warehouseId = null;
          form.setFieldsValue({
            warehouseId: null,
            dcId: null,
            dcName: null,
            dcCode: null
          })
        }
      }
    }
  }, */
  // 建筑物
  ...genBldgSearchModal(
    1,
    true,
    false,
    function(res, p) {
      p.params.warehouseId = warehouseId
      return p
    },
    function(rowData, form) {
      bldgId = ''
      if (!rowData) {
        form.setFieldsValue({
          bldgId: '',
          bldgCode: '',
          bldgName: ''
        })
      } else {
        bldgId = rowData[0].bldgId
        form.setFieldsValue({
          bldgId: rowData[0].bldgId,
          bldgCode: rowData[0].bldgCode,
          bldgName: rowData[0].bldgName
        })
      }
    },
    { span: 8, labelCol: { span: 6 } }
  ),
  /* ...genBuilding(1, true, false, function (res, p) {
    p.params.warehouseId = warehouseId;
    return p;
  }, function (rowData, form) {
    bldgId = rowData[0].bldgId;
  }, {}), */

  // 库房
  ...genWarehouseRoomSearchModal(
    1,
    true,
    false,
    function(res, p) {
      p.params.bldgId = bldgId
      return p
    },
    function(rowData, form) {
      if (!rowData) {
        warehouseRoomId = ''
        form.setFieldsValue({
          warehouseRoomId: '',
          warehouseRoomCode: '',
          warehouseRoomName: ''
        })
      } else {
        warehouseRoomId = rowData[0].warehouseRoomId
        form.setFieldsValue({
          warehouseRoomId: rowData[0].warehouseRoomId,
          warehouseRoomCode: rowData[0].warehouseRoomCode,
          warehouseRoomName: rowData[0].warehouseRoomName
        })
      }
    },
    { span: 8, labelCol: { span: 6 } }
  ),
  /* { type: 'input', name: 'warehouseRoomId', label: '库房id', hidden: true },
  {
    type: 'searchModal',
    name: 'warehouseRoomName',
    label: '库房名称',
    required: true,
    remoteUrl: { url: '/api/upcloud-base-wh/baseWmsWarehouseRoomMst/page2', method: 'get' },
    rowKey: 'warehouseRoomId',
    targetValueKey: 'warehouseRoomName',
    targetClick: true,
    toolBar: {
      items: [
        {
          type: 'input',
          name: 'warehouseRoomCode',
          label: '库房编码',
          value: ''
        },
        {
          type: 'input',
          name: 'warehouseRoomName',
          label: '库房名称',
          value: ''
        }
      ]
    },
    tableConfig: {
      columns: [
        {
          dataIndex: 'warehouseRoomCode',
          title: '库房编码'
        },
        {
          dataIndex: 'warehouseRoomName',
          title: '库房名称'
        }
      ],
      remoteBefore(res, p) {
        p.params.bldgId = bldgId;
        return p;
      }
    },
    on: {
      ok(event, value, rowData, form) {
        warehouseRoomId = rowData[0].warehouseRoomId
        form.setFieldsValue({
          warehouseRoomId: rowData[0].warehouseRoomId
        })
      },
      change(event, value, rowData, form) {
        if (!value) {
          warehouseRoomId = null;
          form.setFieldsValue({ warehouseRoomId: null })
        }
      }
    }
  }, */

  // 库区
  ...genWarehouseAreaSearchModal(
    1,
    true,
    false,
    function(res, p) {
      p.params.warehouseRoomId = warehouseRoomId
      return p
    },
    function(rowData, form) {
      if (!rowData) {
        warehouseAreaId = ''
        form.setFieldsValue({
          warehouseAreaId: '',
          warehouseAreaCode: '',
          warehouseAreaName: ''
        })
      } else {
        warehouseAreaId = rowData[0].warehouseAreaId
        form.setFieldsValue({
          warehouseAreaId: rowData[0].warehouseAreaId,
          warehouseAreaCode: rowData[0].warehouseAreaCode,
          warehouseAreaName: rowData[0].warehouseAreaName
        })
      }
    },
    { span: 8, labelCol: { span: 6 } }
  ),
  /* { type: 'input', name: 'warehouseAreaId', label: '库区id', hidden: true },
  {
    type: 'searchModal',
    name: 'warehouseAreaName',
    label: '库区名称',
    required: true,
    remoteUrl: { url: '/api/upcloud-base-wh/baseWmsWarehouseArea/page2', method: 'get' },
    rowKey: 'warehouseAreaId',
    targetValueKey: 'warehouseAreaName',
    targetClick: true,
    toolBar: {
      items: [
        {
          type: 'input',
          name: 'warehouseAreaCode',
          label: '库区编码',
          value: ''
        },
        {
          type: 'input',
          name: 'warehouseAreaName',
          label: '库区名称',
          value: ''
        }
      ]
    },
    tableConfig: {
      columns: [
        {
          dataIndex: 'warehouseAreaCode',
          title: '库区编码'
        },
        {
          dataIndex: 'warehouseAreaName',
          title: '库区名称'
        }
      ],
      remoteBefore(res, p) {
        p.params.warehouseRoomId = warehouseRoomId;
        return p;
      }
    },
    on: {
      ok(event, value, rowData, form) {
        warehouseAreaId = rowData[0].warehouseAreaId
        form.setFieldsValue({
          warehouseAreaId: rowData[0].warehouseAreaId
        })
      },
      change(event, value, rowData, form) {
        if (!value) {
          warehouseAreaId = null;
          form.setFieldsValue({ warehouseAreaId: null })
        }
      }
    }
  }, */
  // 逻辑区
  ...genWarehouseLogicSearchModal(
    1,
    true,
    false,
    null,
    function(rowData, form) {
      if (!rowData) {
        warehouseLogicId = ''
        form.setFieldsValue({
          warehouseLogicId: '',
          warehouseLogicCode: '',
          warehouseLogicName: ''
        })
      } else {
        warehouseLogicId = rowData[0].warehouseLogicId
        form.setFieldsValue({
          warehouseLogicId: rowData[0].warehouseLogicId,
          warehouseLogicCode: rowData[0].warehouseLogicCode,
          warehouseLogicName: rowData[0].warehouseLogicName
        })
      }
    },
    { span: 8, labelCol: { span: 6 } }
  ),
  /* { type: 'input', name: 'warehouseLogicId', label: '逻辑区id', hidden: true },
  {
    type: 'searchModal',
    name: 'warehouseLogicName',
    label: '逻辑区名称',
    remoteUrl: { url: '/api/upcloud-base-wh/baseWmsWarehouseLogic/page2', method: 'get' },
    rowKey: 'warehouseLogicId',
    required: true,
    targetValueKey: 'warehouseLogicName',
    targetClick: true,
    toolBar: {
      items: [
        {
          type: 'input',
          name: 'warehouseLogicCode',
          label: '逻辑区编码'
        },
        {
          type: 'input',
          name: 'warehouseLogicName',
          label: '逻辑区名称'
        }
      ]
    },
    tableConfig: {
      columns: [
        {
          dataIndex: 'warehouseLogicCode',
          title: '逻辑区编码'
        },
        {
          dataIndex: 'warehouseLogicName',
          title: '逻辑区名称'
        }
      ],
      remoteBefore(res, p) {
        p.params.warehouseAreaId = warehouseAreaId;
        return p;
      }
    },
    on: {
      ok(event, value, rowData, form) {
        warehouseLogicId = rowData[0].warehouseLogicId
        form.setFieldsValue({
          warehouseLogicId: rowData[0].warehouseLogicId
        })
      },
      change(event, value, rowData, form) {
        warehouseLogicId = null;
        if (!value) {
          form.setFieldsValue({ warehouseLogicId: null })
        }
      }
    }
  }, */
  {
    type: 'input',
    name: 'warehouseLocatorName',
    label: '货位名称',
    required: true
  },
  {
    type: 'input',
    name: 'warehouseLocatorCode',
    label: '货位编码',
    required: true
  },
  {
    type: 'select',
    name: 'warehouseLocatorType',
    label: '货位类型',
    required: true,
    ...remote('wms_loctype')
  },
  // {
  //   type: 'number',
  //   name: 'floorNum',
  //   label: '楼层'
  // },
  {
    type: 'input',
    name: 'inIndex',
    label: '上架序号'
  },
  {
    type: 'input',
    name: 'layerNum',
    label: '货位所在层'
  },
  {
    type: 'input',
    name: 'columnNum',
    label: '货位列'
  },
  {
    type: 'input',
    name: 'rowNum',
    label: '货位行'
  },
  {
    type: 'input',
    name: 'cardPanelNumber',
    label: '货位的卡板数',
    required: true
  },
  {
    type: 'select',
    name: 'itemPlaceType',
    label: '码放类型',
    ...remote('stacktype')
  },
  {
    type: 'number',
    name: 'locatorLaneNum',
    label: '货位巷道编码'
  },
  {
    type: 'number',
    name: 'len',
    label: '长',
    min: 0
  },
  {
    type: 'number',
    name: 'wid',
    label: '宽',
    min: 0
  },
  {
    type: 'number',
    name: 'hgt',
    label: '高',
    min: 0
  },
  {
    type: 'number',
    name: 'vol',
    label: '体积',
    min: 0
  },
  {
    type: 'number',
    name: 'wgtlmt',
    label: '最大承重',
    min: 0
  },
  {
    type: 'switch',
    name: 'lockFlag',
    label: '锁定标志'
  },
  {
    type: 'input',
    name: 'outIndex',
    label: '拣货序号'
  },
  {
    type: 'select',
    name: 'storageType',
    label: '货位存放属性',
    options: [
      {
        label: '保管位',
        value: 'B'
      },
      {
        label: '拣货位',
        value: 'J'
      }
    ]
  },
  {
    type: 'number',
    name: 'warehouseLocatorDistance',
    label: '作业距离（米）'
  },
  {
    type: 'switch',
    name: 'isEnable',
    label: '是否启用'
  },
  {
    type: 'select',
    name: 'accessLevel',
    label: '数据访问控制级'
  }
  // {
  //   type: 'input',
  //   name: 'attribute1',
  //   label: '预留字段1'
  // },
  // {
  //   type: 'input',
  //   name: 'attribute2',
  //   label: '预留字段2'
  // },
  // {
  //   type: 'input',
  //   name: 'attribute3',
  //   label: '预留字段3'
  // },
  // {
  //   type: 'input',
  //   name: 'attribute4',
  //   label: '预留字段4'
  // }
]
const modalItems2 = [...genWarehouseLogicSearchModal(1, true, false, null, null, { span: 24, labelCol: { span: 8 } })]
const attachItems = [
  {
    type: 'input',
    name: 'attribute11',
    label: '预留字段1'
  },
  {
    type: 'input',
    name: 'attribute12',
    label: '预留字段2'
  },
  {
    type: 'input',
    name: 'attribute13',
    label: '预留字段3'
  },
  {
    type: 'input',
    name: 'attribute14',
    label: '预留字段4'
  },
  {
    type: 'input',
    name: 'attribute15',
    label: '预留字段5'
  },
  {
    type: 'input',
    name: 'segment11',
    label: '预留字段6'
  },
  {
    type: 'input',
    name: 'segment12',
    label: '预留字段7'
  },
  {
    type: 'input',
    name: 'segment13',
    label: '预留字段8'
  },
  {
    type: 'input',
    name: 'segment14',
    label: '预留字段8'
  },
  {
    type: 'input',
    name: 'segment15',
    label: '预留字段8'
  }
]

const columns = [
  {
    dataIndex: 'warehouseLocatorName',
    title: '货位名称',
    width: 100
  },
  {
    dataIndex: 'warehouseLocatorCode',
    title: '货位编码',
    width: 100
  },
  {
    dataIndex: 'dcName',
    title: '仓配中心名称',
    width: 100
  },
  {
    dataIndex: 'dcCode',
    title: '仓配中心编码',
    width: 100
  },
  {
    dataIndex: 'warehouseName',
    title: '仓库名称',
    width: 100
  },
  {
    dataIndex: 'warehouseCode',
    title: '仓库编码',
    width: 100
  },
  {
    dataIndex: 'warehouseRoomName',
    title: '库房名称',
    width: 100
  },
  {
    dataIndex: 'warehouseRoomCode',
    title: '库房编码',
    width: 100
  },
  {
    dataIndex: 'warehouseLogicName',
    title: '逻辑区名称',
    width: 160
  },
  { dataIndex: 'warehouseLogicCode', title: '逻辑区编码', width: 120 }, // {
  { dataIndex: 'warehouseAreaName', title: '库区名称', width: 80 }, // {
  { dataIndex: 'warehouseAreaCode', title: '库区编码', width: 80 }, // {
  //   dataIndex: 'floorNum',
  //   title: '楼层'
  // },
  {
    dataIndex: 'inIndex',
    title: '上架序号',
    width: 60
  },
  {
    dataIndex: 'cardPanelNumber',
    title: '货位卡板数',
    width: 80
  },
  {
    dataIndex: 'layerNum',
    title: '货位所在层',
    width: 80
  },
  {
    dataIndex: 'columnNum',
    title: '货位列',
    width: 50
  },
  {
    dataIndex: 'rowNum',
    title: '货位行',
    width: 50
  },
  {
    dataIndex: 'itemPlaceType',
    title: '码放类型',
    width: 60,
    customRender: text => {
      if (text == '1') return '堆垛'
      if (text == '2') return '货架'
      if (text == '3') return '托盘'
    }
  },
  {
    dataIndex: 'locatorLaneNum',
    title: '货位巷道编码',
    width: 80
  },
  {
    dataIndex: 'lockFlag',
    title: '锁定标志',
    width: 50
  },
  {
    dataIndex: 'outIndex',
    title: '拣货序号',
    width: 50
  },
  {
    dataIndex: 'storageType',
    title: '货位存放属性',
    width: 80
  },
  {
    dataIndex: 'warehouseLocatorDistance',
    title: '作业距离(米)拣货路径',
    width: 120
  },
  {
    dataIndex: 'gmtCreate',
    title: '修改时间',
    width: 120,
    customRender: text => {
      return moment(text).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  {
    dataIndex: 'action',
    title: '操作',
    scopedSlots: { customRender: 'action' },
    fixed: 'right',
    align: 'center',
    width: 100
  }
]

export { items, extendItems, attachItems, modalItems, columns, modalItems2 }
