import React, { Component } from "react";
import style from "./index.less";
import { Radio} from '@commonComp';
import arrow from '@common/images/icon_tri_blue.png';
import loadingIcon from '@common/images/loading.gif';
import ChooseItem from "../ChooseItem";
import ScaleItem from "../ScaleItem";
import ChartItem from "../ChartItem";

class AssessResultHis extends Component {
  constructor(props) {
    super(props);
    this.state={
      chartTimeTypes:{},      //图表模块
    };
    this.showAssessFn = this.showAssessFn.bind(this);
    this.getAssessContent = this.getAssessContent.bind(this);
  }
  handleChartChange(i,selects){
    const {chartTimeTypes} = this.state;
    this.setState({
      chartTimeTypes:Object.assign(chartTimeTypes,{[i]:selects})
    });
  }
  showAssessFn(){
    //获取历史评估数据
    this.props.getAssessHis(this.props.inquiryId);
  }
  handoutTypes(obj,item,i){
    const {wholeAssess,wholeIndexs,chooseSelecteds} =obj;
    const {getIndexData,inquiryDate,indexData,inquiryId,timeTypes} = this.props;
    const {chartTimeTypes} = this.state;
    const name = item.regionName+"：";
    const list = item.data&&item.data.rows;
    switch (+item.regionType){
      case 0:     //数据来源与右侧手动添加
        return <ScaleItem title={name}
                          data={wholeAssess}
                          indexs={wholeIndexs}
                          disabled={true}
                          isRecommend={item.isRecommend}></ScaleItem>;
      case 1:     //数据来源于大数据
        return <ChooseItem title={name} data={list} selecteds={chooseSelecteds&&chooseSelecteds[i]} disabled={true}></ChooseItem>;
      case 10:    //数据来源于后台
        return <ChartItem title={name}
                          data={indexData||{}}
                          timeTypes={JSON.stringify(chartTimeTypes||{})=='{}'?timeTypes&&timeTypes[i]:chartTimeTypes[i]}
                          initFn={getIndexData}
                          endDate={inquiryDate}
                          pindex={i}
                          handleChange={this.handleChartChange.bind(this,i)}></ChartItem>;
      default:
        return '';
    }
  }
  getAssessContent(){
    const { list,inquiryId } = this.props;
    const obj = list&&list[inquiryId];
    const data = obj&&obj.data;
    if(data&&data.length>0){
      return data.map((it,i)=>{
        return this.handoutTypes(obj,it,i);
      });
    }else{
      return data===null||(data&&data.length==0)?<p className={style['no-data']}>没有历史评估记录！</p>:'';
    }
  }
  /*componentDidMount(){
    const indexTimeTypes = JSON.parse(JSON.stringify(this.props.indexTimeTypes));
    console.log(indexTimeTypes)
    this.setState({
      chartTimeTypes:indexTimeTypes
    });
  }*/
  render() {
    const { list,inquiryId,loading } = this.props;
    const data = list&&list[inquiryId]&&list[inquiryId].data;
    return <div className={style['assess-cont']}>
      <div className={style['assess-result']}>
        <p className={style['enter']}>管理和评估结果：<a onClick={this.showAssessFn}>查看结果<img src={arrow} /></a></p>
      </div>
      <div className={style['assess-box']}>
        {loading?<p className={style['loading']}><img src={loadingIcon} alt='loading...'/></p>:''}
        {/*{data?data.map((it,i)=>{
          return this.handoutTypes(it,i);
        }):data===null?<p className={style['no-data']}>没有历史评估记录！</p>:''}*/}
        {this.getAssessContent()}
      </div>
    </div>;
  }
}

export default AssessResultHis;
