import { listPageV2 } from "@/api/workOrder/orderType";
import { getDataDictionary } from '@/api/dataDictionary.js'

export default {
  data() {
    return {
      orderSmallTypeData: [],
      orderChannels: []
    }
  },
  computed: {
    serviceInfo() {
      return [
        {
          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 = ''
              }
            }
          }
        },
        {
          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
          }
        },
        {
          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 = ''
              }
            }
          }
        },
        // ...(() => {
        //   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: {
            disabled: !this.formOptions.appointmentTime.isEdit,
            style: { width: '100%' },
            placeholder: '请选择',
            'value-format': 'yyyy-MM-dd HH:mm:ss',
            'picker-options': {
              disabledDate: time => {
                return time.getTime() < (Date.now() - 86400000)
              }
            }
          },
          formItemAttributes: {
            label: '预约上门日期',
            prop: 'appointmentTime',
            rules: this.formOptions.appointmentTime.isRules
          }
        },
        {
          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-input',
          md: 6,
          attributes: {
            disabled: !this.formOptions.source.isEdit,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '工单来源',
            prop: 'source',
            rules: this.formOptions.source.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
      }))
    })
  },
}
