import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { sessionConsts } from '../../consts/storageConsts';
import { Store } from 'amos-tool';
import Header from './../main/header';
import loadScripts from 'dt2react/lib/utils/loadScripts';
import { outMap } from './../../consts/urlConsts';

const lsTool = Store.lsTool;

/**
 * root
 *监控主屏
 * @class RootView
 * @extends {Component}
 */
class RootView extends Component {
  constructor(props) {
    super(props);
    this.state = {
      submenu: [],
      extendSystem: [],
      loadAmap: false,
      BasicMap: null
    };
  }

  componentWillMount() {
    let systemMenu = JSON.parse(lsTool.read(sessionConsts.systemMenu));
    this.setState({ extendSystem: systemMenu });
  }

  componentDidMount() {
    if (outMap) {
      Object.keys(localStorage).map(e => {
        if (e.indexOf('_AMap') === 0) {
          localStorage.removeItem(e);
        }
      });
      let script = null;
      let mainUI = null;

      script = {
        key: 'amapscripts',
        //url: `//172.16.3.121:8085/extra/amap/js/outamap.1.4.6.js?rnd= ${Math.random()}`
        url: `/extra/amap/js/outamap.1.4.6.js?rnd= ${Math.random()}`
      };
      mainUI = {
        key: 'mainUI',
        //url: '//10.56.28.228/webapi.amap.com/ui/1.0/main.js'
        url: '/extra/amap/js/outmain.1.4.6.js'
      };

      loadScripts.asyncLoadScript(script, () => {
        loadScripts.asyncLoadScript(mainUI, () => {
          setTimeout(() => {
            this.setState({
              loadAmap: true,
              BasicMap: require('./mapScreen/BasicMap')
            });
          }, 2000);
        });
      });
    } else {
      this.setState({
        loadAmap: true,
        BasicMap: require('./mapScreen/BasicMap')
      });
    }
  }

  componentWillUnmount() {

  }
  render() {
    const { children } = this.props;
    let BasicMap = this.state.BasicMap;
    return (
      <div className="view-root">
        <div className="header">
          <Header extendSystem={this.state.extendSystem} add={this.add} />
        </div>
        <div className="content-body">
          {children}
          { this.state.loadAmap && <BasicMap />}
        </div>
      </div>
    );
  }
}

RootView.propTypes = {
  children: PropTypes.node
};

export default RootView;
