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

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 api = {
  queryDictList: upcloud_base_common + '/dict/listbytype',

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

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

  deleteList: upcloud_base_common + '/dicttype/delete'
}
const items = function(self) {
  return [
    // 仓库
    ...genWarehouseSearchModal(
      1,
      true,
      false,
      null,
      function(rowDatas, form) {
        form.setFieldsValue({
          bldgId: '',
          bldgCode: '',
          bldgName: '',
          warehouseAreaId: '',
          warehouseAreaCode: '',
          warehouseAreaName: '',
          warehouseLogicId: '',
          warehouseLogicCode: '',
          warehouseLogicName: ''
        })
        if (rowDatas) form.disableItem(['warehouseAreaName', 'warehouseLogicName'])
        else form.enableItem(['warehouseAreaName', 'warehouseLogicName'])
      },
      { span: 6, labelCol: { span: 6 } }
    ),
    ...genBldgSearchModal(
      1,
      false,
      false,
      function(vnode, params) {
        let vals = self.$refs.search.form.getFieldsValue()
        params.params.warehouseId = vals.warehouseId
        return params
      },
      function(rowDatas, form) {
        form.setFieldsValue({
          warehouseAreaId: '',
          warehouseAreaCode: '',
          warehouseAreaName: '',
          warehouseLogicId: '',
          warehouseLogicCode: '',
          warehouseLogicName: ''
        })
        form.disableItem(['warehouseLogicName'])
        if (rowDatas) form.enableItem(['warehouseAreaName'])
      },
      { span: 6, labelCol: { span: 6 } }
    ),
    ...genWarehouseAreaSearchModal(
      1,
      false,
      false,
      function(vnode, params) {
        let vals = self.$refs.search.form.getFieldsValue()
        params.params.bldgId = vals.bldgId
        return params
      },
      function(rowDatas, form) {
        form.setFieldsValue({
          warehouseLogicId: '',
          warehouseLogicCode: '',
          warehouseLogicName: ''
        })
        if (rowDatas) form.enableItem(['warehouseLogicName'])
      },
      { span: 6, labelCol: { span: 6 } },
      { disabled: true }
    ),
    ...genWarehouseLogicSearchModal(
      1,
      false,
      false,
      function(vnode, params) {
        let vals = self.$refs.search.form.getFieldsValue()
        params.params.warehouseAreaId = vals.warehouseAreaId
        params.params.warehouseId = vals.warehouseId
        return params
      },
      null,
      { span: 6, labelCol: { span: 6 } },
      { disabled: true }
    ),
    {
      type: 'select',
      name: 'warehouseAreaCategory',
      label: '库别',
      value: '',
      span: 6,
      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: 6,
      labelCol: { span: 6 }
      // ...grid
    },

    {
      type: 'input',
      name: 'warehouseLocatorName',
      label: '货位名称',
      span: 6,
      labelCol: { span: 6 }
      // ...grid
    },
    {
      type: 'input',
      name: 'warehouseLocatorCode',
      label: '货位编码',
      span: 6,
      labelCol: { span: 6 }
      // ...grid
    },
    {
      type: 'number',
      name: 'floorNum',
      label: '楼层',
      span: 6,
      labelCol: { span: 6 }
      // ...grid
    },
    {
      type: 'input',
      name: 'locatorLaneNum',
      label: '巷道',
      span: 6,
      labelCol: { span: 6 }
      // ...grid
    },
    {
      span: 6,
      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 = function(self) {
  return [
    { type: 'input', name: 'dcId', label: '仓配中心id', hidden: true },
    { type: 'input', name: 'dcCode', label: '仓配中心编码', value: '', hidden: true },
    { type: 'input', name: 'dcName', label: '仓配中心', value: '', disabled: true },
    ...genWarehouseSearchModal(
      1,
      true,
      false,
      null,
      function(rowDatas, form) {
        form.setFieldsValue({
          dcId: rowDatas ? rowDatas[0].dcId : '',
          dcCode: rowDatas ? rowDatas[0].dcCode : '',
          dcName: rowDatas ? rowDatas[0].dcName : '',
          bldgId: '',
          bldgCode: '',
          bldgName: '',
          warehouseAreaId: '',
          warehouseAreaCode: '',
          warehouseAreaName: '',
          warehouseLogicId: '',
          warehouseLogicCode: '',
          warehouseLogicName: ''
        })
        form.disableItem(['warehouseAreaName', 'warehouseLogicName'])
        if (rowDatas) form.enableItem(['bldgName'])
      },
      { span: 8, labelCol: { span: 6 } }
    ),
    ...genBldgSearchModal(
      1,
      true,
      false,
      function(vnode, params) {
        let vals = self.$refs.modelForm.form.getFieldsValue()
        params.params.warehouseId = vals.warehouseId
        return params
      },
      function(rowDatas, form) {
        form.setFieldsValue({
          warehouseAreaId: '',
          warehouseAreaCode: '',
          warehouseAreaName: '',
          warehouseLogicId: '',
          warehouseLogicCode: '',
          warehouseLogicName: ''
        })
        form.disableItem(['warehouseLogicName'])
        if (rowDatas) form.enableItem(['warehouseAreaName'])
      },
      { span: 8, labelCol: { span: 6 } },
      { disabled: true }
    ),
    ...genWarehouseAreaSearchModal(
      1,
      true,
      false,
      function(vnode, params) {
        let vals = self.$refs.modelForm.form.getFieldsValue()
        params.params.bldgId = vals.bldgId
        params.params.warehouseId = vals.warehouseId
        return params
      },
      function(rowDatas, form) {
        form.setFieldsValue({
          warehouseLogicId: '',
          warehouseLogicCode: '',
          warehouseLogicName: ''
        })
        if (rowDatas) form.enableItem(['warehouseLogicName'])
      },
      { span: 8, labelCol: { span: 6 } },
      { disabled: true }
    ),
    ...genWarehouseLogicSearchModal(
      1,
      true,
      false,
      function(vnode, params) {
        let vals = self.$refs.modelForm.form.getFieldsValue()
        params.params.warehouseAreaId = vals.warehouseAreaId
        params.params.warehouseId = vals.warehouseId
        return params
      },
      null,
      { span: 8, labelCol: { span: 6 } },
      { disabled: true }
    ),
    {
      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: '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: '是否启用',
      value: true
    },
    {
      type: 'select',
      name: 'accessLevel',
      label: '数据访问控制级'
    },
    {
      type: 'input',
      name: 'attribute2',
      label: '存储条件',
      hidden: true
    },
    {
      type: 'select',
      name: 'attribute1',
      label: '存储条件',
      required: true,
      options: [
        { value: 'CW', label: '常温' },
        { value: 'HW', label: '恒温' },
        { value: 'YL', label: '阴凉' },
        { value: 'LC', label: '冷藏' },
        { value: 'LD', label: '冷冻' }
      ],
      on: {
        change(val, p2, options, p4) {
          p4.setFieldsValue({
            attribute2: p2
          })
        }
      }
    },
    {
      type: 'select',
      name: 'areaCategory',
      label: '库别',
      required: true,
      ...remote('wms_storeroom')
    },
    //   humitureDeviceNo
    {
      type: 'input',
      name: 'humitureDeviceNo',
      label: '测点编号'
    },
    {
      type: 'select',
      name: 'mixType',
      label: '混放类型',
      options: [
        { value: '1', label: '单品单批' },
        { value: '2', label: '多品单批' },
        { value: '3', label: '多品多批' }
      ]
    }
  ]
}
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',
    ellipsis: true,
    title: '货位名称',
    width: 100
  },
  {
    dataIndex: 'warehouseLocatorCode',
    title: '货位编码',
    width: 100
  },
  {
    dataIndex: 'dcName',
    title: '仓配中心名称',
    width: 100
  },
  {
    dataIndex: 'dcCode',
    title: '仓配中心编码',
    width: 100
  },
  {
    dataIndex: 'warehouseName',
    title: '仓库名称',
    ellipsis: true,
    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: 120 }, // {
  { dataIndex: 'warehouseAreaCode', title: '库区编码', width: 80 }, // {
  //   dataIndex: 'floorNum',
  //   title: '楼层'
  // },
  {
    dataIndex: 'inIndex',
    title: '上架序号',
    width: 80
  },
  {
    dataIndex: 'cardPanelNumber',
    title: '货位卡板数',
    width: 100
  },
  {
    dataIndex: 'layerNum',
    title: '货位所在层',
    width: 100
  },
  {
    dataIndex: 'columnNum',
    title: '货位列',
    width: 80
  },
  {
    dataIndex: 'rowNum',
    title: '货位行',
    width: 80
  },
  {
    dataIndex: 'itemPlaceType',
    title: '码放类型',
    width: 80,
    customRender: text => {
      if (text == '1') return '堆垛'
      if (text == '2') return '货架'
      if (text == '3') return '托盘'
    }
  },
  {
    dataIndex: 'locatorLaneNum',
    title: '货位巷道编码',
    width: 100
  },
  {
    dataIndex: 'lockFlag',
    title: '锁定标志',
    width: 80
  },
  {
    dataIndex: 'outIndex',
    title: '拣货序号',
    width: 80
  },
  {
    dataIndex: 'storageType',
    title: '货位存放属性',
    width: 100
  },
  {
    dataIndex: 'warehouseLocatorDistance',
    title: '作业距离(米)拣货路径',
    width: 160
  },
  {
    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
  }
]

const modalEditItems = function(self) {
  return [
    // {
    //   type: 'select',
    //   name: 'warehouseAreaCategory',
    //   label: '库别',
    //   value: '',
    //   autoLoad: true,
    //   remoteUrl: { url: api.queryDictList + '?dicttypeCodes=wms_storeroom', method: 'post' },
    //   replaceFields: {
    //     value: 'dictCode',
    //     label: 'dictName'
    //   }
    // },
    ...genWarehouseRoomSearchModal(1, false, false, null, self.changeBldg, grid, null),
    ...genWarehouseLogicSearchModal(
      1,
      false,
      false,
      function(vnode, params) {
        let vals = self.$refs.modelEditType.form.getFieldsValue()
        params.params.warehouseAreaId = vals.warehouseAreaId
        params.params.warehouseId = vals.warehouseId
        return params
      },
      null,
      // { span: 8, labelCol: { span: 6 } },
      { disabled: false }
    ),
    ...genWarehouseAreaSearchModal(
      1,
      false,
      false,
      function(vnode, params) {
        let vals = self.$refs.modelEditType.form.getFieldsValue()
        params.params.bldgId = vals.bldgId
        return params
      },
      function(rowDatas, form) {
        form.setFieldsValue({
          warehouseLogicId: '',
          warehouseLogicCode: '',
          warehouseLogicName: ''
        })
        if (rowDatas) form.enableItem(['warehouseLogicName'])
      },
      // { span: 6, labelCol: { span: 6 } },
      { disabled: false }
    ),
    {
      type: 'select',
      name: 'attribute1',
      label: '存储条件',
      options: [
        { value: 'CW', label: '常温' },
        { value: 'HW', label: '恒温' },
        { value: 'YL', label: '阴凉' },
        { value: 'LC', label: '冷藏' },
        { value: 'LD', label: '冷冻' }
      ],
      on: {
        change(val, p2, options, p4) {
          p4.setFieldsValue({
            attribute2: p2
          })
        }
      }
    },
    {
      type: 'switch',
      name: 'isEnable',
      label: '是否启用',
      value: true
    }
  ]
}

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