// 润乾报表公共组件
import React from 'react';
import { $http } from 'containers/config/https';
class reports extends React.Component {
    constructor(props) {
        super(props);
        let session = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        let sessionUserID=session.userID;
        let sessionUserName=session.userName;
        let sessionLocID=session.locID;
        let sessionLocDesc=session.locDesc;
        let sessionGroupID=session.groupID;
        let sessionGroupDesc=session.groupDesc;
        let sessionHospID=session.hospDesc;
        let sessionHospDesc=session.hospDesc;
        let sessionStr="&sessionUserID="+sessionUserID+"&sessionUserName="+sessionUserName+"&sessionLocID="+sessionLocID+"&sessionLocDesc="+sessionLocDesc+"&sessionGroupID="+sessionGroupID+"&sessionGroupDesc="+sessionGroupDesc+"&sessionHospID="+sessionHospID+"&sessionHospDesc="+sessionHospDesc;
        this.sessionStr=sessionStr;
        this.state = {
            path:'',
            //debugPath:"http://localhost:6868/demo/reportJsp/preview.jsp?rpxHome=C%3A%2FUsers%2Fxys%2FDesktop%2F报表&dfxHome="   //本地预览路径
            debugPath:"http://localhost:6868/demo/reportJsp/preview.jsp?rpxHome=C:/Users/xys/Desktop/润乾安装文件&dfxHome="    //^头要转成encode格式
        }
    }
    componentDidMount(){
        if (!this.props.debug){
            this.getRaqSoftPath();
        }
    }

    /**
     * @ByYQL: 报表配置走新路径配置，入参增加appCode:'newRaqsoft'，没有则表示走之前的配置数据
     * Create 2023-3-27
     */
    //获取润乾服务器路径
    getRaqSoftPath = () => {
        $http.post('urlS', this, {
            code: "01040114",
            data:  {
                params: [{appCode:'newRaqsoft'}]
            },
            success: function (res) {
                if(res.errorCode==0){
                    this.setState({path:res.result.path});
                }else {
                    message.error("没数据");
                }
            }
        });
    }
    getUrl=()=>{
        let url="";
        let splitUrl="";
        let raqName=this.props.raqName ? this.props.raqName:"";
        const params=this.props.params ? this.props.params:{};
        let paramStr="";
        for (let key in params){
            paramStr=paramStr+"&"+key+"="+params[key];
        }
        if (raqName.indexOf(".rpx") === -1) {
            raqName=raqName+".rpx";
        }
        if (this.state.path){
            if(this.state.path.indexOf("//")>-1) {
                let newUrl=this.state.path.split("//")[1];
                if(newUrl.indexOf("/")>-1){
                    for(var i=1;i<newUrl.split("/").length;i++){
                        splitUrl=splitUrl+"/"+newUrl.split("/")[i];
                        // if(splitUrl===''){
                        //     splitUrl=newUrl.split("/")[i];
                        // }else{
                        //     splitUrl=splitUrl+"/"+newUrl.split("/")[i];
                        // }
                    }
                }else{
                    splitUrl="/"+newUrl;
                }
            }else {
                splitUrl=this.state.path;
            }
            url=window.location.protocol+"//"+window.location.host+splitUrl+'?rpx='+raqName+paramStr+this.sessionStr;
        }else if (this.state.debugPath){
            url=this.state.debugPath+'&rpx='+raqName+paramStr+this.sessionStr;
        }
        return encodeURI(url);
    }
    render() {
        return (
            <div style={{height:'99%'}}>
                <iframe
                    style={{width:'100%',border:'0px',height:'100%'}}
                    // sandbox="allow-scripts allow-forms allow-same-origin"
                    scrolling="auto"
                    src={this.getUrl()}
                />
            </div>
        );
    }
};
export default reports;
