<script setup lang="ts" name="Approve">
import { ref, provide } from 'vue'
import Tabbar from '@/components/Tabbar.vue'
// import { useRouter, useRoute } from 'vue-router'
import { useAppStore } from '@/store/modules/app'
import { useDict } from '../../common/utils/dict'
import ApproveListTodo from './modules/ApproveListTodo.vue'
import ApproveListDone from './modules/ApproveListDone.vue'
import ApproveListSend from './modules/ApproveListSend.vue'
import ApproveListChaoS from './modules/ApproveListChaoS.vue'

// const router = useRouter()
const appStore = useAppStore()
const { process_group } = useDict('process_group')

const active = ref('0')
const states = [{
  'value': '1',
  'label': '审核中'
}, {
  'value': '2',
  'label': '审批通过'
}, {
  'value': '3',
  'label': '审核终止'
}, {
  'value': '4',
  'label': '审核拒绝'
}]

const badges = ref(['', '', '', ''])

// 考勤流程编码
const processCode = {
  'qingjia': 'leave', // 考勤-请假
  'xiaojia': 'leaveClose', // 考勤-销假
  'chuchaishenqing': 'trip', // 考勤-出差
  'chuchaiguanbi': 'tripClose', // 考勤-出差关闭
  'gerentiaoxiu': 'overTimeForLeave', // 考勤-个人调休调休
  'jiabanshenqing': 'overtimeWork', // 考勤-加班申请
  'yichangjiaban': 'abnormalOvertime', // 考勤-异常加班申请
  'jititiaoxiu': 'dayOff', // 考勤-集体调休
  'qianka': 'cardSign', // 考勤-签卡
  'waichushenqing': 'outside', // 考勤-当日外出申请
  'zhaopinxuqiu': 'recruitmentNeeds', // 人事-招聘需求申请
  'niruzhi': 'employmentApplication', // 人事-拟入职申请
  'ruzhishenqing': '', // 人事-入职申请
  'renshiyidong': 'employeesChanges', // 人事-人事异动
  'special-flow': '', // 高层通用流程
  'special-card-signing-flow': '', // 高层签卡流程
  'zhoujuan-flow': '', // 销售通用流程
  'xinzijiesuan': '', // 薪酬-薪资结算审批
  'yuangongjiangcheng': 'employeesDisciplinary', // 人事-员工奖惩
  'lizhishenqing': 'resignation', // 人事-离职申请
  'lizhigongzuojiaojie': '', // 人事-离职工作交接
  'useCarApply': 'useCarApply' // 用车申请
  /** 老数据兼容 */
  // 'ATT_LEAVE_REQUEST_': 'leave', // 考勤-请假
  // 'ATT_LEAVE_REQUEST_MASTER_DG': 'leave',
  // 'ATT_LEAVE_REQUEST_MASTER_KS': 'leave',
  // 'ATT_LEAVE_REQUEST_STAFF_DOWM_SEVEN_DAYS_DG': 'leave',
  // 'ATT_LEAVE_REQUEST_STAFF_DOWM_SEVEN_DAYS_KS': 'leave',
  // 'ATT_LEAVE_REQUEST_STAFF_UP_SEVEN_DAYS_DG': 'leave',
  // 'ATT_LEAVE_REQUEST_STAFF_UP_SEVEN_DAYS_KS': 'leave',
  // 'ATT_LEAVE_REQUEST_STAFF_LESS_ONE_DAY_MARKETING': 'leave',
  // 'ATT_LEAVE_REQUEST_STAFF_LESS_THREE_DAY_MARKETING': 'leave',
  // 'ATT_LEAVE_REQUEST_STAFF_LESS_FIVE_DAY_MARKETING': 'leave',
  // 'ATT_LEAVE_REQUEST_STAFF_MORE_FIVE_DAY_MARKETING': 'leave',
  // 'ATT_LEAVE_REQUEST_MASTER_MARKETING': 'leave',
  // 'ATT_LEAVE_REQUEST_BACK': 'leaveClose', // 考勤-销假
  // 'ATT_BUSINESS_TRIP': 'trip', // 考勤-出差
  // 'ATT_BUSINESS_TRIP_STAFF_FACTORY': 'trip',
  // 'ATT_BUSINESS_TRIP_MASTER_FACTORY_KS': 'trip',
  // 'ATT_BUSINESS_TRIP_MASTER_FACTORY_DG': 'trip',
  // 'ATT_BUSINESS_TRIP_BACK_FACTORY': 'tripClose',
  // 'ATT_BUSINESS_TRIP_STAFF_MARKETING': 'trip',
  // 'ATT_BUSINESS_TRIP_MASTER_MARKETING': 'trip',
  // 'ATT_BUSINESS_TRIP_BACK': 'tripClose', // 考勤-出差关闭
  // 'ATT_BUSINESS_TRIP_BACK_MARKETING': 'tripClose',
  // 'ATT_OVERTIME_WORKING_LEAVE': 'overTimeForLeave', // 考勤-加班调休
  // 'ATT_OVERTIME_WORKING_LEAVE_DOWN_GROUP_LEADER_KS': 'overTimeForLeave',
  // 'ATT_OVERTIME_WORKING_LEAVE_DOWN_GROUP_LEADER_DG': 'overTimeForLeave',
  // 'ATT_OVERTIME_WORKING_LEAVE_UP_GROUP_LEADER_KS': 'overTimeForLeave',
  // 'ATT_OVERTIME_WORKING_LEAVE_UP_GROUP_LEADER_DG': 'overTimeForLeave',
  // 'ATT_OVERTIME_WORKING': '', // 考勤-加班、异常加班
  // 'ATT_DAY_OFF_MARKETING': '', // 考勤-调休
  // 'ATT_REGISTER_CARD': 'cardSign', // 考勤-签卡
  // 'ATT_REGISTER_CARD_FACTORY': 'cardSign',
  // 'ATT_REGISTER_CARD_MARKETING': 'cardSign',
  // 'ATT_OUTSIDE_APPLY': 'outside' // 考勤-当日外出申请
}

