<!-- 测试、校准或检定合格分包方名录编辑 -->
<script lang="ts" setup name="SubpackageDirectoriesEdit">
import { ElMessage, ElMessageBox } from 'element-plus'
import subpackageDirectoriesBasic from './components/basic.vue'
import subpackageDirectoriesPerson from './components/person.vue'
import subpackageDirectoriesAbility from './components/ability.vue'
import ApprovalDialog from '@/components/Approval/ApprovalDialog.vue'
import { approvalDelete, cancelApproval, refuseApproval } from '@/api/business/subpackage/directories'
const loading = ref(false) // 表单加载状态
const infoId = ref('') // id
const pageType = ref('add') // 页面类型: add, edit, detail
const approvalStatusName = ref('') // 审批状态名字
const showSubmitButton = ref(false) // 提交按钮是否禁用
const $route = useRoute()
const subpackageDirectoriesBasicRef = ref() // 基本信息组件ref
const subpackageDirectoriesPersonRef = ref() // 分包方人员组件ref
const subpackageDirectoriesAbilityRef = ref() // 业务能力组件ref
const taskId = ref('') // 任务id，用于同意、驳回、拒绝审批
const processId = ref('') // 流程实例id
const approvalDialog = ref() // 审批组件ref
const showApprovalButton = ref(true) // 是否展示审批按钮
const showRefuseEditButton = ref(true) // 是否展示未通过编辑按钮
const textMap: { [key: string]: string } = {
  edit: '编辑',
  add: '新建',
  detail: '详情',
}// 字典

// 初始化router
const $router = useRouter()

// --------------------------------------路由参数----------------------------------------------
// 从路由中获取页面类型参数
if ($route.params && $route.params.type) {
  pageType.value = $route.params.type as string
  if ($route.params.id) {
    infoId.value = $route.params.id as string
  }
}
// -------------------------------------标签--------------------------------------------------
const radioMenus = ref([ // 标签内容
  { name: '基本信息', value: 'subpackage-directories-basic' },
  { name: '分包方人员', value: 'subpackage-directories-person' },
  { name: '业务能力', value: 'subpackage-directories-ability' },
  { name: '审批详情', value: 'subpackage-directories-approval-record' },
])
const current = ref('subpackage-directories-basic') // 选择的tab 默认基本信息
// -----------------------------------------按钮--------------------------------------------------
// 关闭新增页面的回调
const close = () => {
  $router.back()
}
// 点击提交
const handleSubmit = () => {
  subpackageDirectoriesBasicRef.value.submitForm(processId.value, infoId.value)
}
// 点击保存
const saveForm = () => {
  if (current.value === 'subpackage-directories-basic') { // 基本信息
    subpackageDirectoriesBasicRef.value.saveForm()
  }
  else if (current.value === 'subpackage-directories-person') { // 分包方人员
    subpackageDirectoriesPersonRef.value.savePerson()
  }
  else {
    subpackageDirectoriesAbilityRef.value.saveAbility()
  }
}
// 新建保存成功
const addSuccess = (id: string) => {
  infoId.value = id
  showSubmitButton.value = true // 保存成功显示提交按钮
}
// 提交成功
const submitSuccess = () => {
  pageType.value = 'detail' // 详情模式-不可编辑
  showRefuseEditButton.value = false // 隐藏提交按钮
}

// 点击删除--已取消
const del = () => {
  approvalDelete({ id: infoId.value, taskId: taskId.value }).then(() => {
    ElMessage.success('已删除')
    close()
  })
}

// 点击编辑
const handleEdit = () => {
  pageType.value = 'edit' // 编辑模式
}
// -------------------------------------------审批----------------------------------------

// 审批结束回调
const approvalSuccess = () => {
  showApprovalButton.value = false
}

// 审批
const handleApprove = (val: string) => {
  if (val === '取消') {
    const params = {
      processInstanceId: processId.value!,
      comments: '',
      id: infoId.value,
    }
    ElMessageBox.confirm(
      '确认取消该审批吗？',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      },
    )
      .then(() => {
        cancelApproval(params).then((res) => {
          ElMessage({
            type: 'success',
            message: '已取消',
          })
          showApprovalButton.value = false
        })
      })
  }
  else if (val === '同意') {
    approvalDialog.value.initDialog('agree', taskId.value)
  }
  else if (val === '拒绝') {
    approvalDialog.value.initDialog('refuse', taskId.value, infoId.value)
  }
}

