<!-- 审批设置 -->
<script setup>
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.ts'

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({})
const approverVisible = ref(false)
const approverRoleVisible = ref(false)
const checkedRoleList = ref([])// 角色列表
const checkedList = ref([]) // 选择的成员
// const { approverConfig1, approverDrawer } = workFlowStore
const { setApproverConfig, setApprover } = workFlowStore

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

const visible = computed({
  get() {
    console.log('1')
    return workFlowStore.approverDrawer
  },
  set() {
    closeDrawer()
  },
})
watch(() => workFlowStore.approverConfig1, (val) => {
  approverConfig.value = val.value
})
const changeRange = () => {
  approverConfig.value.nodeUserList = []
}
const changeType = (val) => {
  approverConfig.value.nodeUserList = []
  approverConfig.value.examineMode = ''
  approverConfig.value.noHanderAction = ''
  if (val === 2) { // 主管
    approverConfig.value.directorLevel = 1
  }
  else if (val === 4) {
    approverConfig.value.selectMode = 1
    approverConfig.value.selectRange = 1
  }
  else if (val === 7) { // 连续多级主管
    approverConfig.value.examineEndDirectorLevel = 1
  }
}
// 添加成员
const addApprover = () => {
  approverVisible.value = true
  checkedList.value = approverConfig.value.nodeUserList
}
// 添加角色
const addRoleApprover = () => {
  approverRoleVisible.value = true
  console.log('approverRoleVisible.value', approverRoleVisible.value)
  checkedRoleList.value = approverConfig.value.nodeUserList
}
// 选择好成员
const sureApprover = (data) => {
  approverConfig.value.nodeUserList = data
  approverVisible.value = false
}
// 选择好角色
const sureRoleApprover = (data) => {
  approverConfig.value.nodeUserList = data
  approverRoleVisible.value = false
}

