import {
  createWebHashHistory,
  createRouter,
  createWebHistory
} from 'vue-router';
import api from '../api/user.js';
import cookie from '@/utils/cookie.js';
import host from "../utils/host.js"
const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home.vue')
  },
  {
    path: '/caseList',
    name: 'caseList',
    component: () => import('@/views/caseList.vue')
  },
  {
    path: '/caseIntro',
    name: 'caseIntro',
    component: () => import('@/views/caseIntro.vue')
  },
  {
    path: '/caseDetail',
    name: 'caseDetail',
    component: () => import('@/views/caseDetail.vue')
  },
  {
    path: '/comment',
    name: 'comment',
    component: () => import('@/views/comment.vue')
  },
  {
    path: '/result',
    name: 'result',
    component: () => import('@/views/result.vue')
  },
];
const router = createRouter({
  // createWebHashHistory URL 带井号
  // createWebHistory URL 去井号
  history:createWebHistory('web'),
  routes: routes,
});
router.beforeEach(async(to, from, next) => {
   if(to.path!=='/caseIntro' || to.path!=='/result'){
     document.title = '互动病例';
   }
  let source=to.query.source
  // 权限验证逻辑
  if (source == 3) {
        if(to.path!=='/result'){
          localStorage.setItem('token_other','')
        }
        let token=localStorage.getItem('token_other');
        if(!token){
        const {data,code}=await api.login({
          source:3,
          platform_key:to.query.platform_key,
          user_iden:to.query.user_iden,
          mobile_encryption:to.query.mobile_encryption,
          doctor_name:to.query.doctor_name,
          hospital_name:to.query.hospital_name,
          department_name:to.query.department_name,
          address:to.query.address,
          title:to.query.title,
          share_user_id:to.query.share_user_id,
          is_white:to.query.is_white,
          project_id:to.query.project_id
        })
        if(code==200){
          localStorage.setItem('token_other',data.token);
          localStorage.setItem('userInfo',JSON.stringify(data));
          localStorage.setItem('source',3);
          next();
        }
     }else{
      next();
     }

  } else {
    //localStorage.setItem('token_gandan','eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMTg5NDI3NjIyOTg5NzM5MjEyOCIsInBsYXRmb3JtX2lkIjoiMSIsImV4cCI6MTc0NDQyMzU1NCwibmJmIjoxNzQxODMxNTU0LCJpYXQiOjE3NDE4MzE1NTR9.LuU5xpSpTQq6VY7qZJf854qMoNCMVjRj1vi1_Ii8akM');
    let token='';
    let token_other=localStorage.getItem('token_other');
    let token_gandan=localStorage.getItem('token_gandan');
    if(source==3){
      if(token_other){
        token=token_other;
      }
    }else{
      if(token_gandan){
        token=token_gandan;
      }
    }
    if(!token){
      let unionid='';
      let platform_key='123456';
      if(import.meta.env.MODE==='production') {
        platform_key='dk&*dj1P'
      };
      let userInfo=cookie.readCookie('wechat_user_info');
      let videoToken=cookie.readCookie('video_token');
      if(userInfo){
        let json=JSON.parse(userInfo);
         unionid=json.unionid;
         const {data,code}=await api.login({
          source:2,
          platform_key:platform_key,
          unionid:unionid?unionid:'',
          user_iden:to.query.uuid?to.query.uuid:'',
        })
        if(code==200){
          localStorage.setItem('token_gandan',data.token);
          localStorage.setItem('userInfo',JSON.stringify(data));
          localStorage.setItem('source','');
          next();
        }
      }else if(videoToken){
        const {data,code}=await api.login({
          source:2,
          platform_key:platform_key,
          unionid:unionid?unionid:'',
          user_iden:to.query.uuid?to.query.uuid:'',
          token:videoToken
        })
        if(code==200){
          localStorage.setItem('token_gandan',data.token);
          localStorage.setItem('userInfo',JSON.stringify(data));
          localStorage.setItem('source','');
          next();
        }
      }else{
        let url='https://dev-wx.igandan.com';
        if(host.indexOf('//dev-caseplatform.igandan.com')==-1){
          url='https://wx.igandan.com'
        }
         window.location.href = url+"/hcp/Signup2020online_tologin?back_url="+host+"/web/home"
      }
		}else{
     next();
    }
  }



});
export default router;