import React, { Component } from "react";
import styles from "./index.less";
import { normalVal,timestampToTime } from '@utils/tools';
import { InspectCommon, Calendar,DelToast} from '@commonComp';
import slideUp from "@common/images/slide-up.png";
import slideDown from "@common/images/slide-down.png";
import date1 from '../img/date1.png';
import close from '../img/close.png';

class SlideSelect extends Component {
  constructor(props) {
    super(props);
    this.state = {
      show:false,
      activeInd:false,
      activeName:''
    };
    this.toTime = this.toTime.bind(this)
    this.handleSlide = this.handleSlide.bind(this)
    this.handleDel = this.handleDel.bind(this)
    this.handleCancel = this.handleCancel.bind(this)
    this.delConfirm = this.delConfirm.bind(this)
  }
  toTime(time){
    let tmpTim = time.split(',').join('')-0
    if(time && tmpTim.toString() != 'NaN'){
      let date = new Date('1900-01-01');
      let dateTim = date.getTime();
      let times = (tmpTim-2)*24*60*60*1000;
      let result = timestampToTime(dateTim+times).split(' ')[0]
      return result;
    }else{
      return time;
    }
  }
  handleSlide(){
    let tmpShow = this.state.show
    this.setState({
      show:!tmpShow
    })
  }
  handleDel(){
    const {handleDelClick,idx,item} = this.props;
    this.setState({
      activeInd:true,
      activeName:item.name
    })
    handleDelClick&&handleDelClick(1,idx);
  }
  handleCancel(){
    this.setState({
      activeInd:false,
      activeName:''
    })
  }
  delConfirm(){
    const {handleDelConfirm} = this.props;
    handleDelConfirm&&handleDelConfirm();
    this.setState({
      activeInd:false,
      activeName:''
    })
  }
  render() {
    const {getInfomation,handleConfirm,changeActivePart,handleDelClick,getItemList,date,item,idx,handleFillShow,showDetails,handleLabelSub,showFill,changeShowFill,handlePush,dateTime,currentIdx,currentData,fillActive,handleShowDate,handleChangeDate,showToast} = this.props;
    const {show,activeInd,activeName} = this.state;
    let numPlus = 0,numPlus1 = 0;
    return (
      <li key={item.questionId} className={styles.slideLi}> 
        {
            // 标签，血常规。。
            item.show ?
            <p className={styles.staticTagActive}>
                <span className={styles.tagSpan} data-flg="current" style={{color:"#000"}} onClick={(e) => { handleLabelSub(e, item.questionId,idx); handleFillShow(e,idx) }}>
                  {item.name}
                  <span className={styles.imgInfo} onClick={()=>getInfomation({name:item.uniqueName || '', position: 1, type: 12})}></span>
                </span>
            </p>:
            <p >
                <i className={styles.tagSpan} data-flg="current" onClick={(e) => { handleLabelSub(e,item.questionId,idx); handleFillShow(e,idx) }}>
                  {item.name}
                  <span className={styles.imgInfo} onClick={()=>getInfomation({name:item.uniqueName || '', position: 1, type: 12})}></span>
                </i>
            </p>
        }
        
        {
          item.details && item.details.map((val)=>{
            if(val.value && val.value != ''){
              numPlus = ++numPlus
            }
          })
        }
        {
            item.show ?
                <table className={styles.table}>
                    {
                        show?(item.details.map((val)=>{
                            if(val.value && val.value != ''){
                                return <tr>
                                    <td style={{width:'30%'}}>
                                      <span className={styles.tagSpan}>
                                        {val.name}
                                        <span className={styles.imgInfo} onClick={()=>getInfomation({name:val.uniqueName || '', position: 1, type: 12})}></span>                                        
                                      </span>
                                    </td>
                                    {showDetails(val)}
                                    <td style={{width:'25%'}}>
                                        {normalVal(val.minValue,val.maxValue)}
                                    </td>
                                    <td style={{width:'25%'}}>{'化验时间:'+item.time}</td>
                                </tr>
                            }
                        })):(item.details.map((val,num)=>{
                          if(val.value && val.value != '' ){
                              ++numPlus1;
                              if(numPlus1 < 5){
                                return <tr>
                                    <td style={{width:'30%'}}>
                                      <span className={styles.tagSpan}>
                                        {val.name}
                                        <span className={styles.imgInfo} onClick={()=>getInfomation({name:val.uniqueName || '', position: 1, type: 12})}></span>
                                      </span>
                                    </td>
                                    {showDetails(val)}
                                    <td style={{width:'25%'}}>
                                        {normalVal(val.minValue,val.maxValue)}
                                    </td>
                                    <td style={{width:'25%'}}>{'化验时间:'+item.time}</td>
                                </tr>
                              }
                          }
                      }))
                    }
                </table>:null
        }
        {
          numPlus > 4 ?  
          <div className={styles.slides} onClick={this.handleSlide}>
              {
                show ? <span>收起</span> :<span>剩余<span className={styles.num}>{numPlus-4}</span>项</span>
              }
              <img src={show ?slideUp:slideDown} alt=""/>
          </div>:null
        }
        <div className={styles.searchResult}>
            {
                showFill && fillActive.id == item.id && idx == currentIdx ?
                    <InspectCommon
                        showFill={showFill}
                        handleClear={(e)=>{
                            changeActivePart('','',true)
                        }}
                        handleConfirm={(e)=>{
                            if(JSON.stringify(currentData) == '{}'){
                                handleConfirm(e,idx,dateTime,fillActive);
                            }else{
                                handleConfirm(e,idx,dateTime,currentData);
                            }
                            changeShowFill()
                            handlePush&&handlePush();     //右侧推送
                        }}
                    >
                    
                        <div className={styles.searchResultT}>
                            <img style={{"position":"absolute","top":"8px","right":"8px",cursor:"pointer"}} src={date1} alt="" onClick={handleShowDate}/>
                            <p style={{position:"absolute",right:"25px",top:"4px",lineHeight:"28px",cursor:"pointer",paddingRight:"10px"}} onClick={handleShowDate}>
                                {
                                  dateTime
                                }
                            </p>
                            <div style={{display:date?"block":"none",position:"relative"}}>
                                {/* 日期组件 */}
                                <Calendar isShow={true} handleChange={handleChangeDate}></Calendar>
                            </div>
                        </div>
                            {/* 填写单内容显示 */}
                            { getItemList() }
                    </InspectCommon>
                : null
            }
        </div>
        {/*<img className={styles.partDel} src={close} alt="删除项" onClick={()=>{handleDelClick(1,idx)}}/>*/}
        <span id="addClose" className={styles.partDel} onClick={this.handleDel}></span>
        <DelToast show={showToast&&activeInd?true:false}
                    name={activeName} 
                    cancel={this.handleCancel}
                    confirm={this.delConfirm}/>
    </li>
    );
  }
}
export default SlideSelect;