// 点击确定，保存审批设置
const saveApprover = () => {
  approverConfig.value.examineMode = approverConfig.value.tempExamineMode
  console.log(approverConfig.value.settype)
  // 成员
  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) {
    console.log(approverConfig.value.directorLevel)
    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
  }

  // if (approverConfig.value.settype === 11) {
  //   approverConfig.value.settype = 1
  // }
  // 把审批设置反应在流程图里面
  approverConfig.value.error = !$func.setApproverStr(approverConfig.value)
  // 保存设置
  setApproverConfig({
    value: approverConfig.value,
    flag: true,
    id: workFlowStore.approverConfig1.id,
    // id: workFlowStore.approverConfig1.value.id,
  })
  emits('update:nodeConfig', approverConfig.value)
  closeDrawer()
}
</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">
      <!-- <div class="drawer_content">
        <div class="approver_content">
          <el-radio-group v-model="approverConfig.settype" class="clear" @change="changeType">
            <el-radio :label="1">
              指定成员
            </el-radio>
            <el-radio :label="2">
              主管
            </el-radio>
            <el-radio :label="4">
              发起人自选
            </el-radio>
            <el-radio :label="5">
              发起人自己
            </el-radio>
            <el-radio :label="7">
              连续多级主管
            </el-radio>
          </el-radio-group>
          <el-button v-if="approverConfig.settype === 1" type="primary" @click="addApprover">
            添加/修改成员
          </el-button>
          <p v-if="approverConfig.settype === 1" class="selected_list">
            <span v-for="(item, index) in approverConfig.nodeUserList" :key="index">{{ item.name }}
              <img src="@/assets/images/add-close1.png" @click="$func.removeEle(approverConfig.nodeUserList, item, 'targetId')">
            </span>
            <a v-if="approverConfig.nodeUserList.length !== 0" @click="approverConfig.nodeUserList = []">清除</a>
          </p>
        </div>
        <div v-if="approverConfig.settype === 2" class="approver_manager">
          <p>
            <span>发起人的：</span>
            <select v-model="approverConfig.directorLevel">
              <option v-for="item in directorMaxLevel" :key="item" :value="item">
                {{ item === 1 ? '直接' : `第${item}级` }}主管
              </option>
            </select>
          </p>
          <p class="tip">
            找不到主管时，由上级主管代审批
          </p>
        </div>
        <div v-if="approverConfig.settype === 5" class="approver_self">
          <p>该审批节点设置“发起人自己”后，审批人默认为发起人</p>
        </div>
        <div v-show="approverConfig.settype === 4" class="approver_self_select">
          <el-radio-group v-model="approverConfig.selectMode" style="width: 100%;">
            <el-radio :label="1">
              选一个人
            </el-radio>
            <el-radio :label="2">
              选多个人
            </el-radio>
          </el-radio-group>
          <h3>选择范围</h3>
          <el-radio-group v-model="approverConfig.selectRange" style="width: 100%;" @change="changeRange">
            <el-radio :label="1">
              全公司
            </el-radio>
            <el-radio :label="2">
              指定成员
            </el-radio>
            <el-radio :label="3">
              指定角色
            </el-radio>
          </el-radio-group>
          <el-button v-if="approverConfig.selectRange === 2" type="primary" @click="addApprover">
            添加/修改成员
          </el-button>
          <el-button v-if="approverConfig.selectRange === 3" type="primary" @click="addRoleApprover">
            添加/修改角色
          </el-button>
          <p v-if="approverConfig.selectRange === 2 || approverConfig.selectRange === 3" class="selected_list">
            <span v-for="(item, index) in approverConfig.nodeUserList" :key="index">{{ item.name }}
              <img src="@/assets/images/add-close1.png" @click="$func.removeEle(approverConfig.nodeUserList, item, 'targetId')">
            </span>
            <a v-if="approverConfig.nodeUserList.length !== 0 && approverConfig.selectRange !== 1" @click="approverConfig.nodeUserList = []">清除</a>
          </p>
        </div>
        <div v-if="approverConfig.settype === 7" class="approver_manager">
          <p>审批终点</p>
          <p style="padding-bottom: 20px;">
            <span>发起人的：</span>
            <select v-model="approverConfig.examineEndDirectorLevel">
              <option v-for="item in directorMaxLevel" :key="item" :value="item">
                {{ item === 1 ? '最高' : `第${item}` }}层级主管
              </option>
            </select>
          </p>
        </div>
        <div v-if="(approverConfig.settype === 1 && approverConfig.nodeUserList.length > 1) || approverConfig.settype === 2 || (approverConfig.settype === 4 && approverConfig.selectMode === 2)" class="approver_some">
          <p>多人审批时采用的审批方式</p>
          <el-radio-group v-model="approverConfig.examineMode" class="clear">
            <el-radio :label="1">
              依次审批
            </el-radio>
            <br>
            <el-radio v-if="approverConfig.settype !== 2" :label="2">
              会签(须所有审批人同意)
            </el-radio>
          </el-radio-group>
        </div>
        <div v-if="approverConfig.settype === 2 || approverConfig.settype === 7" class="approver_some">
          <p>审批人为空时</p>
          <el-radio-group v-model="approverConfig.noHanderAction" class="clear">
            <el-radio :label="1">
              自动审批通过/不允许发起
            </el-radio>
            <br>
            <el-radio :label="2">
              转交给审核管理员
            </el-radio>
          </el-radio-group>
        </div>
      </div> -->

      <!-- ------------------------------------------------------------ -->
      <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">
          主管
        </el-radio>
        <el-radio :label="7">
          连续多级主管
        </el-radio>
      </el-radio-group>
      <!-- 选择好的成员或者角色 -->
      <!-- <div class="show-people">
        <span v-for="item in approverConfig.nodeUserList" :key="item.id" class="selected-people">
          {{ item.name }}
          <el-icon style="vertical-align: middle;" @click="$func.removeEle(approverConfig.nodeUserList, item, 'targetId')">
            <close />
          </el-icon>
        </span>
      </div> -->
      <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 }}
          <!-- <img src="../../assets/images/add-close1.png" @click="$func.removeEle(approverConfig.nodeUserList, item, 'targetId')"> -->
          <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>

      <!-- 底部确定取消按钮 -->
      <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>
