import { remote } from './remote'
import { grid } from './common'
import {
  genWarehouseSearchModal,
  genWarehouseRoomSearchModal,
  genWarehouseAreaSearchModal,
  genBldgSearchModal, genCustomerSearchModal, genCargoownerAuthSearchModal
} from '../../../SearchModal'
import { upcloud_base_wh,upcloud_base_common, upcloud_base_chain} from "@/config/serviceSettings";

const items = function(self) {
  return [
    /* ...genwarehouse(grid), */
    // 仓库
    ...genWarehouseSearchModal(
      1,
      true,
      false,
      null,
      function(rowData, form) {
        if (!rowData) {
          form.setFieldsValue({
            dcId: '',
            dcCode: '',
            dcName: ''
          })
        } else {
          form.setFieldsValue({
            dcId: rowData[0].dcId,
            dcCode: rowData[0].dcCode,
            dcName: rowData[0].dcName
          })
          let item = self.items.find(t => t.name == 'stageType')
          self.getTypeList(rowData[0].warehouseId, item)
          self.getShipline(rowData[0].warehouseCode, self.items)
        }
      },
      { span: 8, labelCol: { span: 6 } }
    ),
    {
      type: 'select',
      name: 'isEnable',
      label: '状态',
      options: [
        {
          label: '启用',
          value: 1
        },
        {
          label: '禁用',
          value: 0
        }
      ],
      ...grid
    },
    {
      type: 'select',
      name: 'stageType',
      label: '类型',
      options: [],
      // ...remote('pub_workstage_stagetype'),
      ...grid
    },
    {
      type: 'select',
      name: 'shiplineId',
      label: '配送方向',
      ...remote('common_shiplineid'),
      // options: [],
      ...grid
    },
    {
      type: 'select',
      name: 'deliveryType',
      label: '提货方式',
      // ...remote('pub_workstage_delivertype'),
      remoteUrl: {
        url: upcloud_base_common+`/dict/listbytype/?dicttypeCodes=pub_workstage_delivertype`,
        method: 'post'
      },
      replaceFields: {
        label: 'dictName',
        value: 'dictName'
      },
      ...grid
    },
    // {
    //   type: 'input',
    //   name: 'tunnelno',
    //   label: '巷道',
    //   value: '',
    //   ...grid
    // },
    {
      span: 8,
      type: 'buttonGroup',
      // style: { textAlign: 'right' },
      options: [
        {
          value: '查询',
          icon: 'search',
          btnType: 'primary',
          style: 'margin-right:10px',
          htmlType: 'submit'
        },
        {
          value: '重置',
          name: 'ccz',
          icon: 'redo',
          on: {
            click(event, form) {
              form.resetFields()
            }
          }
        }
      ]
    }
  ]
}

