import React, { Component } from "react";
import style from "../index.less";
import { Radio,CheckBtn } from '@commonComp';
/****
 * 大数据推送出的类型
 * 要素：名称、选项、推荐选择
 *
 * ****/
class ChooseItem extends Component {
  constructor(props) {
    super(props);
    this.state = {
      saveData:null,
    };
  }

  handleRadio(name,value){
    const {handleChange,selecteds} = this.props;
    handleChange&&handleChange(Object.assign({},selecteds,{[name]:value}));
  }
  handleCheckbox(name,value){
    const {handleChange,selecteds} = this.props;
    selecteds[name]?selecteds[name]:selecteds[name]=[];
    const index = selecteds[name].findIndex((i)=>i==value);
    if(index!=-1){
      selecteds[name].splice(index,1);
    }else{
      selecteds[name].push(value);
    }
    handleChange&&handleChange(selecteds);
  }
  render() {
    const {title,data,selecteds,disabled} = this.props;
    return <div className={style['assess-item']}>
        <h2>{title}</h2>
        <div className={style['item-content']}>
          <ul>
            {data&&data.map((it)=>{
              return <li>
                <span>{it.name}：</span>
                <div className={style['row']}>
                  {it.details&&it.details.map((i)=> {
                    if(it.controltype ==0){
                      return <div className={style['choose-item']}>
                                <Radio label={i.detailName}
                                       value={i.detailName}
                                       disabled={disabled}
                                       isSelect={selecteds&&selecteds[it.name]==i.detailName}
                                       handleClick={this.handleRadio.bind(this,it.name,i.detailName)}></Radio>
                                {i.state?<i>（智能推荐）</i>:''}
                              </div>;
                    }else{
                      return <div className={style['choose-item']}>
                                <CheckBtn id={i.detailName}
                                          value={i.detailName}
                                          label={i.detailName}
                                          disabled={disabled}
                                          isSelect={selecteds&&selecteds[it.name]&&selecteds[it.name].includes(i.detailName)}
                                          handleClick={this.handleCheckbox.bind(this,it.name,i.detailName)}/>
                                {i.state?<i>（智能推荐）</i>:''}
                              </div>;
                    }
                  })}
                </div>
                {/*<div className={style["recommend"]}>
                  推荐选择：正常
                </div>*/}
              </li>
            })}
          </ul>
        </div>
      </div>;
  }
}

export default ChooseItem;