<template>
  <div v-if="isMobileDevice() && !isDingTalk()" class="loading">跳转处理中...</div>
  <!--  <a ref="myLink" :href="href" v-show="false">跳转app</a>-->
  <!--  <div v-if="isMobileDevice()&&isDingTalk()" class="wxtip" id="JweixinTip">-->
  <!--    <span class="wxtip-icon"></span>-->
  <!--  </div>-->
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import * as LoginApi from '@/api/login'
import * as authUtil from '@/utils/auth'

const { push } = useRouter()
const route = useRoute()

// 设备检测
const isMobileDevice = (): boolean => {
  const userAgentInfo = navigator.userAgent

  const mobileAgents = [
    'Android',
    'iPhone',
    'SymbianOS',
    'Windows Phone',
    'iPad',
    'iPod',
    'OpenHarmony'
  ]

  const mobileFlag = mobileAgents.some((mobileAgent) => {
    return userAgentInfo.indexOf(mobileAgent) > 0
  })

  return mobileFlag
}

// 判断是否是钉钉环境
const isDingTalk = (): boolean => {
  const userAgent = navigator.userAgent.toLowerCase()
  return userAgent.includes('dingtalk')
}

// 业务路由映射
const businessRoutes: Record<string, string> = {
  generateInspect: 'InspectOrderWrite',
  failureReport: 'BpmProcessInstanceDetail',
  maintainOut: 'BpmProcessInstanceDetail',
  generateOperation: '',
  generateMaintenance: '',
  generateMaintain: '',
  rdDailyReport: 'rdDailyReport',
  rdReportDetail: 'rdReportDetail',
  rdReportApproval: 'rdReportApproval',
  rhDailyReport: 'rhDailyReport',
  rhReportApproval: 'rhReportApproval',
  ryDailyReport: 'ryDailyReport',
  ryReportApproval: 'ryReportApproval',
  ryXjDailyReport: 'ryXjDailyReport',
  ryXjReportApproval: 'ryXjReportApproval',
  'qhse-accident': 'qhse-accident'
}

// const href = ref('')
// const myLink = ref(null)

// const clickA = () => {
//   myLink.value?.click()
// }

