/**
 * 
 * 对外组件调用
 */
import React from 'react';
import { Popconfirm, Icon, Table, Form, message, Result } from 'antd';
import { $http } from 'containers/config/https';
import { Util } from 'tools/index';
import asyncComponent from '../../routers/AsyncComponent.js';
import store from 'store';
import Consultation from 'pages/scheduling/Consultation';

class CommonComponent extends React.Component {
    constructor() {
        super();
        var host = window.location.host;
        this.state = {
            host: host
        };
    }

    //取Url对应的参数
    getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = this.props.location ? this.props.location.search.substr(1).match(reg) : null;
        if (r != null) {
            return decodeURIComponent(r[2]);
        }
        return null;
    }

    componentDidMount() {
        this.handelHeight()
        var url = decodeURIComponent(this.props.location.search);
        if (url.indexOf("?") > -1) {
            var arr = url.split('?')
            this.AutoLogon(arr[1]);
        } else {
            message.error('地址不合法！')
            return
        }
    }

    componentWillReceiveProps(nextProps){
        var url = decodeURIComponent(nextProps.location.search);
        if (url.indexOf("?") > -1) {
            var arr = url.split('?')
            this.AutoLogon(arr[1]);
        } else {
            message.error('地址不合法！')
            return
        }
}

    handelHeight() {
        var s = document.body.clientHeight;
        let header = +this.state.defaultMenuType === 2 ? 50 + 40 + 10 : 48 + 22;
        let tableHeight = s - header - 190;
        store.dispatch({
            type: "compileY",
            value: tableHeight
        });
    }

    componentWillUnmount() {
        sessionStorage.removeItem('patientData')
        sessionStorage.clear()
    }

    AutoLogon(url) {
        var params = {
            params: {
                data: url
            }
        }
        $http.post('urlDeault', this, {
            code: "99010007",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    document.title = '坐诊医生'
                    let userData = Object.assign({}, res.result[0])
                    userData.messageTime = 1
                    window.sessionStorage.setItem("userData", JSON.stringify(userData));
                    window.sessionStorage.setItem("ipConfig", JSON.stringify(res.ipConfig));
                    this.setState({
                        userData: res.result
                    }, () => {
                        if (this.getQueryString('admNo')) {
                            this.loadSessionData()
                        }
                        this.loadComponent()
                    })
                } else {
                    this.setState({
                        userData: {},
                        errStatus: true
                    })
                }
            }
        });
    }

    loadComponent() {
        var params = {
            params: [{
                code: this.getQueryString('path')
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040208",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    if (!Util.isEmpty(res.result)) {
                        document.title = res.result.title
                    }
                    this.setState({
                        componentSource: res.result
                    })
                }
            }
        });
    }

    loadSessionData() {
        var params = {
            params: [{
                admNo: this.getQueryString('admNo')
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050225",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    if (!Util.isEmpty(res.result)) {
                        var patientData = {}
                        patientData = {
                            admID: res.result.admID,
                            patID: res.result.patID
                        }
                        sessionStorage.setItem('patientData', JSON.stringify(patientData))
                    } else {
                        this.setState({
                            errStatus: true,
                            userData: {}
                        })
                    }
                } else {
                    this.setState({
                        errStatus: true,
                        userData: {}
                    })
                }
            }
        });
    }

    //获取对应的加载组件
    loadStepCompent() {
        const { componentSource } = this.state;
        if (!Util.isEmpty(componentSource)) {
            const Component = asyncComponent(() => import('../../' + componentSource.component));
            return <Component />
        }
    }

    render() {

        return (
            <div className="common-component-body" style={{ width: '100%', height: '100%' }}>
                {!Util.isEmpty(this.state.userData) ? this.loadStepCompent() :
                    (this.state.errStatus == true ? <Result style={{ marginTop: '10%' }} title={'当前链接地址访问失败！'} status="500"
                        subTitle={<div>
                            <h3>1、访问链接不正确；</h3>
                            <h3>2、当前组件不存在；</h3>
                            <h3>3、没有访问的权限；</h3>
                            <h3>...</h3>
                        </div>} /> : null)
                }
            </div>
        )
    }
};
export default CommonComponent;