// 第三方链接 -- 外部链接
import React from 'react';
import { MD5, Aes } from 'tools';
import { httpConfig } from 'containers/config/httpConfig';

export default class ExternalLinks extends React.Component {
  constructor() {
    super();
    this.state = {
      url: '',
    }
  }

  componentDidMount() {
    let url = '';
    var userData = React.$getSessionData('userData');
    //网络健康宣教 链接
    let sessionID = userData && userData.sessionID ? userData.sessionID : ''; // sessionID
    let hospID = userData && userData.hospID ? userData.hospID : ''; // 医院ID
    let locID = userData && userData.locID ? userData.locID : ''; // 科室ID
    let groupID = userData && userData.groupID ? userData.groupID : ''; // 角色ID
    let userID = userData && userData.userID ? userData.userID : ''; // 用户ID
    let userName = userData && userData.userName ? userData.userName : ''; // 用户姓名
    // ip地址
    let ipConfig = React.$getSessionData('ipConfig');
    if (this.props.location) {
      let stateParams;
      if ('location' in this.props && this.props.location.state && this.props.location.state) {
        stateParams = this.props.location.state;
        React.$setSessionData('ExternalLinks', stateParams);
      } else {
        stateParams = React.$getSessionData('ExternalLinks');
      }
      if (stateParams) {
        let permiss = '3';
        let linkHealth = '/Article/ArticleWlyy/Index?' + 'drId=' + userID + '&drname=' + userName + '&permiss=' + permiss;

        //问卷填写和查看接口 链接
        let userid = '1';
        let recordid = '1';
        let linkQuestionnaire = '/Survey/External/QuestIndexcardno?' + 'userid=' + userid + '&recordid=' + recordid;

        let tt = ((new Date().getTime() * 10000) + 621355968000000000); //UTC时间戳
        let pid = stateParams.PartnerId ? stateParams.PartnerId : '';
        let PartnerKey = stateParams.PartnerKey ? stateParams.PartnerKey : '';
        let encrypt = stateParams.encrypt ? stateParams.encrypt : '';

        if (String(stateParams.flag) === "0") {
          //MD5加密
          let signed = MD5.md5(pid + tt + PartnerKey);
          let linkUrl = encodeURIComponent(linkHealth)
          url = stateParams.url + 'pid=' + pid + '&tt=' + tt + '&signed=' + signed + '&url=' + linkUrl + '&encrypt=' + encrypt;
          // window.open(stateParams.url+'pid='+pid+'&tt='+tt+'&signed='+signed+'&url='+linkUrl+'&encrypt='+encrypt);
        } else if (String(stateParams.flag) === "1") {
          //MD5加密
          let signed = MD5.md5(pid + tt + PartnerKey);
          let linkUrl = encodeURIComponent(linkQuestionnaire);
          url = stateParams.url + 'pid=' + pid + '&tt=' + tt + '&signed=' + signed + '&url=' + linkUrl + '&encrypt=' + encrypt;
        } else if (stateParams.flag === "session") {
          url = stateParams.url ? (stateParams.url + "?sessionID=" + sessionID + "&hospID=" + hospID + "&locID=" + locID + "&groupID=" + groupID + "&userID=" + userID) : "";
        } else if (stateParams && stateParams.urlType == 'insunew') { // 医保平台
          let windowHost = window.location && window.location.protocol && window.location.host ? (window.location.protocol + '//' + window.location.host) : (window.location && window.location.origin ? window.location.origin : '');
          let windowHostSplit = windowHost && windowHost.indexOf(':') > -1 ? windowHost.split(':') : []; // 如果存在端口号的情况则将端口号去掉 [不存在端口号的则不拼接本地配置的医保端口号]
          windowHost = windowHostSplit && windowHostSplit.length > 2 ? (windowHostSplit[0] + ':' + windowHostSplit[1]) : windowHost;
          let insunewParams = JSON.stringify({ sessionData: { ...userData, ...ipConfig }, path: stateParams.url, defaultMenuType: stateParams?.menuType || 3 });
          url = stateParams.url ? (windowHost + (httpConfig && 'insuPort' in httpConfig && httpConfig.insuPort && windowHostSplit && windowHostSplit.length > 2 ? (':' + httpConfig.insuPort) : '') + '/insunew/#' + stateParams.url + '?params=' + Aes.Encrypt(insunewParams)) : '';
          // url = stateParams.url ? ('http://127.0.0.1:3002/#' + stateParams.url + '?params=' + Aes.Encrypt(insunewParams)) : '';
        } else {
          url = stateParams.url ? stateParams.url : "";
        }
      }
    } else {
      let parasData = this.props?.paras || {};
      if (parasData && parasData.url) {
        /**
         * @ByYQL: 通过入参中的type，保存至sessionStorage，用于区分子项目打开不同功能模块
         * urlType为 1 时需拼装url链接，动态获取当前域名路径
         * Create 2022-2-9
         */
        if (parasData && parasData.urlType == '1') {
          var nowUrl = window.location.href   //获取当前路径
          // 当配置传入的flag等于session的话就给当前链接传入session数据
          if (parasData && parasData.flag && parasData.flag === "session") {
            url = parasData.url ? (nowUrl.substr(0, nowUrl.indexOf("/his/#/")) + parasData.url + "?sessionID=" + sessionID + "&hospID=" + hospID + "&locID=" + locID + "&groupID=" + groupID + "&userID=" + userID) : "";
          } else {
            url = parasData.url ? nowUrl.substr(0, nowUrl.indexOf("/his/#/")) + parasData.url : "";
          }
        } else if (parasData && parasData.urlType == 'insunew') { // 医保平台
          let windowHost = window.location && window.location.protocol && window.location.host ? (window.location.protocol + '//' + window.location.host) : (window.location && window.location.origin ? window.location.origin : '');
          let windowHostSplit = windowHost && windowHost.indexOf(':') > -1 ? windowHost.split(':') : []; // 如果存在端口号的情况则将端口号去掉 [不存在端口号的则不拼接本地配置的医保端口号]
          windowHost = windowHostSplit && windowHostSplit.length > 2 ? (windowHostSplit[0] + ':' + windowHostSplit[1]) : windowHost;
          let insunewParams = JSON.stringify({ sessionData: { ...userData, ...ipConfig }, path: parasData.url, defaultMenuType: parasData?.menuType || 3 });
          url = parasData.url ? (windowHost + (httpConfig && 'insuPort' in httpConfig && httpConfig.insuPort && windowHostSplit && windowHostSplit.length > 2 ? (':' + httpConfig.insuPort) : '') + '/insunew/#' + parasData.url + '?params=' + Aes.Encrypt(insunewParams)) : '';
          // url = parasData.url ? ('http://127.0.0.1:3002/#' + parasData.url + '?params=' + Aes.Encrypt(insunewParams)) : '';
        } else {
          if (parasData && parasData.flag && parasData.flag === "session") {
            url = parasData.url ? (parasData.url + "?sessionID=" + sessionID + "&hospID=" + hospID + "&locID=" + locID + "&groupID=" + groupID + "&userID=" + userID) : "";
          } else {
            url = parasData.url ? parasData.url : "";
          }
        }
        //通过入参中的type，保存至sessionStorage，用于区分子项目打开不同功能模块
        if (parasData && parasData.type) {
          window.sessionStorage.setItem("ExternalLinkType", parasData.type);
        }
      } else {
        console.log("this.props.url", this.props.url)
        url = this.props.url ? this.props.url : "";
      }
    }
    this.setState({ url });
  }
  render() {
    let { url } = this.state;
    return (
      <div style={{ width: '100%', height: "100%", overflow: 'hidden' }}>
        <iframe
          style={{ width: '100%', height: "100%", overflow: 'visible' }}
          ref="iframe"
          src={url}
          width="100%"
          height="100%"
          scrolling="no"
          frameBorder="0"
        />
      </div>
    )
  }
}