const modalItems = function(self) {
  let areas = genWarehouseAreaSearchModal(
    1,
    true,
    false,
    function(res, p) {
      let vals = self.$refs.modelForm.form.getFieldsValue()
      p.params.warehouseId = vals.warehouseId
      p.params.bldgId = vals.bldgId
      p.params.warehouseRoomId = vals.warehouseRoomId
      return p
    },
    function (rowData, form){
      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: rowData ? rowData[0].warehouseRoomId : '',
        warehouseRoomCode: rowData ? rowData[0].warehouseRoomCode : '',
        warehouseRoomName: rowData ? rowData[0].warehouseRoomName : '',
      })
    },
    { span: 8, labelCol: { span: 6 } }
  )
  areas[1].name = 'warehouseAreaCode'
  areas[2].name = 'warehouseAreaName'
  areas[2].targetValueKey = 'warehouseAreaName'
  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(rowData, form) {
        form.setFieldsValue({
          dcId: rowData ? rowData[0].dcId : '',
          dcCode: rowData ? rowData[0].dcCode : '',
          dcName: rowData ? rowData[0].dcName : '',
          bldgId: '',
          bldgCode: '',
          bldgName: '',
          warehouseRoomId: '',
          warehouseRoomCode: '',
          warehouseRoomName: '',
          warehouseAreaId: '',
          warehouseAreaCode: '',
          warehouseAreaName: '',
        })
        if (rowData) {
          let item = self.modalItems.find(t => t.name == 'stageType')
          self.getTypeList(rowData[0].warehouseId, item, true)
          self.getShipline(rowData[0].warehouseCode, self.modalItems)
        }
      },
      { span: 8, labelCol: { span: 6 } }
    ),
    // 建筑物
    ...genBldgSearchModal(
      1,
      true,
      false,
      function(res, p) {
        let vals = self.$refs.modelForm.form.getFieldsValue()
        p.params.warehouseId = vals.warehouseId
        return p
      },
      function (rowData, form){
        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 : '',
          warehouseRoomId: '',
          warehouseRoomCode: '',
          warehouseRoomName: '',
          warehouseAreaId: '',
          warehouseAreaCode: '',
          warehouseAreaName: '',
        })
      },
      { span: 8, labelCol: { span: 6 } }
    ),
    // 库房
    ...genWarehouseRoomSearchModal(
      1,
      true,
      false,
      function(res, p) {
        let vals = self.$refs.modelForm.form.getFieldsValue()
        p.params.warehouseId = vals.warehouseId
        p.params.bldgId = vals.bldgId
        return p
      },
      function (rowData, form){
        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 : '',
          warehouseAreaId: '',
          warehouseAreaCode: '',
          warehouseAreaName: '',
        })
      },
      { span: 8, labelCol: { span: 6 } }
    ),
    // 库区
    ...areas,
    {
      type: 'input',
      name: 'stageCode',
      label: '集货区编码',
      required: true
    },
    {
      type: 'select',
      name: 'stageType',
      label: '集货区类型',
      required: true,
      options: []
    },
    { type: 'input', name: 'segment1', label: 'shiplineCode', value: '', hidden: true },
    { type: 'input', name: 'segment2', label: 'shiplineName', value: '', hidden: true },
    {
      type: 'select',
      name: 'shiplineId',
      label: '配送方向',
      required: true,
      ...remote('common_shiplineid'),
      // options: [],
      on: {
        change(value,label) {
          if(value) {
            self.$refs.modelForm.form.setFieldsValue({
              segment1: value,
              segment2: label
            })
          }else {
            self.$refs.modelForm.form.setFieldsValue({
              segment1: '',
              segment2: ''
            })
          }
          // let item = self.shiplineList.find(t => t.shiplineId == value)

        }
      }
    },
    {
      type: 'select',
      name: 'deliveryType',
      label: '提货方式',
      required: true,
      ...remote('pub_workstage_delivertype')
    },
    {
      type: 'input',
      name: 'customerId',
      label: '客户id',
      hidden: true
    },
    {
      type: 'searchModal',
      name: 'customerName',
      label: '客户名称',
      remoteUrl: { url:upcloud_base_chain+'/baseCustomerMst/v1.0/page', method: 'post' },
      rowKey: 'customerId',
      targetValueKey: 'customerName',
      hidden: true,
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'customerCode',
            label: '客户编码'
          },
          {
            type: 'input',
            name: 'customerName',
            label: '客户名称'
          }
        ]
      },
      tableConfig: {
        columns: [
          {
            dataIndex: 'customerCode',
            title: '客户编码'
          },
          {
            dataIndex: 'customerName',
            title: '客户名称'
          }
        ]
      },
      on: {
        ok(event, value, rowData, form) {
          form.setFieldsValue({
            customerId: `${rowData[0].customerId}`
          })
        }
      }
    },
    {
      type: 'input',
      name: 'numlimit',
      label: '订单数限制',
      hidden: true
    },
    {
      type: 'input',
      name: 'tunnelno',
      label: '巷道',
      hidden: true
    },
    {
      type: 'switch',
      name: 'isEnable',
      label: '状态',
      value: true,
      hidden: true
    },
    // 货主
    ...genCargoownerAuthSearchModal(1, false, false, null, null, { span: 8, labelCol: { span: 6 } })
  ]
}

export const modalEditItems = function(self) {

  return [
    {
      type: 'select',
      name: 'shiplineId',
      label: '配送方向',
      options: [],
      on: {
        change(value) {
          let item = self.shiplineList.find(t => t.shiplineId == value)
          self.$refs.modelEditType.form.setFieldsValue({
            segment1: item.shiplineCode,
            segment2: item.shiplineName
          })
        }
      }
    },
    { type: 'input', name: 'segment1', label: 'shiplineCode', value: '', hidden: true },
    { type: 'input', name: 'segment2', label: 'shiplineName', value: '', hidden: true },
    {
      type: 'select',
      name: 'deliveryType',
      label: '提货方式',
      ...remote('pub_workstage_delivertype')
    },
    {
      type: 'select',
      name: 'stageType',
      label: '暂存区类别',
      options: [],
    }
  ]
}

