import React, { Component } from "react";
import style from "./index.less";
import { Radio} from '@commonComp';
import Notify from '@commonComp/Notify';
import ChooseItem from "./ChooseItem";
import ScaleItem from "./ScaleItem";
import ChartItem from "./ChartItem";

class AssessResult extends Component {
  constructor(props) {
    super(props);
    this.state={
      chooseSelecteds:{},     //大数据选择模块
      chartTimeTypes:{},      //图表模块
      wholeAssessItems:props.wholeIndexs,     //整体评估模块
      wholeAssessText:props.wholeText||''     //整体评估补充说明
    }
    this.handleChooseChange = this.handleChooseChange.bind(this);
    this.handleScaleDel = this.handleScaleDel.bind(this);
    this.handleScaleText = this.handleScaleText.bind(this);
  }
  componentWillMount(){
    //获取评估
    this.props.getAssess();
  }
  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
    });
  }
  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} = this.props;
    isAssessConfirm&&handleSave(this.state);
  }
  handoutTypes(item,i){
    const {getIndexData,indexData,timeTypes,wholeAssessData,wholeIndexs} =this.props;
    //console.log(indexData)
    const chartData = indexData;
    const name = item.regionName+"：";
    const list = item.data&&item.data.rows;
    switch (+item.regionType){
      case 0:     //数据来源与右侧手动添加
        return <ScaleItem title={name} data={wholeAssessData} handleRemove={this.handleScaleDel} handleInp={this.handleScaleText} indexs={this.state.wholeAssessItems}></ScaleItem>;
      case 1:     //数据来源于大数据
        return <ChooseItem title={name} data={list} handleChange={this.handleChooseChange.bind(this,i)}></ChooseItem>;
      case 10:    //数据来源于后台
        return <ChartItem title={name} data={chartData||{}} timeTypes={timeTypes} initFn={getIndexData} handleChange={this.handleChartChange.bind(this,i)}></ChartItem>;
      default:
        return '';
    }
  }
  render() {
    const { onClose,data } = this.props;
    return <div className={style['assess-cont']}>
      {data&&data.map((it,i)=>{
        return this.handoutTypes(it,i);
      })}
    </div>;
  }
}

export default AssessResult;
