<!-- 培训计划审批列表页 -->
<script lang="ts" setup name="approve">
import ListApproval from './components/listApproval.vue' // 全部
import type { IButtonApprove, dictType, menuType } from './plan_interface'
import useDictStore from '@/store/modules/dict'
import { getDictByCode } from '@/api/system/dict'

const dictStore = useDictStore()
const buttonsSet: { [key: string]: IButtonApprove[] } = {
  全部: [{ name: '查看', type: 'primary' },
    { name: '同意', type: 'primary' },
    { name: '驳回', type: 'primary' },
    { name: '拒绝', type: 'danger' },
    { name: '取消', type: 'info' },
    { name: '删除', type: 'danger' },
  ],
  草稿箱: [
    { name: '查看', type: 'primary' },
    { name: '编辑', type: 'primary' },
    { name: '提交', type: 'primary' },
    { name: '删除', type: 'danger' },
  ],
  待审批: [
    { name: '查看', type: 'primary' },
    { name: '同意', type: 'primary' },
    { name: '驳回', type: 'primary' },
    { name: '拒绝', type: 'danger' },
    { name: '删除', type: 'danger' },
  ],
  审批中: [
    { name: '查看', type: 'primary' },
    { name: '删除', type: 'danger' },
    { name: '取消', type: 'info' },
  ],
  已通过: [
    { name: '查看', type: 'primary' },
  ],
  未通过: [
    { name: '查看', type: 'primary' },
    { name: '编辑', type: 'primary' },
  ],
  已取消: [
    { name: '查看', type: 'primary' },
    { name: '编辑', type: 'primary' },
    { name: '提交', type: 'primary' },
    { name: '删除', type: 'danger' },
  ],

}
const menus = ref<menuType[]>([])
// 当前tab
const currentTab = ref('全部')
// 当前审批状态
const currentApprovalStatus = ref('0')
// 当前需要显示的按钮列表
const currentButtons = ref<IButtonApprove[]>([])
currentButtons.value = buttonsSet['全部']
// 审批类型字典
const approvalStatusList = dictStore.approvalStatusListTow
const $route = useRoute()
if ($route.query && $route.query.type) {
  const currentMenu = menus.value.find(item => item.value == $route.query.type)
  if (currentMenu) {
    currentTab.value = currentMenu?.name
  }
}

// 获取字典值
async function getDict() {
  // // 审批状态
  const res = await getDictByCode('approvalStatus')
  // 制作右上角的菜单
  res.data.forEach((item: dictType) => {
    if (item.name === '全部' || item.name === '草稿箱'
      || item.name === '待审批' || item.name === '审批中'
      || item.name === '已通过' || item.name === '未通过'
      || item.name === '已取消') {
      menus.value.push({
        name: item.name,
        value: `${item.value}`,
      })
    }
  })
}

// 组件销毁时存入自己的审批类型 下次进去还是原来的审批类型
onUnmounted(() => {
  sessionStorage.setItem('trinPlanListMenu', currentTab.value)
})
// 挂载的时候找到之前存过的审批类型
onMounted(async () => {
  await getDict()
  currentTab.value = sessionStorage.getItem('trinPlanListMenu') || '全部'
  sessionStorage.getItem('trinPlanListMenu') ? sessionStorage.removeItem('trinPlanListMenu') : ''
})

watch(currentTab, (newValue) => {
  currentApprovalStatus.value = approvalStatusList[newValue]
  currentButtons.value = buttonsSet[newValue]
}, { deep: true, immediate: true })
</script>

<template>
  <div class="container">
    <div class="btns">
      <!-- 三级菜单 -->
      <el-radio-group v-model="currentTab">
        <el-radio-button v-for="item in menus" :key="item.name" :label="item.name">
          {{ item.name }}
        </el-radio-button>
      </el-radio-group>
    </div>
    <!-- 展示区域 -->
    <list-approval :name="currentTab" :status="currentApprovalStatus" :buttons="currentButtons" />
  </div>
</template>

<style lang="scss" scoped>
.container {
  position: relative;

  .btns {
    position: fixed;
    top: 69px;
    right: 15px;
    z-index: 999;
  }
}
</style>
