import { getWebsit } from "@/api/customerManagement.js"
import { memberListPageV2 } from "@/api/masterManagement";

export default {
  data() {
    return {
      websitList: [],
      workerList: []
    }
  },
  computed: {
    workersColumns() {
      return [
        {
          columnAttributes: {
            label: '工程师信息',
            prop: 'workerName'
          }
        },
        {
          columnAttributes: {
            label: '工程师类型',
            prop: 'isMaster'
          },
          render: (h, { row, column, index }) => {
            return (
              <div style="padding-left:10px">
                {row.isMaster ? '主要工程师' : '辅助工程师'}
              </div>
            )
          }
        },
        {
          columnAttributes: {
            label: '联系电话',
            prop: 'workerMobile'
          }
        },
        {
          columnAttributes: {
            label: '身份证',
            prop: 'workerIdcard'
          }
        },
        ...(() => {
          if (this.formOptions.orderWorkers.isEdit) {
            return [
              {
                columnAttributes: {
                  label: '操作',
                },
                render: (h, { row, column, index }) => {
                  return !row.isMaster ? (
                    <div style="padding-left:10px">
                      <el-button type="text" onClick={() => {
                        this.orderInfo.orderWorkers.splice(index, 1)
                      }}>删除</el-button>
                    </div>
                  ) : null
                }
              },
            ]
          }
          return []
        })()
      ]
    },
    pandanxinxi() {
      return [
        {
          isShow: this.formOptions.websitId.isShow,
          name: 'el-select',
          options: this.websitList,
          md: 8,
          attributes: {
            disabled: !this.formOptions.websitId.isEdit,
            placeholder: '请选择',
            clearable: true,
            filterable: true
          },
          formItemAttributes: {
            label: '网点名称',
            prop: 'websitId',
            rules: this.formOptions.websitId.isRules,
          },
          events: {
            change: (val) => {
              this.orderInfo.workerId = ""
              this.orderInfo.workerName = ""
              this.orderInfo.workerIdcard = ""
              this.orderInfo.workerMobile = ""
              this.workerList = []
              this.orderInfo.orderWorkers = []
              this.getWorkers()
              if (val) {
                this.orderInfo['websitName'] = this.websitList.find(item => item.value == val).label
              } else {
                this.orderInfo['websitName'] = ''
              }
            }
          }
        },
        {
          name: 'slot-component',
          md: 16,
          formItemAttributes: { label: '', prop: '' },
          render: (h, { props }) => {
            return null
          }
        },
        ...(() => {
          if (this.formOptions.workerId.isEdit) {
            return [{
              isShow: this.formOptions.workerId.isShow,
              name: 'el-select',
              options: this.workerList.filter(item => this.orderInfo.workerId == item.value || !~this.orderInfo.orderWorkers.map(item => item.workerId).indexOf(item.value)),
              md: 8,
              attributes: {
                disabled: !this.formOptions.workerId.isEdit,
                placeholder: '请选择',
                clearable: true,
                filterable: true
              },
              formItemAttributes: {
                label: '主要工程师',
                prop: 'workerId',
                rules: this.formOptions.workerId.isRules,
              },
              events: {
                change: (val) => {
                  var index = this.orderInfo.orderWorkers.map(item => item.isMaster).indexOf(true)
                  if (!!~index) {
                    this.orderInfo.orderWorkers.splice(index, 1)
                  }
                  if (val) {
                    var data = this.workerList.find(item => item.value == val).data
                    this.orderInfo.workerName = data.workerName
                    this.orderInfo.workerIdcard = data.workerIdcard
                    this.orderInfo.workerMobile = data.workerMobile
                    this.orderInfo.orderWorkers.unshift({ ...data, isMaster: true })
                    if (data.slaveWorkerId) {
                      var data2 = this.workerList.find(item => item.value == data.slaveWorkerId).data
                      this.orderInfo.orderWorkers.push({ ...data2, isMaster: false })
                      this.orderInfo.workerId_fz = ""
                    }
                    if (this.orderInfo.orderWorkers.length > 1) {
                      this.orderInfo.orderWorkers.map((item, index) => {
                        if (index > 0) {
                          item.isMaster = false
                        }
                      })
                    }
                  } else {
                    this.orderInfo.workerName = ""
                    this.orderInfo.workerIdcard = ""
                    this.orderInfo.workerMobile = ""
                  }
                }
              }
            }]
          }
          return []
        })(),
        ...(() => {
          if (this.formOptions.orderWorkers.isEdit) {
            return [{
              isShow: this.formOptions.orderWorkers.isEdit,
              name: 'el-select',
              options: this.workerList.filter(item => !~this.orderInfo.orderWorkers.map(item => item.workerId).indexOf(item.value)),
              md: 8,
              attributes: {
                disabled: !this.formOptions.orderWorkers.isEdit,
                placeholder: '请选择',
                clearable: true,
                filterable: true
              },
              formItemAttributes: {
                label: '辅助工程师',
                prop: 'workerId_fz',
              },
              events: {
                change: (val) => {
                  if (val) {
                    var data = this.workerList.find(item => item.value == val).data
                    this.orderInfo.orderWorkers.push({ ...data, isMaster: false })
                    this.orderInfo.workerId_fz = ""
                  }
                }
              }
            }]
          }
          return []
        })(),
        {
          isShow: this.formOptions.orderWorkers.isShow,
          name: 'slot-component',
          md: 24,
          formItemAttributes: {
            label: '已选工程师',
            prop: 'orderWorkers',
            rules: this.formOptions.orderWorkers.isRules,
          },
          render: (h, { props }) => {
            var { formData } = props
            return (
              <div>
                <zj-table
                  isDrop={true}
                  columns={this.workersColumns}
                  tableData={this.orderInfo.orderWorkers}
                ></zj-table>
              </div>
            )
          }
        }
      ]
    }
  },
  created() {
    // 普通工单选择网点数据
    if (!!~[1,2,3].indexOf(this?.workOrderType) || this.isAllWebsit) {
      getWebsit({ type: "C", status: true, isAll: this.isAllWebsit || this.id ? true : false }).then(res => {
        this.websitList = res.data.map(item => ({
          label: item.name,
          value: item.websitId,
          data: item
        }))
      })
    }
  },
  methods: {
    getWorkers() {
      if (this.orderInfo.websitId) {
        memberListPageV2({ "pageNum": 1, "pageSize": -1, "params": [{ "param": "b.examine_status", "compare": "=", "value": "OK" }, { "param": "b.websit_id", "compare": "=", "value": this.orderInfo.websitId }] }).then(res => {
          this.workerList = res.data.records.map(item => {
            var { id, userId, ...data } = item
            return {
              value: userId,
              label: data.nickName,
              data: {
                workerId: userId,
                workerName: data.nickName,
                workerIdcard: data.idCard,
                workerMobile: data.mobile,
                ...data
              }
            }
          })
        })
      }
    }
  },
}
