<!-- 审批设置 -->
<script setup lang="ts" name="ApproverDrawer">
import { computed, onMounted, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import selectRoleDialog from '../dialog/selectRoleDialog.vue'
import selectEmployeesDialog from '../dialog/selectEmployeesDialog.vue'
import $func from '@/plugins/preload'
import type { IEmployees, IroleInfo } from '@/components/dialog/dialog'

import useWorkFlowStore from '@/store/modules/workFlow'
const props = defineProps({
  directorMaxLevel: {
    type: Number,
    default: 0,
  },
})
const emits = defineEmits(['update:nodeConfig'])

const workFlowStore = useWorkFlowStore()

const approverConfig = ref({}) as any
const approverVisible = ref(false)
const approverRoleVisible = ref(false)
const checkedRoleList = ref([])// 角色列表
const checkedList = ref([]) // 选择的成员
const { setApproverConfig, setApprover } = workFlowStore

const closeDrawer = () => {
  setApprover(false)
}

const disabledVal = window.sessionStorage.getItem('maxLevel') // 上级主管层级
const decisionItem = ref() // 可选决策项
const noticeStarter = ref() // 审批结果通知发起人

const visible = computed({
  get() {
    return workFlowStore.approverDrawer
  },
  set() {
    closeDrawer()
  },
})
watch(() => workFlowStore.approverConfigValue, (val: any) => {
  approverConfig.value = val.value
})

// 切换负责人
const changeType = () => {
  approverConfig.value.nodeUserList = [] // 人员列表
  approverConfig.value.tempExamineMode = '' // 审批方式（会签、或签、逐级审批）
  approverConfig.value.examineMode = '' // 传参审批方式（会签、或签、逐级审批）
  approverConfig.value.noHanderAction = '' // 审批人为空时
  approverConfig.value.directorLevel = '' // 主管
  approverConfig.value.examineEndDirectorLevel = '' // 连续多级主管
}
// 添加成员
const addApprover = () => {
  approverVisible.value = true
  checkedList.value = approverConfig.value.nodeUserList
}
// 添加角色
const addRoleApprover = () => {
  approverRoleVisible.value = true
  checkedRoleList.value = approverConfig.value.nodeUserList
}
// 选择好成员
const sureApprover = (data: IEmployees) => {
  approverConfig.value.nodeUserList = data
  approverVisible.value = false
}
// 选择好角色
const sureRoleApprover = (data: IroleInfo) => {
  approverConfig.value.nodeUserList = data
  approverRoleVisible.value = false
}
// 点击确定，保存审批设置
const saveApprover = () => {
  window.sessionStorage.setItem('decisionItem', `${decisionItem.value}`)
  window.sessionStorage.setItem('noticeStarter', `${noticeStarter.value}`)
  approverConfig.value.examineMode = approverConfig.value.tempExamineMode
  // 成员
  if (approverConfig.value.settype === 1) {
    if (!approverConfig.value.nodeUserList.length) {
      ElMessage({
        message: '请选择审批人',
        type: 'warning',
      })
      return
    }
    if (approverConfig.value.nodeUserList.length > 1 && !approverConfig.value.tempExamineMode) {
      ElMessage({
        message: '请选择审批方式',
        type: 'warning',
      })
      return
    }
  }
  // 角色
  else if (approverConfig.value.settype === 11) {
    if (!approverConfig.value.nodeUserList.length) {
      ElMessage({
        message: '请选择审批人',
        type: 'warning',
      })
      return
    }
    if (!approverConfig.value.tempExamineMode) {
      ElMessage({
        message: '请选择审批方式',
        type: 'warning',
      })
      return
    }
  }
  // 主管
  else if (approverConfig.value.settype === 2) {
    if (!approverConfig.value.tempExamineMode) {
      ElMessage({
        message: '请选择审批方式',
        type: 'warning',
      })
      return
    }
  }
  // 连续多级主管
  else if (approverConfig.value.settype === 7) {
    if (!approverConfig.value.examineEndDirectorLevel) {
      ElMessage({
        message: '请选择审批终点',
        type: 'warning',
      })
      return
    }
  }
  if (!approverConfig.value.noHanderAction) {
    ElMessage({
      message: '审批人为空的选项不能为空',
      type: 'warning',
    })
    return
  }

  // console.log('pppppppp', decisionItem.value)

  // if (!`${decisionItem.value}` || isNaN(decisionItem.value)) {
  //   ElMessage({
  //     message: '请选择决策项',
  //     type: 'warning',
  //   })
  //   return
  // }

  // if (!`${noticeStarter.value}` || isNaN(noticeStarter.value)) {
  //   ElMessage({
  //     message: '请选择审批结果是否通知发起人',
  //     type: 'warning',
  //   })
  //   return
  // }

  // 把审批设置反应在流程图里面
  approverConfig.value.error = !$func.setApproverStr(approverConfig.value)
  // 保存设置
  setApproverConfig({
    value: approverConfig.value,
    flag: true,
    id: workFlowStore.approverConfigValue.id,
  })
  emits('update:nodeConfig', approverConfig.value)
  closeDrawer()
}

onMounted(() => {
  const getDecisionItem = window.sessionStorage.getItem('decisionItem')
  const getNoticeStarter = window.sessionStorage.getItem('noticeStarter')
  if (getDecisionItem) {
    decisionItem.value = parseInt(getDecisionItem) // 可选决策项
  }
  if (getNoticeStarter) {
    noticeStarter.value = parseInt(getNoticeStarter) // 审批结果通知发起人
  }
})
</script>

<template>
  <el-drawer v-model="visible" :append-to-body="true" title="审批设置" direction="rtl" custom-class="set_promoter" size="550px" @close="closeDrawer">
    <div class="demo-drawer__content">
      <h5 class="add-person">
        负责人
      </h5>
      <el-radio-group v-model="approverConfig.settype" style="width: 100%;margin-bottom: 16px;" @change="changeType">
        <el-radio :label="1">
          指定成员
        </el-radio>
        <el-radio :label="11">
          指定角色
        </el-radio>
        <el-radio :label="2" :disabled="disabledVal === '0'">
          主管
        </el-radio>
        <el-radio :label="7" :disabled="disabledVal === '0'">
          连续多级主管
        </el-radio>
      </el-radio-group>
      <!-- 选择好的成员或者角色 -->
      <p v-if="approverConfig && approverConfig.nodeUserList && approverConfig.nodeUserList.length" class="selected_list">
        <span v-for="(item, index) in approverConfig.nodeUserList" :key="index" class="people-box">
          {{ item.name }}
          <el-icon style="vertical-align: middle;cursor: pointer;" @click="$func.removeEle(approverConfig.nodeUserList, item, 'targetId')">
            <close />
          </el-icon>
        </span>
        <a v-if="approverConfig.nodeUserList && approverConfig.nodeUserList.length !== 0" @click="approverConfig.nodeUserList = []">清除</a>
      </p>

      <el-button v-if="approverConfig.settype === 1" type="primary" @click="addApprover">
        <!-- 添加/修改成员 -->选择成员
      </el-button>
      <el-button v-if="approverConfig.settype === 11" type="primary" @click="addRoleApprover">
        <!-- 添加/修改角色 -->选择角色
      </el-button>

      <!-- 分界线 -->
      <div style="width: 100%;height: 1px;margin: 20px 0;background-color: #f2f2f2;" />

      <!-- 主管 -->
      <div v-if="approverConfig.settype === 2" class="add-person">
        <p style="margin-bottom: 32px;">
          <span>发起人的：</span>
          <el-select v-model="approverConfig.directorLevel">
            <el-option
              v-for="item in directorMaxLevel"
              :key="item"
              :value="item"
              :label="item === 1 ? '直接主管' : `第${item}级主管`"
            />
          </el-select>
        </p>
      </div>

      <!-- 审批方式 -->
      <h5 v-if="approverConfig.settype === 2 || approverConfig.settype === 11 || (approverConfig.settype === 1 && approverConfig && approverConfig.nodeUserList && approverConfig.nodeUserList.length > 1)" class="add-person">
        多人审批时采用的审批方式
      </h5>
      <el-radio-group v-if="approverConfig.settype === 2 || approverConfig.settype === 11 || (approverConfig.settype === 1 && approverConfig && approverConfig.nodeUserList && approverConfig.nodeUserList.length > 1)" v-model="approverConfig.tempExamineMode" style="width: 100%;margin-bottom: 16px;">
        <el-radio :label="2">
          会签（需所有审批人同意）
        </el-radio>
        <el-radio :label="1">
          依次审批
        </el-radio>
        <el-radio :label="3">
          或签（其中一个人通过即可）
        </el-radio>
      </el-radio-group>

      <h5 v-if="approverConfig.settype === 7" class="add-person">
        审批终点
      </h5>
      <div v-if="approverConfig.settype === 7" class="add-person" style="margin-bottom: 32px;">
        <el-select v-model="approverConfig.examineEndDirectorLevel">
          <el-option
            v-for="item in directorMaxLevel"
            :key="item"
            :value="item"
            :label="item === 1 ? '最高层级主管' : `第${item}层级主管`"
          />
        </el-select>
      </div>

      <!-- 审批为空时 -->
      <h5 class="add-person">
        审批人为空时
      </h5>
      <el-radio-group v-model="approverConfig.noHanderAction" style="width: 100%;margin-bottom: 16px;">
        <el-radio :label="1">
          自动审批通过
        </el-radio>
        <el-radio :label="2">
          自动审批拒绝
        </el-radio>
        <el-radio :label="3">
          转交给审核管理员
        </el-radio>
      </el-radio-group>

      <!-- 可选决策项 -->
      <!-- <h5 class="add-person">
        可选决策项
      </h5>
      <el-radio-group v-model="decisionItem" style="width: 100%;margin-bottom: 16px;">
        <el-radio :label="1">
          同意、驳回、拒绝
        </el-radio>
        <el-radio :label="2">
          同意、驳回
        </el-radio>
        <el-radio :label="3">
          拒绝
        </el-radio>
      </el-radio-group> -->

      <!-- 审批结果通知发起人 -->
      <!-- <h5 class="add-person">
        审批结果通知发起人
      </h5>
      <el-radio-group v-model="noticeStarter" style="width: 100%;margin-bottom: 16px;">
        <el-radio :label="1">
          是
        </el-radio>
        <el-radio :label="0">
          否
        </el-radio>
      </el-radio-group> -->

      <!-- 底部确定取消按钮 -->
      <div class="demo-drawer__footer clear" style="margin-top: 50px;">
        <el-button type="primary" @click="saveApprover">
          确 定
        </el-button>
        <el-button @click="closeDrawer">
          取 消
        </el-button>
      </div>

      <!-- 角色弹框 -->
      <select-role-dialog
        v-model:visible="approverRoleVisible"
        v-model:data="checkedRoleList"
        @change="sureRoleApprover"
      />
      <!-- 成员弹框 -->
      <select-employees-dialog
        v-model:visible="approverVisible"
        v-model:data="checkedList"
        @change="sureApprover"
      />
    </div>
  </el-drawer>
</template>

<style lang="scss" scoped>
.people-box {
  font-size: 12px;
  margin-right: 10px;
  padding: 3px 6px 3px 9px;
  white-space: nowrap;
  border-radius: 6px;
  border: 1px solid rgb(220 220 220 / 100%);
}
</style>

<style lang="scss">
.set_promoter {
  .add-person {
    margin: 16px 0;
    font-weight: 600;
    font-size: 13px;
    color: #000;
  }

  .show-people {
    margin-bottom: 20px;

    .selected-people {
      font-size: 12px;
      margin-right: 10px;
      padding: 3px 6px 3px 9px;
      white-space: nowrap;
      border-radius: 6px;
      border: 1px solid rgb(220 220 220 / 100%);
    }
  }

  .more-than-one-person {
    display: flex;
    align-items: center;
    margin-top: 20px;

    .label {
      font-weight: 500;
      font-size: 13px;
      margin-right: 20px;
    }
  }

  .approver_content {
    padding-bottom: 10px;
    border-bottom: 1px solid #f2f2f2;
  }

  .approver_self_select,
  .approver_content {
    .el-button {
      margin-bottom: 20px;
    }
  }

  .approver_content,
  .approver_some,
  .approver_self_select {
    .el-radio-group {
      display: unset;
    }

    .el-radio {
      width: 27%;
      margin-bottom: 20px;
      height: 16px;
    }
  }

  .approver_manager p {
    line-height: 32px;
  }

  .approver_manager select {
    width: 420px;
    height: 32px;
    background: rgb(255 255 255 / 100%);
    border-radius: 4px;
    border: 1px solid rgb(217 217 217 / 100%);
  }

  .approver_manager p.tip {
    margin: 10px 0 22px;
    font-size: 12px;
    line-height: 16px;
    color: #f8642d;
  }

  .approver_self {
    padding: 28px 20px;
  }

  .approver_self_select,
  .approver_manager,
  .approver_content,
  .approver_some {
    padding: 20px 20px 0;
  }

  .approver_manager p:first-of-type,
  .approver_some p {
    line-height: 19px;
    font-size: 14px;
    margin-bottom: 14px;
  }

  .approver_self_select h3 {
    margin: 5px 0 20px;
    font-size: 14px;
    font-weight: bold;
    line-height: 19px;
  }
}
</style>
