/**
 * 依托于链接管理的链接跳转逻辑
 * Copyright (c) 2024 zc
 */
import Cookies from "js-cookie";
import { Message } from 'element-ui'
import { systemLinkDetail,menuLinkGet,menuLinkPost } from "@/api/commonality/permission";

export function xmlRequestLink(item) {
  //查询连接管理-详情
  systemLinkDetail({linkId:item.linkId}).then(response => {
    if(!response.data.isAuth&&response.data.authMode != 1){
      noIsAuthNoCarryLink(response.data);
    }else if(!response.data.isAuth&&response.data.authMode == 1){
      noIsAuthCarryLink(response.data);
    }else if(response.data.isAuth&&response.data.authMode != 1){
      isAuthNoCarryLink(response.data);
    }else if(response.data.isAuth&&response.data.authMode == 1){
      isAuthCarryLink(response.data);
    }
  });
}
//无验证/无携参-跳转
export function noIsAuthNoCarryLink(item) {
  window.open(item.linkUrl,item.skipType?'_blank':'_self');
}
//无验证/携参-跳转
export function noIsAuthCarryLink(item) {
  let params = JSON.parse(item.params)
  let argument = '?';
  params.carryArgument.forEach((item,index)=>{
    if(item.type == 1){
      //数据-键值对
      argument = argument+(index==0?'':'&')+item.key+'='+item.value
    }else if(item.type == 2){
      //读取cookie
      argument = argument+(index==0?'':'&')+item.value+'='+Cookies.get(item.key)
    }else if(item.type == 3){
      //读取localStorage
      argument = argument+(index==0?'':'&')+item.value+'='+localStorage.getItem(item.key)
    }else if(item.type == 4){
      //读取sessionStorage
      argument = argument+(index==0?'':'&')+item.value+'='+sessionStorage.getItem(item.key)
    }else if(item.type == 5){
      //生成时间戳-区分13位或者10位
      if(item.num == '10'){
        argument = argument+(index==0?'':'&')+item.key+'='+Date.parse(new Date())/1000
      }else if(item.num == '13'){
        argument = argument+(index==0?'':'&')+item.key+'='+Date.parse(new Date())
      }
    }
  })
  window.open(item.linkUrl+argument,item.skipType?'_blank':'_self');
}
//验证/无携参-跳转
export function isAuthNoCarryLink(item) {
  let urlText = window.location.href.split('://')[0]+'://';
  let params = JSON.parse(item.params)
  let obj = {};
  params.requestArgument.forEach((item)=>{
    if(item.type == 1){
      //数据-键值对
      obj[item.key] = item.value;
    }else if(item.type == 2){
      //读取cookie
      obj[item.value] = Cookies.get(item.key);
    }else if(item.type == 3){
      //读取localStorage
      obj[item.value] = localStorage.getItem(item.key);
    }else if(item.type == 4){
      //读取sessionStorage
      obj[item.value] = sessionStorage.getItem(item.key);
    }else if(item.type == 5){
      //生成时间戳-区分13位或者10位
      if(item.num == '10'){
        obj[item.key] = Date.parse(new Date())/1000;
        formData.append(item.key,);
      }else if(item.num == '13'){
        obj[item.key] = Date.parse(new Date());
      }
    }
  })
  if(item.reqMethod == 'post'){
    if(item.paramType == 'JSON'){
      menuLinkPost(urlText+item.reqApi,obj).then(response => {
        window.open(item.linkUrl,item.skipType?'_blank':'_self')
      })
    }else if(item.paramType == 'formData'){
      let formData = new FormData();
      params.requestArgument.forEach((item)=>{
        if(item.type == 1){
          //数据-键值对
          formData.append(item.key,item.value);
        }else if(item.type == 2){
          //读取cookie
          formData.append(item.value,Cookies.get(item.key));
        }else if(item.type == 3){
          //读取localStorage
          formData.append(item.value,localStorage.getItem(item.key));
        }else if(item.type == 4){
          //读取sessionStorage
          formData.append(item.value,sessionStorage.getItem(item.key));
        }else if(item.type == 5){
          //生成时间戳-区分13位或者10位
          if(item.num == '10'){
            formData.append(item.key,Date.parse(new Date())/1000);
          }else if(item.num == '13'){
            formData.append(item.key,Date.parse(new Date()));
          }
        }
      })
      let xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function() {
        if (xhr.readyState === XMLHttpRequest.DONE) {
          if (xhr.status === 200) {
            let data = JSON.parse(xhr.response)
            if(data.code == 200){
              window.open(item.linkUrl,item.skipType?'_blank':'_self')
            }else{
              Message({
                message: '操作失败,请联系管理员',
                type: 'error',
                offset:100
              })
            }
          } else {
            Message({
              message: '操作失败,请联系管理员',
              type: 'error',
              offset:100
            })
          }
        }
      };
      xhr.open(item.reqMethod,urlText+item.reqApi);
      xhr.send(formData)
    }
  }else if(item.reqMethod == 'get'){
    menuLinkGet(urlText+item.reqApi,obj).then(response => {
      window.open(item.linkUrl,item.skipType?'_blank':'_self')
    })
  }
}
//验证/携参-跳转
export function isAuthCarryLink(item) {
  let urlText = window.location.href.split('://')[0]+'://';
  let params = JSON.parse(item.params)
  let obj = {};
  params.requestArgument.forEach((item)=>{
    if(item.type == 1){
      //数据-键值对
      obj[item.key] = item.value;
    }else if(item.type == 2){
      //读取cookie
      obj[item.value] = Cookies.get(item.key);
    }else if(item.type == 3){
      //读取localStorage
      obj[item.value] = localStorage.getItem(item.key);
    }else if(item.type == 4){
      //读取sessionStorage
      obj[item.value] = sessionStorage.getItem(item.key);
    }else if(item.type == 5){
      //生成时间戳-区分13位或者10位
      if(item.num == '10'){
        obj[item.key] = Date.parse(new Date())/1000;
        formData.append(item.key,);
      }else if(item.num == '13'){
        obj[item.key] = Date.parse(new Date());
      }
    }
  })
  if(item.reqMethod == 'post'){
    if(item.paramType == 'JSON'){
      menuLinkPost(urlText+item.reqApi,obj).then(response => {
        carryLink(item.linkUrl,item.skipType,params.carryArgument,response.data)
      })
    }else if(item.paramType == 'formData'){
      let formData = new FormData();
      params.requestArgument.forEach((item)=>{
        if(item.type == 1){
          //数据-键值对
          formData.append(item.key,item.value);
        }else if(item.type == 2){
          //读取cookie
          formData.append(item.value,Cookies.get(item.key));
        }else if(item.type == 3){
          //读取localStorage
          formData.append(item.value,localStorage.getItem(item.key));
        }else if(item.type == 4){
          //读取sessionStorage
          formData.append(item.value,sessionStorage.getItem(item.key));
        }else if(item.type == 5){
          //生成时间戳-区分13位或者10位
          if(item.num == '10'){
            formData.append(item.key,Date.parse(new Date())/1000);
          }else if(item.num == '13'){
            formData.append(item.key,Date.parse(new Date()));
          }
        }
      })
      let xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function() {
        if (xhr.readyState === XMLHttpRequest.DONE) {
          if (xhr.status === 200) {
            let data = JSON.parse(xhr.response)
            if(data.code == 200){
              carryLink(item.linkUrl,item.skipType,params.carryArgument,data.data)
            }else{
              Message({
                message: '操作失败,请联系管理员',
                type: 'error',
                offset:100
              })
            }
          } else {
            Message({
              message: '操作失败,请联系管理员',
              type: 'error',
              offset:100
            })
          }
        }
      };
      xhr.open(item.reqMethod,urlText+item.reqApi);
      xhr.send(formData)
    }
  }else if(item.reqMethod == 'get'){
    menuLinkGet(urlText+item.reqApi,obj).then(response => {
      carryLink(item.linkUrl,item.skipType,params.carryArgument,response.data)
    })
  }
}
//携参跳转
export function carryLink(linkUrl,skipType,carryArgument,data){
  let argument = '?';
  carryArgument.forEach((item,index)=>{
    if(item.type == 1){
      //数据-键值对
      argument = argument+(index==0?'':'&')+item.key+'='+item.value
    }else if(item.type == 2){
      //读取cookie
      argument = argument+(index==0?'':'&')+item.value+'='+Cookies.get(item.key)
    }else if(item.type == 3){
      //读取localStorage
      argument = argument+(index==0?'':'&')+item.value+'='+localStorage.getItem(item.key)
    }else if(item.type == 4){
      //读取sessionStorage
      argument = argument+(index==0?'':'&')+item.value+'='+sessionStorage.getItem(item.key)
    }else if(item.type == 5){
      //生成时间戳-区分13位或者10位
      if(item.num == '10'){
        argument = argument+(index==0?'':'&')+item.key+'='+Date.parse(new Date())/1000
      }else if(item.num == '13'){
        argument = argument+(index==0?'':'&')+item.key+'='+Date.parse(new Date())
      }
    }else if (item.type == 6) {
      if(item.key.indexOf('&&') != -1){
        let keyList = item.key.split('&&')
        let keyName = data;
        keyList.forEach((keyItem)=>{
          keyName = keyName[keyItem]
        })
        argument = argument+(index==0?'':'&')+item.value+'='+keyName;
      }else{
        argument = argument+(index==0?'':'&')+item.value+'='+data[item.key]
      }
    }
  })
  window.open(linkUrl+argument,skipType?'_blank':'_self')
}
