import React from 'react';
import './assets/css/App.css';
import { HashRouter } from 'react-router-dom';
import Routers from "./routers/Index.jsx";
import { $http } from "containers/config/https";
import { Util } from 'tools';
import Header from 'pages/common/component/Header.jsx';
import { Toast } from 'antd-mobile';

class App extends React.Component {
	constructor() {
		super();
		let weChatParams = sessionStorage.getItem("weChatParams") ? JSON.parse(sessionStorage.getItem("weChatParams")) : {};
		this.state = {
			weChatParams
		}
	}

	componentDidMount() {
		// http://localhost:7777/#/0001/00000001?code=081yLT000y2VgM1uzA1005p8Ti4yLT0j&state=SZSBARMYY#/0001/00000001
		let { weChatParams } = this.state;
		if (weChatParams && weChatParams.openID) { // 选择医院或者患者之后path路径会发生变化，如果openID存在的话
			this.queryWhetherToPayAttention(weChatParams.pubAccCode, weChatParams.openID)
		} else {
			let url = window.location.href.indexOf("?") !== -1 ? String(window.location.href).split("?")[1] : "";
			let code = Util.interceptTheValueOfTheURL(url, "code", "&", "="); // 判断字符串中是否包含&号
			let pubAccCode = Util.interceptTheValueOfTheURL(url, "state", "&", "="); // 判断字符串中是否包含&号
			this.getIDAccordingToURLParameter(code, pubAccCode); // 获取医院以及菜单数据
		}
	}

	handleHeadRef = ref => {
		this.headRef = ref
	}

	// 获取openID
	getIDAccordingToURLParameter = (code, pubAccCode) => {
		let data = {
			params: [{
				code,
				pubAccCode
			}]
		}
		$http.post("urlS", this, {
			code: "01020001",
			data: data,
			success: function (res) {
				let openID = "";
				// console.log("触发了获取openID")
				if (+res.errorCode === 0) {
					openID = res.result && res.result.openid ? res.result.openid : "";
					// 将获取的openID存到session
					let weChatParams = {
						code,
						pubAccCode,
						openID,
					}
					window.sessionStorage.setItem(
						"weChatParams",
						JSON.stringify(
							{ ...weChatParams }
						)
					);
				} else {
					Toast.info("获取信息异常，请关闭程序重新进入。", 3, () => { }, false);
					// 将获取的openID存到session
					openID = ""
					// openID = "oBowjwIRV-ykByyqjWaQ-kbfgCYA"
					// let weChatParams = {
					// 	code,
					// 	pubAccCode,
					// 	openID,
					// }
					// window.sessionStorage.setItem(
					// 	"weChatParams",
					// 	JSON.stringify(
					// 		{ ...weChatParams }
					// 	)
					// );
				}
				openID && this.queryWhetherToPayAttention(pubAccCode, openID)
			},
			error: function (error) {
				console.log(error)
				Toast.info("网络异常，请稍后重试。", 3, () => { }, false);
			}
		});
	}

	// 查询是否关注
	queryWhetherToPayAttention = (pubAccCode, openid) => {
		let data = {
			params: [{
				pubAccCode,
				openid
			}]
		}
		$http.post("urlS", this, {
			code: "01020020",
			data: data,
			success: function (res) {
				if (+res.errorCode === 0) {
					if (+res.result.subscribe === 1) {
						this.headRef.getHospSelectData();
					} else {
						// 跳转到关注界面
						let url = res.result && res.result.url ? res.result.url : '';
						console.log(url)
						window.location.href = url;
					}
				}
			},
			error: function (error) {
				Toast.info("网络异常，请稍后重试。", 3, () => { }, false);
			}
		});
	}

	render() {
		return (
			<HashRouter>
				<div className="app">
					<Header onRef={this.handleHeadRef} />
					<div className="content">
						<Routers />
					</div>
				</div>
			</HashRouter>
		)
	}
}
export default App;