onMounted(async () => {
  let {
    type = '',
    id = '',
    userId = '',
    deptId = '',
    createTime = '',
    orderStatus = '',
    orderName = '',
    userName = ''
  } = route.query

  const isValidType = Object.keys(businessRoutes).includes(type as string)

  if (isMobileDevice()) {
    if (type === 'generateOperation') {
      window.location.href =
        import.meta.env.VITE_BASE_URL +
        '/deepoil/#/?type=' +
        type +
        '&id=' +
        id +
        '&userId=' +
        userId +
        '&deptId=' +
        deptId +
        '&createTime=' +
        createTime +
        '&orderStatus=' +
        orderStatus +
        '&orderName=' +
        orderName +
        '&userName=' +
        userName
    } else if (type === 'rdReportApproval') {
      window.location.href =
        import.meta.env.VITE_BASE_URL + '/deepoil/#/pages/ruiDu/approval?id=' + id
    } else if (type === 'rdReportDetail') {
      window.location.href =
        import.meta.env.VITE_BASE_URL + '/deepoil/#/pages/ruiDu/detail?id=' + id
    } else if (type === 'rdDailyReport') {
      window.location.href =
        import.meta.env.VITE_BASE_URL + '/deepoil/#/pages/ruiDu/edit?id=' + id + '&istime=false'
    } else if (type === 'rhDailyReport') {
      window.location.href = import.meta.env.VITE_BASE_URL + '/deepoil/#/pages/ruihen/edit?id=' + id
    } else if (type === 'rhReportApproval') {
      window.location.href =
        import.meta.env.VITE_BASE_URL + '/deepoil/#/pages/ruihen/approval?id=' + id
    } else if (type === 'ryDailyReport') {
      window.location.href =
        import.meta.env.VITE_BASE_URL + '/deepoil/#/pages/ruiying/edit?id=' + id
    } else if (type === 'ryReportApproval') {
      window.location.href =
        import.meta.env.VITE_BASE_URL + '/deepoil/#/pages/ruiying/approval?id=' + id
    } else if (type === 'ryXjDailyReport') {
      window.location.href =
        import.meta.env.VITE_BASE_URL + '/deepoil/#/pages/ruiyingx/edit?id=' + id
    } else if (type === 'ryXjReportApproval') {
      window.location.href =
        import.meta.env.VITE_BASE_URL + '/deepoil/#/pages/ruiyingx/approval?id=' + id
    } else if (type === 'qhse-accident') {
      window.location.href = import.meta.env.VITE_BASE_URL + '/deepoil/#/pages/qhse/detail?id=' + id
    } else {
      window.location.href =
        import.meta.env.VITE_BASE_URL +
        '/deepoil/#/?type=' +
        type +
        '&id=' +
        id +
        '&userId=' +
        userId
    }
    // 移动端跳转deepoil协议
    // try{
    //   const typelower = type.toLowerCase();
    //   window.location.href = `<a>deepoil://${typelower}/${id}</a>`
    // }catch (error) {
    //   alert(error.message)
    // }
    // href.value = 'deepoil://'+type+'/'+id
    // nextTick(()=>{
    //   setTimeout(clickA,1000)
    // })
  } else if (isValidType) {
    authUtil.setTenantId('1')
    const res = await LoginApi.simpleLogin(userId)
    if (!res) {
      return
    }

    authUtil.setToken(res)
    // PC端路由跳转
    if (type === 'generateInspect') {
      push({ name: 'InspectOrderWrite', params: { id } })
    } else if (type === 'failureReport') {
      push({
        name: 'BpmProcessInstanceDetail',
        query: {
          id: id
        }
      })
    } else if (type === 'generateMaintain') {
      debugger
      push({ name: 'MaintainEdit', params: { id } })
    } else if (type === 'maintainOut') {
      push({
        name: 'BpmProcessInstanceDetail',
        query: {
          id: id
        }
      })
    } else if (type === 'generateOperation') {
      // push({
      //   name: 'FillOrderInfo',
      //   params: { deptId,userId,createTime,id,orderStatus }
      // })
      id = deptId + ',' + userId + ',' + createTime + ',' + id + ',' + orderStatus
      push({ name: 'FillOrderInfo', params: { id } })
    } else if (type === 'generateMaintenance') {
      push({
        name: 'IotMainWorkOrderBom',
        params: { id }
      })
    } else if (type === 'rdDailyReport') {
      push({
        path: '/iotdayilyreport/FillDailyReport',
        query: { id: id, mode: 'edit' }
      })
    } else if (type === 'rdReportApproval') {
      push({
        path: '/iotdayilyreport/IotRdDailyReport',
        query: { id: id, mode: 'approval' }
      })
    } else if (type === 'rdReportDetail') {
      push({
        path: '/iotdayilyreport/IotRdDailyReport',
        query: { id: id, mode: 'detail' }
      })
    } else if (type === 'rhDailyReport') {
      push({ path: '/iotdayilyreport/IotRhDailyReport/fill', query: { id: id } })
    } else if (type === 'rhReportApproval') {
      push({
        path: '/iotdayilyreport/IotRhDailyReport/approval',
        query: { id: id }
      })
    } else if (type === 'ryDailyReport') {
      push({ path: '/iotdayilyreport/IotRyDailyReport/fill', query: { id: id } })
    } else if (type === 'ryReportApproval') {
      push({
        path: '/iotdayilyreport/IotRyDailyReport/approval',
        query: { id: id }
      })
    } else if (type === 'ryXjDailyReport') {
      push({ path: '/iotdayilyreport/IotRyXjDailyReport/fill', query: { id: id } })
    } else if (type === 'ryXjReportApproval') {
      push({
        path: '/iotdayilyreport/IotRyXjDailyReport/approval',
        query: { id: id }
      })
    } else if (type === 'qhse-accident') {
      push({
        path: '/qhse/qhse_faultReport/detail',
        query: { id: id }
      })
    }
  } else {
    // 默认跳转
    push({ name: 'Login' })
  }
})
</script>

<style scoped>
.loading {
  padding-top: 100px;
  font-size: 16px;
  color: #666;
  text-align: center;
}

.wxtip {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 998;
  display: block;
  width: 100%;
  height: 100%;
  text-align: center;
  background: rgb(0 0 0 / 80%);
}

.wxtip-icon {
  position: absolute;
  display: block;
  width: 100%;
  height: 100%;
  background: url('@/assets/imgs/bgzz.png') center center no-repeat;
  background-size: cover; /* 新增样式，使背景图片覆盖整个元素 */
}

.wxtip-txt {
  margin-top: 107px;
  font-size: 16px;
  line-height: 1.5;
  color: #fff;
}
</style>
