import React, { Component } from "react";
import style from "./index.less";
import { Radio } from '@commonComp';
import ChooseItem from "./ChooseItem";
import ScaleItem from "./ScaleItem";
import Information from '../Information'
import ChartItem from "./ChartItem";
import Notify from '@commonComp/Notify';
import {readyKeepHistory,deepClone} from '@utils/tools';
import loadingIcon from '@common/images/loading.gif';

class AssessResult extends Component {
  constructor(props) {
    super(props);
    const chooseSelecteds = deepClone(props.chooseSelecteds);
    const wholeIndexs = deepClone(props.wholeIndexs);      //深度复制，Object.assgin为浅复制，下下级会同源
    const wholeScale = deepClone(props.wholeScaleItems);
    const wholeResults = deepClone(props.wholeResults);

    this.state={
      chooseSelecteds:chooseSelecteds,     //大数据选择模块
      chartTimeTypes:{},      //图表模块
      wholeAssessItems:wholeIndexs,     //整体评估模块
      wholeScaleItems:wholeScale,
      wholeAssessText:props.wholeAssessText||{},     //整体评估补充说明
      wholeResults: wholeResults,
      calcuResult:props.calcuResult,     //计算公式结果
      addedScaleIds:props.addedScaleIds,    //已加入量表的id
    };
    this.handleChooseChange = this.handleChooseChange.bind(this);
    this.handleScaleDel = this.handleScaleDel.bind(this);
    this.handleScaleText = this.handleScaleText.bind(this);
  }
  handleScaleText(text){
    const {disName} = this.props;
    const obj = Object.assign({},this.state.wholeAssessText);
    obj[disName] = text;
      this.setState({
        wholeAssessText:obj
      })
  }
  handleScaleDel(isList,i,id,j){
    let ids = [...this.state.addedScaleIds||[]],idIndex='';
    if(id){
      idIndex = ids.findIndex((ii)=>id==ii);
      ids.splice(idIndex,1);
    }
    if(isList){         //删除全部量表中的项目
      const items = Object.assign({},this.state.wholeScaleItems);
      delete items[i];
      //items[i]=null;
      this.setState({
        wholeScaleItems: items,
        addedScaleIds:ids
      });
    }else{
      const items = Object.assign({},this.state.wholeAssessItems);
      const inx = items[i].findIndex(x=>x==j);
      items[i].splice(inx,1);
      this.setState({
        wholeAssessItems: items,
        addedScaleIds:ids
      });
    }
  }
  handleChooseChange(i,selects){
    const {chooseSelecteds} = this.state;
    this.setState({
      chooseSelecteds: Object.assign(chooseSelecteds,{[i]:selects})
    });
  }
  handleChartChange(i,selects){
    const {chartTimeTypes} = this.state;
    this.setState({
      chartTimeTypes:Object.assign(chartTimeTypes,{[i]:selects})
    });
  }
  componentWillUnmount(){
    //点确定关闭弹窗时把参数传到父组件去
    const {saveAssessInfos,isAssessConfirm,clearChartData} = this.props;
    clearChartData&&clearChartData();
    if(isAssessConfirm && readyKeepHistory() == 1){
      Notify.error("主诉不能为空");
    }else if(isAssessConfirm && readyKeepHistory() == 2){
      Notify.info('诊断不能为空');
    }
    if(isAssessConfirm){
      saveAssessInfos(this.state,readyKeepHistory());
    }
  }
  componentWillReceiveProps(next){
    if(JSON.stringify(next.addedScaleIds)!=JSON.stringify(this.props.addedScaleIds)){
      this.setState({
        addedScaleIds:[...next.addedScaleIds||[]]
      })
    }
  }
  handoutTypes(item,i){
    const {getIndexData,indexData,timeTypes,wholeIndexs,chronicPushItems,
      formulaResult,indexNames,disName} =this.props;
    let obj = [];
    Object.keys(wholeIndexs).map((i)=>{
      obj[i]=chronicPushItems[i];
    });
    const {chooseSelecteds,wholeAssessItems,wholeScaleItems,wholeAssessText,chartTimeTypes,wholeResults,calcuResult} = this.state;
    const chartData = indexData;
    const name = item.regionName+"：";
    const list = item.data&&item.data.rows;
    switch (+item.regionType){
      case 0:     //数据来源与右侧手动添加
        return <ScaleItem title={name}
                          data={obj}
                          handleRemove={this.handleScaleDel}
                          handleInp={this.handleScaleText}
                          text={wholeAssessText[disName]}
                          indexs={wholeAssessItems}
                          scaleItems ={wholeScaleItems}
                          result={wholeResults}
                          calcuResult={calcuResult}
                          formulaResult={formulaResult}></ScaleItem>;
      case 1:     //数据来源于大数据
        return <ChooseItem title={name}
                           data={list}
                           handleChange={this.handleChooseChange.bind(this,i)}
                           selecteds={chooseSelecteds[i]||{}} ></ChooseItem>;
      case 10:   //数据来源于后台
        return <ChartItem title={name}
                          data={chartData||{}}
                          names={indexNames||{}}
                          disName = {item.diseaseName}
                          timeTypes={JSON.stringify(chartTimeTypes)=='{}'?timeTypes&&timeTypes[i]:chartTimeTypes[i]}
                          pindex={i}
                          initFn={getIndexData}
                          handleChange={this.handleChartChange.bind(this,i)}></ChartItem>;
      default:
        return '';
    }
  }
  render() {
    const {disName,chronicMagItemName} =this.props;
    const data = this.props.data&&this.props.data[disName];
    return <div className={style['assess-cont']} id="AssistResult">
      <div className={style['printShow']} style={{borderBottom: (chronicMagItemName == disName)?'1px solid #ccc':''}}>
        <Information></Information>
        {
          chronicMagItemName == disName?<p>诊断：{chronicMagItemName}复诊评估</p>:null
        }
      </div>
      {!data?<p className={style['loading']}><img src={loadingIcon} alt='loading...'/></p>:''}
      {data&&data.length==0?<p className={style['no-data']}>暂无数据！</p>:''}
      {data && data.map((it, i) => {
        return this.handoutTypes(it, i);
      })}
    </div>;
  }
}

export default AssessResult;