const TypeColumns = {
  ref: 'TypeColumnsRef',
  remoteUrl: {
    url: upcloud_base_wh + '/workstage/setting/page',
    method: 'get',
    headers: {
      'Content-Type': 'x-www-form-urlencoded;'
    }
  },
  columns: [
    {
      dataIndex: 'type',
      title: '集货区类型'
    },
    {
      dataIndex: 'maxVolNum',
      title: '最大体积'
    },
    {
      dataIndex: 'minVolNum',
      title: '最小体积'
    },
    {
      dataIndex: 'maxPieceNum',
      title: '最大件数'
    },
    {
      dataIndex: 'minPieceNum',
      title: '最小件数'
    },
    {
      dataIndex: 'minClientNum',
      title: '最小客户数'
    },
    {
      dataIndex: 'maxBillNum',
      title: '最大订单数量'
    },
    {
      dataIndex: 'action',
      title: '操作',
      scopedSlots: { customRender: 'action' },
      width: 100
    }
  ],
  items: [
    {
      type: 'input',
      name: 'likeName',
      label: '名称'
    },
    {
      type: 'select',
      name: 'category',
      label: '策略分类',
      options: [
        {
          label: '上架',
          value: 'InStock'
        },
        {
          label: '下架',
          value: 'OutStock'
        },
        {
          label: '补货',
          value: 'Replenishment'
        }
      ]
    },

    {
      span: 5,
      type: 'buttonGroup',
      style: { textAlign: 'left' },
      options: [
        {
          value: '查询',
          icon: 'search',
          btnType: 'primary',
          style: 'margin-right:10px',
          htmlType: 'submit'
        },
        {
          value: '重置',
          name: 'ccz',
          icon: 'redo',
          on: {
            click(event, form) {
              form.resetFields()
            }
          }
        }
      ]
    }
  ],
  modalItems: [
    // 仓库
    ...genWarehouseSearchModal(1, true, false, null, null, { span: 8, labelCol: { span: 12 } }),
    {
      type: 'input',
      name: 'type',
      required: true,
      label: '集货区类型'
    },

    {
      type: 'input',
      name: 'maxVolNum',
      required: true,
      label: '最大体积'
    },
    {
      type: 'input',
      name: 'minVolNum',
      required: true,
      label: '最小体积'
    },
    {
      type: 'input',
      name: 'maxPieceNum',
      required: true,
      label: '最大件数'
    },
    {
      type: 'input',
      name: 'minPieceNum',
      required: true,
      label: '最小件数'
    },

    {
      type: 'input',
      name: 'maxClientNum',
      required: true,
      label: '最大客户数量'
    },
    {
      type: 'input',
      name: 'minClientNum',
      required: true,
      label: '最小客户数'
    },
    {
      type: 'input',
      name: 'maxBillNum',
      required: true,
      label: '最大订单数量'
    },
  ]
}
const columns = [
  {
    dataIndex: 'dcName',
    title: '仓配中心',
    ellipsis:true,
    width: 100
  },
  {
    dataIndex: 'warehouseName',
    title: '仓库',
    ellipsis:true,
    width: 100
  },
  {
    dataIndex: 'bldgName',
    title: '建筑物',
    ellipsis:true,
    width: 100
  },
  {
    dataIndex: 'warehouseRoomName',
    title: '库房',
    ellipsis:true,
    width: 100
  },
  {
    dataIndex: 'warehouseAreaName',
    title: '库区',
    ellipsis:true,
    width: 100
  },
  {
    dataIndex: 'stageCode',
    title: '集货区编号',
    ellipsis:true,
    width: 100
  },
  {
    dataIndex: 'stageTypeName',
    title: '类型',
    ellipsis:true,
    width: 80
  },
  {
    dataIndex: 'segment2',
    title: '配送方向',
    ellipsis:true,
    width: 80
  },
  {
    dataIndex: 'deliveryType',
    title: '提货方式',
    ellipsis:true,
    width: 80
  },
  { dataIndex: 'maxVolNum', title: '最大体积', width: 90 },
  { dataIndex: 'minVolNum', title: '最小体积', width: 90 },
  { dataIndex: 'maxPieceNum', title: '最大件数', width: 90 },
  { dataIndex: 'minPieceNum', title: '最小件数', width: 90 },
  { dataIndex: 'maxClientNum', title: '最大客户数量', width: 90 },
  { dataIndex: 'minClientNum', title: '最小客户数量', width: 90 },
  { dataIndex: 'maxBillNum', title: '最大订单数量', width: 90 },
  {
    dataIndex: 'isEnable',
    title: '状态',
    ellipsis:true,
    width: 70,
    scopedSlots: { customRender: 'isEnable' }
  },
  {
    dataIndex: 'action',
    title: '操作',
    fixed: 'right',
    scopedSlots: { customRender: 'action' },
    width: 150
  }
]

export { items, modalItems, columns, TypeColumns }
