import React, { Component } from "react";
import { connect } from "react-redux";
import BannerContainer from '@containers/TypeConfigContainer';
//  引入组件
import BodyContainer from "@components/BodyContainer";
import {ConfirmModal} from '@commonComp';
import store from '@store';

import {HIDEDROP,SETMINSCREEN,SETSYSTEMCONFIG,SETPRE,SETREADDITEMS,RESET_SELECT_TAG,ISREAD} from '@store/types/homePage.js';
import {embedPush,getMRAnalyse} from '@store/async-actions/pushMessage';
import {CLEAR_SEARCH,DELETE_MAIN_SELECTED_TAGS} from '@types/mainSuit';
import {CURRENT_CLEAR,DELETE_CURRENT_SELECTED_TAGS} from '@types/currentIll';
import {OTHERHIS_CLEAR,DELETE_OTHER_SELECTED_TAGS} from '@types/otherHistory';
import {CHECKBODY_CLEAR,DELETE_CHECK_SELECTED_TAGS} from '@types/checkBody';
import style from './index.less';
import {getInitModules,getChronic,getSystemConfig,getCommonData,getHosData} from '@store/async-actions/homePage.js';
import {getPreMsg} from '@store/async-actions/patInfo.js';
import { getUrlArgObject,parseNameVal,pushAllDataList } from "@utils/tools";
import $ from 'jquery';
import loading from '@common/images/loading.gif';

class HomePage extends Component {
    constructor() {
        super();
        this.state={
            timer:null,
        };
        this.hidePops = this.hidePops.bind(this);
    }
    componentDidMount(){
        const {setWindow,getChronicList,getConfig,getCommon,clearAddItems} = this.props;
        //清除已保存的血压标签数据
        clearAddItems();
        //获取配置
        // getConfig();
        getCommon()
        //getChronicList&&getChronicList();//获取慢病列表
        // 获取并监听窗口宽度，用于有横向滚动条时患者信息和打印预览跟随滚动
        let width = $(window).width();
        let height = $(window).height();
        setWindow && setWindow({width,height});
        
        $(window).resize(function(){
            let reWidth = $(window).width(); 
            let reHeight = $(window).height();  
            setWindow && setWindow({width:reWidth,height:reHeight});
        })    
        //光标没落到元素上继续backspace防止页面回退
        $(window).on("keydown",(e)=>{   
            let ev = e || window.event;  
            if(ev.keyCode==8){
                const elem = ev.srcElement || ev.target;
                const nodeN = elem.nodeName;
                if(nodeN != "SPAN" && nodeN != "DIV" && nodeN != "INPUT" && nodeN != "TEXTAREA"){
                    return false;
                }
            } 
        })
    }
    hidePops(){
        const {hideAllDrop,handleClear,resetSelect } = this.props;
        //重置选中范围
        resetSelect();
        if(!this.isBar){    //onMousrdown的目标为滚动条时，标签填写单不关闭
          hideAllDrop();        //隐藏填写单
          handleClear&&handleClear();       //清空搜索结果
        }
    }
  setElem(e){
      //onMousedown的目标为滚动条时，标签填写单不关闭
    if(e.target.className=='scrollbar'){
      this.isBar = true;
    }else{
      this.isBar = false;
    }
  }
  handleKeyUp(e){
      const {deleteLabels} = this.props;
    //删除选中标签
    if(e.keyCode==8||e.keyCode==46){      //backspace或del都可删除
      deleteLabels();
    }
  }
    render() {
        const {flag,showPre,noReferRecord,referRecord,mode} = this.props;
        return <div className={style['home-page']}
                    onClick={this.hidePops} onmousedown={this.setElem.bind(this)} onkeyup={this.handleKeyUp.bind(this)}>
            <BannerContainer />
            {/* <InfoTitle /> */}
            <BodyContainer mode={mode}></BodyContainer>
          <ConfirmModal visible={showPre} okText='是' cancelText='否' confirm={referRecord}  cancel={noReferRecord} close={noReferRecord}>
              <div className={style['confirm-info']}>是否引用预问诊信息？</div>
              <p className={style.tip}>(引用数据会清除当前内容)</p>
          </ConfirmModal>
          {/* {flag?<div className={style['mask']}>
                <img src={loading} className={style['load']}/>
            </div>:""} */}
        </div>;
    }
}

const mapStateToProps = function (state) {
  return {
    flag:state.homePage.loadingFlag,
    showPre:state.homePage.showPre,
    mode:state.typeConfig.typeConfig[0]
  }
};
const boxMap = {
  '1':DELETE_MAIN_SELECTED_TAGS,
  '2':DELETE_CURRENT_SELECTED_TAGS,
  '3':DELETE_OTHER_SELECTED_TAGS,
  '4':DELETE_CHECK_SELECTED_TAGS
};
const mapDispatchToProps = function (dispatch) {
  return {
    clearAddItems(){
      dispatch({
        type:SETREADDITEMS
      })
    },
    referRecord(){
        const state = store.getState();
        const {homePage} = state;
        pushAllDataList(1,'push',homePage.preData,'preIcss');
        dispatch(embedPush({mode:5}));
        dispatch(getMRAnalyse());
        dispatch({type:SETPRE,show:false});
      },
      noReferRecord(){
        dispatch({type:SETPRE,show:false});
      },
      handleClear(){//清空搜索结果
           dispatch({type:CLEAR_SEARCH});
           dispatch({type:CURRENT_CLEAR});
           dispatch({type:OTHERHIS_CLEAR});
           dispatch({type:CHECKBODY_CLEAR});
      },
        hideAllDrop(){
            dispatch({
              type:HIDEDROP
            });
        },
        /*getInit(){
            dispatch(getInitModules);
        }*/
        setWindow(obj){
            dispatch({
                type:SETMINSCREEN,
                obj:obj
            })
        },
        getChronicList(){
            dispatch(getChronic)
        },
        getConfig(){
          getSystemConfig().then((res)=>{
            if(res.data.code=='0'){
                dispatch({
                  type:SETSYSTEMCONFIG,
                  data: parseNameVal(res.data.data)
                })
            }else{

            }
          });
        },
        getCommon(){
          dispatch(getCommonData())
          dispatch(getHosData())
        },
      deleteLabels(){   //选中删除标签
        const state = store.getState();
        const {homePage} = state;
        const start = homePage.select_start;
        const end = homePage.select_end;
        const boxMark = homePage.select_boxMark;
        if(!boxMark||start===undefined||end===undefined)return;
        //删除后清除选中标记,放在删除事件后面会有清空不及时的问题
        dispatch({
          type:RESET_SELECT_TAG
        });
        dispatch({
          type:boxMap[boxMark],
          start,
          end,
          boxMark
        });
        dispatch({
          type:ISREAD
        })
      },
      resetSelect(){
          dispatch({
            type:RESET_SELECT_TAG
          })
      }
    }
};

export default connect(mapStateToProps, mapDispatchToProps)(HomePage);