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} from '@utils/tools';

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

    this.state={
      chooseSelecteds:chooseSelecteds,     //大数据选择模块
      chartTimeTypes:{},      //图表模块
      wholeAssessItems:wholeIndexs,     //整体评估模块
      wholeAssessText:props.wholeAssessText||'',     //整体评估补充说明
      wholeResults:{
        possible:Object.assign({},props.possible),
        radioVal:Object.assign({},props.radioVal)
      },
    };
    this.handleChooseChange = this.handleChooseChange.bind(this);
    this.handleScaleDel = this.handleScaleDel.bind(this);
    this.handleScaleText = this.handleScaleText.bind(this);
    this.handleResult = this.handleResult.bind(this);
  }
  componentWillMount(){
    //获取评估
    this.props.getAssess(this.props.isRecommend);
  }
  handleScaleText(text){
      this.setState({
        wholeAssessText:text
      })
  }
  handleScaleDel(i,j){
    const items = Object.assign({},this.state.wholeAssessItems);
    const inx = items[i].findIndex(x=>x==j);
    items[i].splice(inx,1);
    this.setState({
      wholeAssessItems: items
    });
  }
  handleResult(opt){
    const {wholeResults} = this.state.wholeResults;
    const res = Object.assign({},wholeResults,opt);
    this.setState({
      wholeResults:res
    });
  }
  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 {handleSave,isAssessConfirm,clearChartData,handleSaveCalcu,isRecommend} = this.props;
    clearChartData&&clearChartData();
    if(!isRecommend&&isAssessConfirm && readyKeepHistory() == 1){
      Notify.error("主诉不能为空");
    }else if(!isRecommend&&isAssessConfirm && readyKeepHistory() == 2){
      Notify.info('诊断不能为空');
    }
    handleSaveCalcu&&handleSaveCalcu(this.state.wholeResults);
    isAssessConfirm&&handleSave(this.state,readyKeepHistory(),isRecommend);
  }
  handoutTypes(item,i){
    const {getIndexData,indexData,timeTypes,wholeIndexs,closeAssess,showScaleFn,calcuFormula,isRecommend,chronicPushItems,
      formulaResult,calcuResult} =this.props;
    let obj = [];
    Object.keys(wholeIndexs).map((i)=>{
      obj[i]=chronicPushItems[i];
    });
    const {chooseSelecteds,wholeAssessItems,wholeAssessText,chartTimeTypes,wholeResults} = 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}
                          handleRes={this.handleResult}
                          text={wholeAssessText}
                          indexs={wholeAssessItems}
                          result={wholeResults}
                          showScaleFn={showScaleFn}
                          calcuFormula={calcuFormula}
                          calcuResult={calcuResult}
                          formulaResult={formulaResult}
                          isRecommend={isRecommend}
                          chronicPushItems={chronicPushItems}
                          closeAssess={closeAssess}></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||{}}
                          timeTypes={JSON.stringify(chartTimeTypes)=='{}'?timeTypes&&timeTypes[i]:chartTimeTypes[i]}
                          pindex={i}
                          initFn={getIndexData}
                          handleChange={this.handleChartChange.bind(this,i)}></ChartItem>;
      default:
        return '';
    }
  }
  render() {
    //const {data} =this.props;
    let { isRecommend } = this.props;
    const wholeItems = [{regionName: "整体评估",regionType: "0"}];
    const data = isRecommend?wholeItems:this.props.data;
    return <div className={style['assess-cont']} id="AssistResult">
      <div className={style['printShow']}>
        <Information></Information>
      </div>
      {data && data.map((it, i) => {
        return this.handoutTypes(it, i);
      })}
    </div>;
  }
}

export default AssessResult;
