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';
import { Modal, Select } from 'amos-framework';

import  RiskControlTable  from './../monitor/statistical/RiskControlTable';
import  DangerControlTable  from './../monitor/statistical/DangerControlTable';
import  AlarmControlTable  from './../monitor/statistical/AlarmControlTable';
import  CenterLevelLeft  from './../monitor/statistical/indexLeft';
import  CenterLevelRight  from './../monitor/statistical/indexRight';


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,
      visible1: false,
      visible2: false,
      visible3: false,
      flag:false,
    };
  }

  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() {

  }


  cancel = () => {
    
    this.setState({

      visible1: false,
      visible2: false,
      visible3: false,
      flag:true,
    });


}
  getContext = (type) => {
    const { flag } = this.state;
    if(type==='1'){
      return <RiskControlTable refresh={flag}/>;
    }else if(type==='2'){
      return <DangerControlTable refresh={flag} />;
    }else if(type==='3'){
      return <AlarmControlTable refresh={flag} />;
    }
   
  };
  ondetails = (type) => {
    if(type==='1'){
      this.setState({

        visible1: true
  
      });

    }else if(type==='2'){
      this.setState({

        visible2: true
  
      });
    }else{
      this.setState({

        visible3: true
  
      });
    }
  };

  render() {

    const { visible1,visible2,visible3,flag } = this.state;
    let type1="1";
    let type2="2";
    let type3="3";



    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>


    <CenterLevelLeft  ondetails={ this.ondetails }  />
   
   <CenterLevelRight />
   <Modal
     header="风险管控情况"
     visible={visible1}
     className="risk-model-model99"
     width={'50%'}
     noDefaultFooter
     content={ this.getContext(type1)}
     onCancel={  this.cancel}
     destroyOnClose={true}
   />

     <Modal
     header="隐患管控情况"
     visible={visible2}
     className="risk-model-model99"
     width={'50%'}
     noDefaultFooter
     content={ this.getContext(type2)}
     onCancel={ this.cancel }
     destroyOnClose={true}
   />


     <Modal
     header="告警管控情况"
     visible={visible3}
     className="risk-model-model99"
     width={'50%'}
     noDefaultFooter
     content={this.getContext(type3)}
     onCancel={ this.cancel}
     destroyOnClose={true}
   />





      </div>
    );
  }
}

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

export default RootView;
