<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="search-box">
      <span>搜索分配人员</span>
      <el-input placeholder="输入姓名或者工号" suffix-icon="el-icon-search" v-model="filterText" />
    </div>
    <div class="modal-cnt">
      <el-form :model="allocForm" :rules="allocRules" ref="allocForm" label-width="0" class="demo-ruleForm">
        <!-- <el-form-item label="分配人员" prop="userValue">
          <el-select v-model="allocForm.userValue" filterable placeholder="请选择" :multiple="dutyType === '2'">
            <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 label="" prop="isSuiji" class="suiji">
          <el-checkbox v-model="allocForm.isSuiji" class="checkbox">随机平均分配</el-checkbox>
        </el-form-item>
        <el-form-item label="" prop="userValue">
          <el-tree
            v-model="allocForm.userValue"
            :data="treeData"
            show-checkbox
            node-key="id"
            default-expand-all
            :filter-node-method="filterNode"
            ref="tree"
          />
        </el-form-item>
      </el-form>
    </div>
    <div class="operate-btn">
      <el-button @click="resetForm('allocForm')">重置</el-button>
      <el-button type="primary" @click="onSubmit('allocForm')">提交</el-button>
    </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: () => []
    },
    dutyType: {
      type: String,
      default: '1'
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.getListData()
      }
    },
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  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: '',
        isSuiji: false
      },
      allocRules: {
        userValue: [{ required: true, message: '请选择分配人员', trigger: 'change' }]
      },
      treeData: [{
        id:1,
        label: '耳机',
        children: [{
        id:11,
        label: 'xx',
        }]
      },{
        id:2,
        label: '单点',
        children: [{
        id:22,
        label: '搜索',
        }]
      },{
        id:3,
        label: '司宿舍',
        children: [{
        id:33,
        label: '司宿舍司宿舍',
        }]
      }],
      filterText: ''
    }
  },
  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
        }
      })
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1 || String(data.id).indexOf(value) !== -1
    },
    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;
}
.modal-cnt{
  max-height: 600px;
  overflow-y: scroll;
  overflow-x: hidden;
}
::v-deep .operate-btn {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
.modal-wrap {
  v-deep .el-tree-node__expand-icon.is-leaf {
    color: transparent !important
  }
}
.search-box{
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  span{
    width: 140px;
  }
}
.suiji{
  margin-bottom: 0;
  .checkbox{
    color: #222;
    font-weight: bold;
  }
}
</style>