provide('process_group', process_group)
provide('states', states)
provide('onDetail', (approveCardData: any, type: string) => {
  // console.log(approveCardData, type)
  const matchedKey = Object.keys(processCode).find(key => 
    approveCardData.processCode.includes(key)
  )
  if(matchedKey){
    console.log('matchedKey',matchedKey)
    console.log('approveCardData',approveCardData)
    const page = {
      'trip': '/attendance/trip/detail',
      'leave': '/attendance/leave/detail',
      'tripClose': '/attendance/trip/close-detail',
      'leaveClose': '/attendance/leave/close-detail',
      'overTimeForLeave': '/attendance/overTimeForLeave/detail',
      'cardSign': '/attendance/cardSign/detail',
      'outside': '/attendance/outside/detail',
      'dayOff': '/attendance/dayOff/detail',
      'overtimeWork': '/attendance/overtimeWork/detail',
      'abnormalOvertime': '/attendance/abnormalOvertime/detail',
      'resignation': '/personnel/resignation/detail',
      'employeesChanges': '/personnel/employeesChanges/detail',
      'employmentApplication': '/personnel/employmentApplication/detail',
      'recruitmentNeeds': '/personnel/recruitmentNeeds/detail',
      'employeesDisciplinary': '/personnel/employeesDisciplinary/detail',
      'useCarApply': '/carUse/apply/detail'
    }




    // 待办
    if (type === '1') {
      console.log('待办')
      if(matchedKey === 'waichushenqing' && approveCardData.businessTable.includes("t_att_out_request")){
        uni.navigateTo({
          url:'/approve/detail?type=' + 'outRequest' + '&id=' + approveCardData.businessId + '&instanceId=' + approveCardData.instanceId + '&taskId=' + approveCardData.taskId + '&userId=' + approveCardData.userId + '&userName=' + approveCardData.userName + '&beginTime=' + approveCardData.beginTime + '&endTime=' + approveCardData.endTime
        })
      }else{
        uni.navigateTo({
          url:'/approve/detail?type=' + processCode[matchedKey] + '&id=' + approveCardData.businessId + '&instanceId=' + approveCardData.instanceId + '&taskId=' + approveCardData.taskId + '&userId=' + approveCardData.userId + '&userName=' + approveCardData.userName + '&beginTime=' + approveCardData.beginTime + '&endTime=' + approveCardData.endTime
        })
      }

      
    } else {

      if(matchedKey === 'waichushenqing' && approveCardData.businessTable.includes("t_att_out_request")){
        let instanceId = approveCardData.instanceId
        // 已发
        if (type === '3') {
          instanceId = approveCardData.id
        }
        // 抄送
        if (type === '4') {
          instanceId = approveCardData.ruId
        }
        uni.navigateTo({
          url: '/attendance/outRequest/detail'+ '?id=' + approveCardData.businessId + '&instanceId=' + instanceId + '&beginTime=' + approveCardData.beginTime + '&endTime=' + approveCardData.endTime
        })

      }else{
        let instanceId = approveCardData.instanceId
        // 已发
        if (type === '3') {
          instanceId = approveCardData.id
        }
        // 抄送
        if (type === '4') {
          instanceId = approveCardData.ruId
        }
        uni.navigateTo({
          url: page[processCode[matchedKey]] + '?id=' + approveCardData.businessId + '&instanceId=' + instanceId + '&beginTime=' + approveCardData.beginTime + '&endTime=' + approveCardData.endTime
        })
      }

        
    }
  }else{
        uni.showToast({
          title:'请前往电脑端处理操作'
        })
        return
  }

  
})

function changeTotal(total, index) {
  badges.value[index] = total > 99 ? '99+' : total
  if (index === 0) {
    appStore.updateTodoCount(total)
  }
}

// 切换Tab
function handleChangeTab(val: any): void {
  console.log(val)
}
</script>

<template>
  <div class="approve-container">
    <van-tabs
      v-model:active="active"
      class="tabs"
      @change="handleChangeTab">
      <van-tab
        title="待办"
        name="0"
        :show-zero-badge="false"
        :badge="badges[0]">
        <ApproveListTodo @change-total="changeTotal($event, 0)" />
      </van-tab>
      <van-tab title="已办" name="1">
        <ApproveListDone @change-total="changeTotal($event, 1)" />
      </van-tab>
      <van-tab title="已发" name="2">
        <ApproveListSend @change-total="changeTotal($event, 2)" />
      </van-tab>
      <van-tab title="抄送" name="3">
        <ApproveListChaoS @change-total="changeTotal($event, 3)" />
      </van-tab>
    </van-tabs>
  </div>
  <Tabbar :selected="1"/>
</template>

<style lang="less" scoped>
.approve-container {
  padding-bottom: calc(12rpx + env(safe-area-inset-bottom) + 20rpx + env(safe-area-inset-bottom));
}
.tabs {
  :deep(.van-tabs__wrap) {
    position: sticky;
    z-index: 1;
    top: 0;
  }
}
:deep(.query-wrap) {
  position: sticky;
  z-index: 1;
  top: 0rpx;
}
</style>
