const baseUrl = '/api/service-ai-eis-route-center-eistest'
import { remote } from '@/views/EIS/remote/index.js'
import { seachLocationNo } from './api'
function debounce(fn, wait) {
  let timer
  let dur = wait || 400
  return () => {
    clearTimeout(timer)
    let self = this
    let args = arguments
    timer = setTimeout(() => {
      fn.apply(self, args)
    }, dur)
  }
}
const area = function (labelname = '区域', span = 24, required = true, name = 'startRegion') {
  return {
    span: span,
    type: 'searchModal',
    name: name,
    label: labelname,
    required: required,
    targetClick: true,
    targetValueKey: 'areaNo',
    value: '',
    remoteUrl: {
      url: '/api/upcloud-base-wh-dev-mysql-eistest/storeArea/getAll-storeArea',
    },
    //   doneBefore: function(res) {
    //     console.log(res)
    //     res.data.list = res.data.list.filter(item => item.areaType == 20)
    //     return res
    //   },
    toolBar: {
      rowId: 'id',
      items: [
        {
          type: 'input',
          name: 'areaNo',
          label: '区域编号',
          value: '',
        },
        {
          type: 'select',
          name: 'areaType',
          label: '区域类别',
          value: '',
          options: [
            { value: 10, label: '区域' },
            { value: 20, label: '接驳口' },
          ],
        },
      ],
    },
    on: {
      search: () => {
        //   this.params = { name: 'hdwss' }
      },
      change: (event, value, rowData, form) => {},
    },

    tableConfig: {
      rowKey: 'areaNo',
      remoteBefore: function (vnode, remoteUrl) {
        console.log(remoteUrl)
        // remoteUrl.params.areaType = 20
        return remoteUrl
      },
      columns: [
        {
          dataIndex: 'areaNo',
          title: '区域编号',
        },
        {
          title: '区域类别',
          dataIndex: 'areaType',
          align: 'center',
          key: 'areaType',
          customRender(text) {
            text == 10 && (text = '区域')
            text == 20 && (text = '接驳口')
            return text
          },
        },
      ],
    },
  }
}
const goods = function (labelname = '区域', span = 24, required = true, name = 'startLocation') {
  return {
    span: span,
    type: 'searchModal',
    name: name,
    label: labelname,
    required: required,
    targetClick: true,
    targetValueKey: 'locationNo',
    value: '',
    remoteUrl: {
      url: '/api/upcloud-base-wh-dev-mysql-eistest/eisLocator/goods/page',
      method: 'post',
    },
    //   doneBefore: function(res) {
    //     console.log(res)
    //     res.data.list = res.data.list.filter(item => item.areaType == 20)
    //     return res
    //   },
    toolBar: {
      items: [
        {
          span: 8,
          type: 'input',
          name: 'locationNo',
          label: '货位编号',
          value: '',
        },
        {
          span: 8,
          type: 'input',
          name: 'groupNo',
          label: '货位组编号',
          value: '',
        },
        {
          span: 8,
          type: 'input',
          name: 'areaNo',
          label: '区域',
          value: '',
        },

        {
          span: 8,
          type: 'input',
          name: 'limitHeight',
          label: '限高',
          value: '',
        },
      ],
    },
    on: {
      search: () => {
        //   this.params = { name: 'hdwss' }
      },
      change: (event, value, rowData, form) => {},
    },

    tableConfig: {
      rowKey: 'id',
      remoteBefore: function (vnode, remoteUrl) {
        console.log(remoteUrl)
        // remoteUrl.params.areaType = 20
        return remoteUrl
      },
      columns: [
        {
          dataIndex: 'locationNo',
          key: 'locationNo',
          align: 'center',
          ellipsis: true,
          title: '货位编号',
        },
        {
          title: '货位组编号',
          dataIndex: 'groupNo',
          ellipsis: true,
          //   scopedSlots: { customRender: 'groupNo' },
          align: 'center',
          key: 'groupNo',
        },
        {
          title: '区域编号',
          dataIndex: 'areaNo',
          ellipsis: true,
          align: 'center',
          key: 'areaNo',
          customRender(text) {
            text == 1 && (text = '出库口')
            text == 2 && (text = '在线拣选')
            return text
          },
        },
        {
          title: '位置类型',
          ellipsis: true,
          key: 'locationType',
          dataIndex: 'locationType',
          align: 'center',
          customRender(text) {
            text == 1 && (text = '存储位')
            text == 2 && (text = '工作位')
            text == 3 && (text = '输送线点位')
            return text
          },
        },
      ],
    },
  }
}
let currentArea
let currentLocation
let businessProperty
const containerNo = function (labelname = '区域', span = 24, required = true, name = 'startLocation') {
  return {
    span: span,
    type: 'searchModal',
    name: name,
    label: labelname,
    required: required,
    targetClick: true,
    targetValueKey: 'containerNo',
    value: '',
    remoteUrl: {
      url: '/api/service-ai-eis-route-center-eistest/api/v1/route/location/findContainerPageForNoCarryTask',
      method: 'get',
    },
    doneBefore: function (res) {
      console.log(res)
      // res.data.list = res.data.list.filter(item => item.areaType == 20)
      return res
    },
    toolBar: {
      items: [
        {
          span: 8,
          type: 'input',
          name: 'containerNo',
          label: '容器号',
          value: '',
        },
        {
          span: 8,
          type: 'input',
          name: 'palletNo',
          label: '载具编号',
          value: '',
        },
        {
          span: 8,
          type: 'input',
          name: 'currentArea',
          label: '当前区域',
          value: '',
        },
        {
          span: 8,
          type: 'input',
          name: 'currentLocation',
          label: '当前点位',
          value: '',
        },
      ],
    },
    on: {
      search: () => {},
      change: (event, value, rowData, form) => {
        // console.log(event, value, rowData, form)
        if (rowData[0]) {
          console.log(rowData[0])
          currentArea = rowData[0].currentArea
          currentLocation = rowData[0].currentLocation
          businessProperty = rowData[0].businessProperty
          form.setFieldsValue({ startRegion: currentArea, startLocation: currentLocation, businessProperty })
        } else {
          form.setFieldsValue({
            startRegion: '',
            startLocation: '',
            businessProperty: '',
          })
        }
      },
    },

    tableConfig: {
      rowKey: 'containerNo',
      remoteBefore: function (vnode, remoteUrl) {
        console.log(remoteUrl)
        remoteUrl.params.taskState = 0
        return remoteUrl
      },
      columns: [
        {
          dataIndex: 'containerNo',
          key: 'containerNo',
          align: 'center',
          title: '容器号',
        },
        {
          title: '载具编号',
          dataIndex: 'palletNo',
          align: 'center',
          key: 'palletNo',
          //   customRender(text) {
          //     text == 1 && (text = '四向车')
          //     text == 2 && (text = '堆垛机')
          //     text == 3 && (text = 'AGV')
          //     text == 4 && (text = '提升机')
          //     text == 5 && (text = '多穿车')
          //     text == 6 && (text = '托盘输送线')
          //     text == 7 && (text = '料箱输送线')
          //     return text
          //   },
        },
        {
          title: '当前区域',
          dataIndex: 'currentArea',
          align: 'center',
          key: 'currentArea',
          //   customRender(text) {
          //     text == 1 && (text = '出库口')
          //     text == 2 && (text = '钢平台拣选')
          //     text == 3 && (text = '在线拣选')
          //     return text
          //   }
        },
        {
          title: '当前点位',
          key: 'currentLocation',
          dataIndex: 'currentLocation',
          align: 'center',
        },
        {
          title: '任务状态',
          key: 'taskState',
          dataIndex: 'taskState',
          align: 'center',
          customRender(text) {
            text == 0 && (text = '未开始')
            text == 10 && (text = '待发送任务')
            text == 20 && (text = '已发送指令')
            text == 30 && (text = '设备回告开始')
            text == 40 && (text = '已经离开原位置')
            return text
          },
        },
        {
          title: '重量',
          key: 'weight',
          dataIndex: 'weight',
          align: 'center',
        },
        {
          title: '高度',
          key: 'height',
          dataIndex: 'height',
          align: 'center',
        },
        {
          title: '业务属性',
          key: 'businessProperty',
          dataIndex: 'businessProperty',
          align: 'center',
        },

        {
          title: '操作',
          key: 'operation',
          scopedSlots: { customRender: 'operation' },
          //   fixed: 'right',
          align: 'center',
        },
      ],
    },
  }
}
const config = function (that) {
  return {
    remoteUrl: {
      url: baseUrl + '/api/v1/route/carry/get-carry-list',
      headers: {
        'Content-Type': 'application/JSON',
      },
      method: 'post',
    },
    items: [
      {
        span: 8,
        type: 'input',
        name: 'id',
        label: '任务ID',
        value: '',
      },
      {
        span: 8,
        type: 'input',
        name: 'startRegion',
        label: '起始区域',
        value: '',
      },
      {
        span: 8,
        type: 'input',
        name: 'endRegion',
        label: '终点区域',
        value: '',
      },
      {
        span: 8,
        type: 'input',
        name: 'containerNo',
        label: '容器号',
        value: '',
      },
      {
        span: 8,
        type: 'select',
        name: 'taskType',
        label: '任务类型',
        value: '',
        ...remote('eis_route_carry_task_type'),
      },

      // {
      //   span: 8,
      //   type: 'select',
      //   name: 'type',
      //   label: '站台类型',
      //   value: '',
      //   options: [
      //     { label: '出库口', value: 1 },
      //     { label: '钢平台拣选', value: 2 },
      //     { label: '在线拣选', value: 3 }
      //   ]
      // },

      {
        span: 24,
        type: 'buttonGroup',
        style: { textAlign: 'right' },
        options: [
          {
            value: '查询',
            icon: 'search',
            btnType: 'primary',
            style: 'margin-right:10px',
            htmlType: 'submit',
          },
          {
            value: '重置',
            icon: 'redo',
            style: 'margin-right:50px',
            on: {
              click: (event, form) => {
                console.log(form)
                form.resetFields()
              },
            },
          },
        ],
      },
    ],
    columns: [
      {
        dataIndex: 'id',
        key: 'id',
        align: 'center',
        ellipsis: true,
        title: '任务ID',
      },
      {
        title: '容器号',
        dataIndex: 'containerNo',
        align: 'center',
        ellipsis: true,
        key: 'containerNo',
      },
      {
        title: '任务类型',
        dataIndex: 'taskType',
        align: 'center',
        key: 'taskType',
        ...remote('eis_route_carry_task_type'),
      },
      {
        title: '起始区域',
        key: 'startRegion',
        dataIndex: 'startRegion',
        align: 'center',
        // customRender(text) {
        //   text == 0 && (text = '否')
        //   text == 1 && (text = '是')
        //   return text
        // },
      },
      {
        title: '起始货位',
        dataIndex: 'startLocation',
        align: 'center',
        key: 'startLocation',
        customRender(text) {
          text == 0 && (text = '不索取')
          text == 1 && (text = '索取')
          return text
        },
      },
      {
        title: '终点区域',
        dataIndex: 'endRegion',
        align: 'center',
        key: 'endRegion',
        ellipsis: true,
      },
      {
        title: '终点货位',
        dataIndex: 'endLocation',
        align: 'center',
        key: 'endLocation',
        ellipsis: true,
      },
      {
        title: '是否变更任务',
        dataIndex: 'isChangeTask',
        hidden: true,
        align: 'center',
        key: 'isChangeTask',
        customRender(text) {
          text == 0 && (text = '否')
          text == 1 && (text = '是')
          return text
        },
      },
      {
        title: '是否取消任务',
        dataIndex: 'cleanTaskStatus',
        align: 'center',
        hidden: true,
        key: 'cleanTaskStatus',
        customRender(text) {
          text == 0 && (text = '否')
          text == 1 && (text = '是')
          return text
        },
      },
      {
        title: '任务状态',
        dataIndex: 'taskStatus',
        align: 'center',
        key: 'taskStatus',
        ...remote('eis_route_carry_task_status'),
      },
      {
        title: '优先级',
        dataIndex: 'priority',
        align: 'center',
        key: 'priority',
        hidden: true,
      },
      {
        title: '高度',
        dataIndex: 'height',
        hidden: true,
        align: 'center',
        key: 'height',
      },
      {
        title: '重量',
        dataIndex: 'weight',
        hidden: true,
        align: 'center',
        key: 'weight',
      },

      {
        title: '业务属性',
        dataIndex: 'businessProperty',
        hidden: true,
        ellipsis: true,
        align: 'center',
        key: 'businessProperty',
      },
      {
        title: '优先级',
        dataIndex: 'priority',
        // hidden: true,
        ellipsis: true,
        align: 'center',
        key: 'priority',
      },

      {
        title: '任务描述',
        dataIndex: 'taskRemark',
        align: 'center',
        ellipsis: true,
        key: 'taskRemark',
      },
      {
        title: '创建时间',
        dataIndex: 'createTime',
        ellipsis: true,
        align: 'center',
        key: 'createTime',
      },

      {
        title: '操作',
        key: 'operation',
        scopedSlots: { customRender: 'operation' },
        fixed: 'right',
        align: 'center',
      },
    ],
    Modelitems: [
      {
        span: 24,
        type: 'input',
        name: 'id',
        required: false,
        label: '任务号',
        value: '',
      },
      {
        span: 24,
        type: 'select',
        name: 'taskType',
        required: true,
        label: '任务类型',
        ...remote('eis_route_carry_task_type'),
        value: '',
      },
      {
        ...containerNo('容器号', 24, true, 'containerNo'),
      },
      {
        // ...area('起始区域', 24, false, 'startRegion'),
        span: 24,
        type: 'input',
        name: 'startRegion',
        required: false,
        disabled: true,
        label: '起始区域',
        value: '',
      },

      {
        // ...goods('起点点位', 24, false, 'startLocation'),
        span: 24,
        type: 'input',
        name: 'startLocation',
        disabled: true,
        required: false,
        label: '起点点位',
        value: '',
      },
      {
        // labelname = '区域', span = 24, required = true, name = 'startRegion'
        ...area('终点区域', 24, true, 'endRegion'),
      },
      {
        ...goods('终点点位', 24, false, 'endLocation'),
      },
      // {
      //   span: 24,
      //   type: 'select',
      //   name: 'endLocation',
      //   required: false,
      //   label: '终点点位',
      //   value: '',
      //   options: [
      //     { label: '否', value: 0 },
      //     { label: '是', value: 1 },
      //   ],
      // },
      {
        span: 24,
        type: 'input',
        name: 'businessProperty',
        required: true,
        label: '业务属性',
        value: '',
      },

      {
        span: 24,
        type: 'number',
        name: 'priority',
        required: true,
        label: '优先级',
        value: '',
      },

      {
        span: 24,
        type: 'input',
        name: 'taskRemark',
        required: false,
        label: '任务描述',
        value: '',
      },
      //   {
      //     title: '任务描述',
      //     dataIndex: 'taskRemark',
      //     align: 'center',
      //     ellipsis: true,
      //     key: 'taskRemark',
      //   },
    ],
    details: {
      currentArea: '当前节点区域',
      nextArea: '下个节点区域',
      routeTaskState: '任务状态',
      routeExecuteState: '执行状态',
      currentLocation: '当前节点点位',
      nextLocation: '下个节点点位',
      deviceTask_id: '设备任务ID',
      deviceNo: '执行设备编号',
      arriveTime: '到位时间',
      errorMsg: '错误消息',
      updateTime: '修改时间',
    },
  }
}

export default config
