<template>
  <div>
    <el-button
      type="primary"
      icon="Edit"
      @click="openApproverEdit"
    >审核人编辑</el-button>
    <el-dialog
      v-model="editApproverShow"
      title="编辑审核人"
      width="40%"
    >
      <el-tabs type="border-card">
        <el-tab-pane label="新增与修改">
          <el-alert
            :title="prompt"
            type="info"
            :closable="false"
          />
          <el-form
            label-width="90px"
            label-position="left"
            size="large"
            style="margin-top: 20px"
          >
            <el-form-item label="审核人：">
              <el-select
                v-model="approverId"
                placeholder="请选择人员名称"
                filterable
                clearable
                @clear="clearApproverId"
                @change="changeApproverId"
              >
                <el-option
                  v-for="item in approverList"
                  :key="item.ID"
                  :label="item.user.nickName"
                  :value="item.ID"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="用户：">
              <el-select
                v-model="approverInfo.userId"
                placeholder="请选择人员名称"
                filterable
              >
                <el-option
                  v-for="item in userList"
                  :key="item.ID"
                  :label="item.nickName"
                  :value="item.ID"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="详情：">
              <el-input
                v-model="approverInfo.approverDescription"
                placeholder="请输入审核人详情"
                clearable
              />
            </el-form-item>
            <el-form-item label="权限详情：">
              <el-input
                v-model="approverInfo.approverRightsDescription"
                placeholder="请输入审核人权限详情"
                clearable
              />
            </el-form-item>
            <el-row
              justify="end"
              style="margin-top: 40px"
            >
              <el-col :span="2">
                <el-button
                  type="primary"
                  size="large"
                  @click="editApprover"
                >
                  确认
                </el-button>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="删除">
          <el-row justify="center">
            <el-col :span="10">
              <el-select
                v-model="delApproverId"
                placeholder="请选择审核人员"
                size="large"
                filterable
              >
                <el-option
                  v-for="item in approverList"
                  :key="item.ID"
                  :label="item.user.nickName"
                  :value="item.ID"
                />
              </el-select>
            </el-col>
            <el-col
              :span="3"
              :offset="2"
            >
              <el-button
                type="primary"
                size="large"
                @click="delApprover"
              >删除</el-button>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue'
import { getApprovePerson, postApprovePerson, putApprovePerson, deleteApprovePerson } from '@/api/approver'
import { getAllUsers } from '@/api/user'
import { ElMessage, ElMessageBox } from 'element-plus'

defineOptions({
  name: 'ApproveList'
})
onMounted(() => {
  // queryApproveList()
  queryApprover()
  queryAllUsers()
})
// 查询审核人列表
const approverList = reactive([])
const queryApprover = () => {
  getApprovePerson().then(res => {
    if (res.code === 0) {
      approverList.length = 0
      approverList.push(...res.data)
      if (approverList.length !== 0) {
        delApproverId.value = approverList[0].ID
      } else {
        delApproverId.value = 0
      }
    }
  })
}
// .................................................................................
// 新增、编辑审核人
const prompt = '注意：如果不选择审核人则默认为添加审核人，如果选择审核人则默认修改该审核人信息'
const approverId = ref('')
const approverInfo = reactive({
  id: 0,
  userId: 0,
  approverDescription: '',
  approverRightsDescription: ''
})
const editApproverShow = ref(false)
const editApprover = () => {
  if (approverId.value === '') {
    // 新增审批人
    approverInfo.id = 0
    const add = {
      userId: approverInfo.userId,
      approverDescription: approverInfo.approverDescription,
      approverRightsDescription: approverInfo.approverRightsDescription
    }
    postApprovePerson(add).then(res => {
      if (res.code === 0) {
        ElMessage.success('添加成功')
        queryApprover()
      }
    })
  } else {
    // 修改审批人
    approverInfo.id = approverId.value
    const modify = {
      id: approverInfo.id,
      userId: approverInfo.userId,
      approverDescription: approverInfo.approverDescription,
      approverRightsDescription: approverInfo.approverRightsDescription
    }
    putApprovePerson(modify).then(res => {
      if (res.code === 0) {
        ElMessage.success('修改成功')
        queryApprover()
      }
    })
  }
}
const clearApproverId = () => {
  approverId.value = ''
  approverInfo.id = 0
  approverInfo.userId = userList[0].ID
  approverInfo.approverDescription = ''
  approverInfo.approverRightsDescription = ''
}

const changeApproverId = (val) => {
  approverList.forEach(item => {
    if (item.ID === val) {
      approverInfo.userId = item.user.ID
      approverInfo.approverDescription = item.approverDescription
      approverInfo.approverRightsDescription = item.approverRightsDescription
    }
  })
}
// ..............................................................................
// 获取审批列表
// const condition = reactive({
//   page: 1,
//   pageSize: 10
// })
// const queryApproveList = () => {
//   getApproveList(condition).then(res => {
//     if (res.code === 0) {
//       console.log('审核列表', res)
//     }
//   })
// }

// 打开审核人编辑窗口
const openApproverEdit = () => {
  editApproverShow.value = true
  approverInfo.userId = userList[0].ID
  if (approverList.length !== 0) {
    delApproverId.value = approverList[0].ID
  }
}
// .................................................................................
// 删除审核人
const delApproverId = ref(0)
const delApprover = () => {
  if (approverList.length === 0) {
    ElMessage.error('暂无审核人')
    return
  }
  ElMessageBox.confirm(
    '你确定进行删除此审核人吗?',
    '删除',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      const deleteId = {
        id: delApproverId.value
      }
      deleteApprovePerson(deleteId).then(res => {
        if (res.code === 0) {
          ElMessage.success('删除成功')
          queryApprover()
          console.log(approverList.length)
        }
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
// .................................................................................
// 获取用户列表
const userList = reactive([])
const queryAllUsers = () => {
  getAllUsers().then(res => {
    if (res.code === 0) {
      userList.length = 0
      userList.push(...res.data)
    }
  })
}
</script>

<style scoped>

</style>
