<script lang="ts" setup name="approve">
import ListApproval from './components/listApproval.vue' // 全部
import useDictStore from '@/store/modules/dict'
export interface menuType {
  name: string
  value: string
}
export interface IButton {
  name: string
  type: '' | 'default' | 'text' | 'primary' | 'success' | 'warning' | 'info' | 'danger'
  permission?: string
  proxy?: string
}
const dictStore = useDictStore()
const buttonsSet: { [key: string]: IButton[] } = {
  全部: [{ 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: 'primary' },
    { name: '删除', type: 'danger' },
  ],
  未通过: [
    { name: '查看', type: 'primary' },
    { name: '删除', type: 'danger' },
  ],
  已取消: [
    { name: '查看', type: 'primary' },
    { name: '删除', type: 'danger' },
  ],

}
const menus = ref<menuType[]>([
  { name: '全部', value: '0' },
  { name: '草稿箱', value: '1' },
  { name: '待审批', value: '2' },
  { name: '审批中', value: '3' },
  { name: '已通过', value: '4' },
  { name: '未通过', value: '5' },
  { name: '已取消', value: '6' },
])
// 当前tab
const currentTab = ref('全部')
// 当前审批状态
const currentApprovalStatus = ref('0')
// 当前需要显示的按钮列表
const currentButtons = ref<IButton[]>([])
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
  }
}

// 组件销毁时存入自己的审批类型 下次进去还是原来的审批类型
onUnmounted(() => {
  sessionStorage.setItem('trinPlanListMenu', currentTab.value)
})
// 挂载的时候找到之前存过的审批类型
onMounted(() => {
  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: 76px;
    right: 15px;
    z-index: 999;
  }
}
</style>
