<template>
  <el-dialog
    class="modal-wrap"
    append-to-body
    :title="titleName"
    :visible.sync="visible"
    :width="width"
    :before-close="onClose"
    :destroy-on-close="destroyOnClose"
  >
    <div class="modal-cnt">
      <el-form :model="allocForm" :rules="allocRules" ref="allocForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分配人员" prop="userValue">
          <el-select v-model="allocForm.userValue" filterable placeholder="请选择">
            <el-option v-for="option in selectOptions" :key="option.userId" :label="option.nickName" :value="option.userId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="operate-btn">
          <el-button type="primary" @click="onSubmit('allocForm')">提交</el-button>
          <el-button @click="resetForm('allocForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import { getUserList, upanddel } from '@/api/grid/list/gridlist'

export default {
  components: {},
  props: {
    className: {
      type: String,
      default: ''
    },
    titleName: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: '500px'
    },
    destroyOnClose: {
      type: Boolean,
      default: true
    },
    detailArr: {
      type: Array,
      default: () => []
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.getListData()
      }
    }
  },
  computed: {
    checkList() {
      if (isEmpty(this.indicatorsGroupName.trim())) {
        return this.listIndicatorsGroup
      }
      return this.listIndicatorsGroup.filter((item) => item.indicatorsGroupName.includes(this.indicatorsGroupName.trim()))
    }
  },
  data() {
    return {
      visible: false,
      selectOptions: [],
      allocForm: {
        userValue: ''
      },
      allocRules: {
        userValue: [{ required: true, message: '请选择分配人员', trigger: 'change' }]
      }
    }
  },
  methods: {
    async getListData() {
      const res = await getUserList({
        deptIds: this.detailArr?.map((ot) => ot.belongBranchId)?.join(',')
      })
      this.selectOptions = res?.data || []
    },
    onClose() {
      this.visible = false
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    async onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.doAlloc()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async doAlloc() {
      const resultObj = this.selectOptions.find((ot) => ot.userId === this.allocForm.userValue)
      const newArr = this.detailArr?.map((item) => ({
        custIds: item.custIds,
        custPatterns: item.custPatterns,
        belongUserNames: resultObj.nickName,
        belongUserIds: resultObj.userName
      }))
      const res = await upanddel({
        belongUserNames: newArr?.map((ot) => ot.belongUserNames),
        belongUserIds: newArr?.map((ot) => ot.belongUserIds),
        custIds: newArr?.map((ot) => ot.custIds),
        custPatterns: newArr?.map((ot) => ot.custPatterns)
      })
      if (res.code === 200) {
        this.$message.success(res.msg)
        this.resetForm('allocForm')
        this.onClose()
        this.$emit('onRefresh')
      } else {
        this.$message.error(res.msg)
      }
    },
    onOpen() {
      this.visible = true
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: center;
}
::v-deep .operate-btn {
  margin-top: 70px;
}
</style>