// 拒绝
const refuse = (comments: string, taskId: string, id: string) => {
  const params = {
    id,
    taskId, // 任务id
    comments, // 拒绝原因
  }
  refuseApproval(params).then((res) => {
    ElMessage({
      type: 'success',
      message: '已拒绝',
    })
    showApprovalButton.value = false
  })
}

// -----------------------------------------钩子--------------------------------------------------
watch(() => approvalStatusName.value, (val) => {
  if (val === '全部' || val === '草稿箱' || pageType.value === 'add') { // 全部草稿箱把审批详情删了
    if (radioMenus.value[radioMenus.value.length - 1].value === 'subpackage-directories-approval-record') {
      radioMenus.value.pop()
    }
    console.log(radioMenus.value)
  }
  else { // 非全部和草稿箱把审批详情加上
    if (radioMenus.value[radioMenus.value.length - 1].value !== 'subpackage-directories-approval-record') {
      radioMenus.value.push({ name: '审批详情', value: 'subpackage-directories-approval-record' })
    }
  }
})
onMounted(() => {
  approvalStatusName.value = $route.query.approvalStatusName as string // 审批状态名字
  processId.value = $route.query.processId as string // 流程实例id
  taskId.value = $route.query.taskId as string // 任务id用于审批
})
</script>

<template>
  <app-container>
    <detail-page v-loading="loading" :title="`测试、校准或检定工作分包评审表 (${textMap[pageType]})`">
      <template #btns>
        <el-button v-if="pageType === 'detail' && approvalStatusName === '待审批' && showApprovalButton" type="primary" @click="handleApprove('同意')">
          同意
        </el-button>
        <el-button v-if="pageType === 'detail' && approvalStatusName === '待审批' && showApprovalButton" type="danger" @click="handleApprove('拒绝')">
          拒绝
        </el-button>
        <el-button v-if="pageType === 'detail' && approvalStatusName === '审批中' && showApprovalButton" type="info" @click="handleApprove('取消')">
          取消
        </el-button>
        <el-button
          v-if="pageType === 'add' || (pageType === 'edit' && approvalStatusName !== '未通过' && approvalStatusName !== '已取消' && approvalStatusName !== '全部')"
          type="primary"
          :disabled="!infoId"
          @click="handleSubmit"
        >
          提交
        </el-button>
        <el-button v-if="approvalStatusName !== '已审批' && pageType === 'detail' && approvalStatusName === '未通过' && showRefuseEditButton" type="primary" @click="handleEdit">
          编辑
        </el-button>
        <el-button v-if="pageType !== 'detail'" type="primary" @click="saveForm">
          保存
        </el-button>
        <el-button v-if="approvalStatusName === '已取消'" type="danger" @click="del">
          删除
        </el-button>
        <el-button type="info" @click="close">
          关闭
        </el-button>
      </template>
      <el-radio-group v-model="current">
        <el-radio-button
          v-for="item in radioMenus"
          :key="item.value"
          :label="item.value"
          :disabled="!infoId"
        >
          {{ item.name }}
        </el-radio-button>
      </el-radio-group>
    </detail-page>
    <!-- 基本信息 -->
    <subpackage-directories-basic
      v-if="current === 'subpackage-directories-basic'"
      :id="infoId"
      ref="subpackageDirectoriesBasicRef"
      :page-type="pageType"
      :approval-status-name="approvalStatusName"
      @add-success="addSuccess"
      @submit-success="submitSuccess"
    />
    <!-- 分包方人员 -->
    <subpackage-directories-person
      v-show="current === 'subpackage-directories-person'"
      :id="infoId"
      ref="subpackageDirectoriesPersonRef"
      :page-type="pageType"
    />
    <!-- 业务能力 -->
    <subpackage-directories-ability
      v-show="current === 'subpackage-directories-ability'"
      :id="infoId"
      ref="subpackageDirectoriesAbilityRef"
      :page-type="pageType"
    />
    <!-- 审批详情 -->
    <approval-record-table v-if="current === 'subpackage-directories-approval-record'" :process-id="processId" />
    <!-- 审批弹窗 -->
    <approval-dialog ref="approvalDialog" @on-success="approvalSuccess" @refuse="refuse" />
  </app-container>
</template>

<style lang="scss" scoped>
// 样式
</style>

