import { listPageV2 } from '@/api/workOrder/orderType'
import { getDataDictionary } from '@/api/dataDictionary.js'
import { getWebsit } from '@/api/customerManagement.js'

export default {
  data() {
    return {
      orderSmallTypeData: [],
      orderChannels: [],
      WORKER_ORDER_SOURCE: [],
      createWebsitList: []
    }
  },
  computed: {
    serviceInfo() {
      return [
        {
          isShow: this.formOptions.createWebsitId.isShow,
          name: 'el-select',
          md: 6,
          options: this.createWebsitList,
          attributes: {
            disabled: !this.formOptions.createWebsitId.isEdit,
            placeholder: '请选择',
            clearable: true,
            filterable: true
          },
          formItemAttributes: {
            label: '创建网点',
            prop: 'createWebsitId',
            rules: this.formOptions.createWebsitId.isRules
          },
          events: {
            change: val => {
              if (val) {
                this.orderInfo.createWebsitName = this.createWebsitList.find(item => item.value == val).label
              } else {
                this.orderInfo.createWebsitName = ''
              }
            }
          }
        },
        {
          isShow: this.formOptions.serviceStatus.isShow,
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !this.formOptions.serviceStatus.isEdit,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '服务状态',
            prop: 'serviceStatus',
            rules: this.formOptions.serviceStatus.isRules
          }
        },
        // -------------------------------------------------
        {
          isShow: this.formOptions.orderSmallType.isShow && !this.id,
          name: 'el-select',
          md: 6,
          options: this.orderSmallTypeData,
          attributes: {
            disabled: !this.formOptions.orderSmallType.isEdit,
            placeholder: '请选择',
            clearable: true,
            filterable: true
          },
          formItemAttributes: {
            label: '工单类型',
            prop: 'orderSmallType',
            rules: this.formOptions.orderSmallType.isRules
          },
          events: {
            change: val => {
              if (val) {
                this.orderInfo.orderSmallTypeText = this.orderSmallTypeData.find(item => item.value == val).label
              } else {
                this.orderInfo.orderSmallTypeText = ''
              }
              this.getWebsitFun(true)
            }
          }
        },
        {
          isShow: this.formOptions.orderSmallType.isShow && this.id,
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !this.formOptions.orderSmallType.isEdit
          },
          formItemAttributes: {
            label: '工单类型',
            prop: 'orderSmallTypeText',
            rules: this.formOptions.orderSmallType.isRules
          }
        },
        // ...(() => {
        //   if (this.workOrderType == 1) {
        //     // 普通工单
        //     return [{
        //       isShow: this.formOptions.orderChannelId.isShow,
        //       name: 'el-select',
        //       md: 6,
        //       options: this.orderChannels,
        //       attributes: {
        //         disabled: !this.formOptions.orderChannelId.isEdit,
        //         placeholder: '请选择',
        //         clearable: true,
        //         filterable: true
        //       },
        //       formItemAttributes: {
        //         label: '工单来源',
        //         prop: 'orderChannelId',
        //         rules: this.formOptions.orderChannelId.isRules
        //       },
        //       events: {
        //         change: (val) => {
        //           if (val) {
        //             this.orderInfo.orderChannelText = this.orderChannels.find(item => item.value == val).label
        //           } else {
        //             this.orderInfo.orderChannelText = ''
        //           }
        //         }
        //       }
        //     }]
        //   } else if (this.workOrderType == 4) {
        //     this.orderInfo.orderChannelId = "工程维保"
        //     // 维保工单
        //     return [{
        //       isShow: this.formOptions.orderChannelId.isShow,
        //       name: 'el-input',
        //       md: 6,
        //       attributes: {
        //         disabled: !this.formOptions.orderChannelId.isEdit,
        //       },
        //       formItemAttributes: {
        //         label: '工单来源',
        //         prop: 'orderChannelId',
        //         rules: this.formOptions.orderChannelId.isRules
        //       }
        //     }]
        //   }
        // })(),
        {
          isShow: this.formOptions.appointmentTime.isShow,
          name: 'el-date-picker',
          md: 6,
          attributes: {
            'picker-options': 'pickerOptions11',
            'popper-class': 'custom-popper11',
            'append-to-body': true,
            disabled: !this.formOptions.appointmentTime.isEdit,
            style: { width: '100%' },
            type: 'datetime',
            placeholder: '请选择',
            'value-format': 'yyyy-MM-dd HH:mm:ss',
            'picker-options': {
              disabledDate: time => {
                return time.getTime() < Date.now() - 86400000
              }
            }
          },
          events: {
            change: value => {
              if (value && new Date(value).getTime() < new Date().getTime() - 2000) {
                this.orderInfo.appointmentTime = ''
                this.$message.warning('必须大于当前时间')
              } else {
                this.orderInfo.appointmentEndTime = ''
              }
            }
          },
          formItemAttributes: {
            label: '预约上门开始时间',
            prop: 'appointmentTime',
            rules: this.formOptions.appointmentTime.isRules
          }
        },
        {
          isShow: this.formOptions.appointmentTime.isShow,
          name: 'el-date-picker',
          md: 6,
          attributes: {
            'picker-options': 'pickerOptions11',
            'popper-class': 'custom-popper11',
            'append-to-body': true,
            disabled: !this.formOptions.appointmentTime.isEdit,
            style: { width: '100%' },
            type: 'datetime',
            placeholder: '请选择',
            'value-format': 'yyyy-MM-dd HH:mm:ss',
            'picker-options': {
              disabledDate: time => {
                var data = this.orderInfo?.appointmentTime?.split(' ')?.[0]
                return (
                  time.getTime() < new Date(data ? `${data} 00:00:00` : '').getTime() ||
                  time.getTime() > new Date(data ? `${data} 23:59:59` : '').getTime()
                )
              }
            }
          },
          events: {
            change: value => {
              if (value && new Date(value).getTime() < new Date().getTime() - 2000) {
                this.orderInfo.appointmentEndTime = ''
                this.$message.warning('必须大于当前时间')
              } else if (value && new Date(value).getTime() < new Date(this.orderInfo?.appointmentTime).getTime()) {
                this.orderInfo.appointmentTime = value
              }
            }
          },
          formItemAttributes: {
            label: '预约上门结束时间',
            prop: 'appointmentEndTime',
            rules: this.formOptions.appointmentTime.isRules
          }
        },
        {
          isShow: this.formOptions.orderChannelId.isShow,
          name: 'el-select',
          md: 6,
          options: this.orderChannels,
          echoInsertion: true,
          attributes: {
            disabled: !this.formOptions.orderChannelId.isEdit,
            placeholder: '请选择',
            clearable: true,
            filterable: true
          },
          formItemAttributes: {
            label: '工单渠道',
            prop: 'orderChannelId',
            propName: 'orderChannelText',
            rules: this.formOptions.orderChannelId.isRules
          },
          events: {
            change: val => {
              if (val) {
                this.orderInfo.orderChannelText = this.orderChannels.find(item => item.value == val).label
              } else {
                this.orderInfo.orderChannelText = ''
              }
              this.getWebsitFun(true)
            }
          }
        },
        {
          isShow: this.formOptions.saleType.isShow,
          name: 'el-select',
          md: 6,
          options: [
            { value: 1, label: '零售' },
            { value: 2, label: '工程' },
            { value: 3, label: '延保' },
            { value: 4, label: '工程维保' }
          ],
          attributes: {
            disabled: !this.formOptions.saleType.isEdit,
            placeholder: '请选择',
            clearable: true,
            filterable: true
          },
          formItemAttributes: {
            label: '销售类型',
            prop: 'saleType',
            rules: this.formOptions.saleType.isRules
          }
        },
        {
          isShow: this.formOptions.saleCompany.isShow,
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !this.formOptions.saleCompany.isEdit,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '销售单位',
            prop: 'saleCompany',
            rules: this.formOptions.saleCompany.isRules
          }
        },

        {
          isShow: this.formOptions.source.isShow,
          name: 'el-select',
          md: 6,
          options: this.WORKER_ORDER_SOURCE,
          echoInsertion: true,
          attributes: {
            disabled: !this.formOptions.source.isEdit,
            placeholder: '请选择',
            clearable: true,
            filterable: true
          },
          formItemAttributes: {
            label: '工单来源',
            prop: 'source',
            propName: 'source',
            rules: this.formOptions.source.isRules
          },
          events: {
            change: val => {
              this.getWebsitFun(true)
            }
          }
        },

        // {
        //   isShow: this.formOptions.source.isShow,
        //   name: 'el-input',
        //   md: 6,
        //   attributes: {
        //     disabled: !this.formOptions.source.isEdit,
        //     placeholder: '请输入'
        //   },
        //   formItemAttributes: {
        //     label: '工单来源',
        //     prop: 'source',
        //     rules: this.formOptions.source.isRules
        //   }
        // },
        {
          isShow: this.formOptions.salesPlatform.isShow,
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !this.formOptions.salesPlatform.isEdit,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '销售平台',
            prop: 'salesPlatform',
            rules: this.formOptions.salesPlatform.isRules
          }
        },
        {
          isShow: this.formOptions.salesOrderId.isShow,
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !this.formOptions.salesOrderId.isEdit,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '销售单号',
            prop: 'salesOrderId',
            rules: this.formOptions.salesOrderId.isRules
          }
        },
        {
          isShow: this.formOptions.serviceOrderId.isShow,
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !this.formOptions.serviceOrderId.isEdit,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '服务单号',
            prop: 'serviceOrderId',
            rules: this.formOptions.serviceOrderId.isRules
          }
        },
        {
          isShow: this.formOptions.urgent.isShow,
          name: 'el-select',
          md: 6,
          options: [
            { value: '一般', label: '一般' },
            { value: '急', label: '急' },
            { value: '紧急', label: '紧急' }
          ],
          echoInsertion: true,
          attributes: {
            clearable: true,
            filterable: true,
            disabled: !this.formOptions.urgent.isEdit,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '紧急程度',
            prop: 'urgent',
            propName: 'urgent',
            rules: this.formOptions.urgent.isRules
          }
        },
        {
          isShow: this.formOptions.complaint.isShow,
          name: 'el-select',
          md: 6,
          options: [
            { value: '特级', label: '特级' },
            { value: '一级', label: '一级' },
            { value: '二级', label: '二级' },
            { value: '三级', label: '三级' }
          ],
          echoInsertion: true,
          attributes: {
            clearable: true,
            filterable: true,
            disabled: !this.formOptions.complaint.isEdit,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '投诉等级',
            prop: 'complaint',
            propName: 'complaint',
            rules: this.formOptions.complaint.isRules
          }
        },
        {
          isShow: this.formOptions.evaluate.isShow,
          name: 'el-select',
          md: 6,
          options: [
            { value: '满意（默认）', label: '满意（默认）' },
            { value: '好评', label: '好评' },
            { value: '满意', label: '满意' },
            { value: '中评', label: '中评' },
            { value: '差评', label: '差评' }
          ],
          echoInsertion: true,
          attributes: {
            clearable: true,
            filterable: true,
            disabled: !this.formOptions.evaluate.isEdit,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '评价结果',
            prop: 'evaluate',
            propName: 'evaluate',
            rules: this.formOptions.evaluate.isRules
          }
        },
        {
          isShow: this.formOptions.reqStatus.isShow,
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !this.formOptions.reqStatus.isEdit,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '要求状态',
            prop: 'reqStatus',
            rules: this.formOptions.reqStatus.isRules
          }
        },
        {
          isShow: this.formOptions.reqType.isShow,
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !this.formOptions.reqType.isEdit,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '要求类型',
            prop: 'reqType',
            rules: this.formOptions.reqType.isRules
          }
        },
        {
          isShow: this.formOptions.reqText.isShow,
          name: 'el-input',
          md: 12,
          attributes: {
            disabled: !this.formOptions.reqText.isEdit,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '要求内容',
            prop: 'reqText',
            rules: this.formOptions.reqText.isRules
          }
        }
      ]
    }
  },
  created() {
    // 普通工单选择网点数据
    if (!!~[1, 2, 3].indexOf(this?.workOrderType)) {
      // 获取工单类型
      listPageV2({ pageNum: 1, pageSize: -1, params: [{ param: 'a.status', compare: '=', value: 'true' }] }).then(
        res => {
          this.orderSmallTypeData = res.data.records.map(item => ({
            value: item.id,
            label: item.orderSmallTypeText
          }))
        }
      )
    }

    // 获取工单渠道
    getDataDictionary({
      pageNum: 1,
      pageSize: -1,
      params: [
        { param: 'a.status', compare: '=', value: 'ON' },
        { param: 'a.dict_type', compare: '=', value: 'ORDER_CHANNEL' }
      ]
    }).then(res => {
      this.orderChannels = res.data.records.map(item => ({
        value: item.dictCode,
        label: item.dictValue
      }))
    })

    // 获取工单来源
    getDataDictionary({
      pageNum: 1,
      pageSize: -1,
      params: [
        { param: 'a.status', compare: '=', value: 'ON' },
        { param: 'a.dict_type', compare: '=', value: 'WORKER_ORDER_SOURCE' }
      ]
    }).then(res => {
      this.WORKER_ORDER_SOURCE = res.data.records.map(item => ({
        value: item.dictValue,
        label: item.dictValue
      }))
    })

    getWebsit({ status: true }).then(res => {
      this.createWebsitList = res.data.map(item => ({
        value: item.websitId,
        label: item.name
      }))
    })
  }
}
