import { Component, OnInit ,Input, Output, EventEmitter} from '@angular/core';

@Component({
  selector: 'app-page14',
  templateUrl: './page14.component.html',
  styleUrls: ['./page14.component.css']
})
export class Page14Component implements OnInit {
  @Input() curPageData: Array<any>
  @Output() getAllAnswer = new EventEmitter<any>()
  selectedObj:Object;
  allOptions: Array<any>
  optionType:number
  constructor() { }

  ngOnInit() {
    // console.log(this.curPageData)
    //拿到数据中是多选还是单选2表示多选1表示单选
    this.optionType = this.curPageData['questions'][0].optionType
    // console.log(this.optionType)
    if(this.optionType==1){
      this.selectedObj = {}
      this.setOptionSelected()
    }
    if(this.optionType==2){
      this.allOptions = []
    }

  }

  getManyAnswer(options) {
    options['selected'] = !options['selected']
    for (let i = 0; i < this.allOptions.length; i++) {
      // 更改问题状态，防止重复往数组push
      if (options['optionId'] == this.allOptions[i]['optionId']) {
          let index = this.allOptions.indexOf(this.allOptions[i])
          if (index > -1) {
            this.allOptions.splice(index, 1)
          } 
      }
    }
    this.allOptions.push(options)
    for (let i = 0; i < this.allOptions.length; i++) {
      let index;
            // 当选中无疾病时
            if(options['optionId']==111 && options['selected']==true){
              index = this.allOptions.indexOf(options);
              this.allOptions[i]['selected'] = false;
              if(index > -1){
                this.allOptions[index]['selected'] = true;
              }
            }else{
              // 选中其他疾病时，无疾病状态改为false
              if(this.allOptions[i]['optionId']==111){
                this.allOptions[i]['selected'] = false;
              }
            }
    }
    const ret = {
      // ...this.curPageData,
      pageId: this.curPageData['pageId'],
      questions: [
        {
          ...this.curPageData['questions'][0],
          questionId: this.curPageData['questions'][0].questionId,
          options: this.filterItems()
        }
      ]
    }
    this.getAllAnswer.emit(ret)
  }

  filterItems() {
    return this.allOptions.filter(item=> {
        return item['selected']==true;
    })
  }

  //单选调用的方法
  getAnswer(options) {
    if(!this.selectedObj['selected']){
      this.selectedObj = options
      options['selected'] = true;
    }else{
      if(this.selectedObj['optionId']!=options.optionId){
        this.selectedObj['selected'] = false;
        options['selected'] = true;
        this.selectedObj = options
      }
    }
    const ret = {
      pageId: this.curPageData['pageId'],
      questions: [
        {
          questionId: this.curPageData['questions'][0].questionId,
          options: [options]
        }
      ]
    }
    this.getAllAnswer.emit(ret)
  }

  setOptionSelected() {
    const options = this.curPageData['questions'][0].options
    if (!options) {
      return
    }
     options.forEach(option => {
        if (option['selected']) {
          this.selectedObj = option
        }
      })
  }

  //判断用哪个方法
  checkFun(options){
    if(this.optionType==1){
      this.getAnswer(options)
    }
    if(this.optionType==2){
      this.getManyAnswer(options)
    }
  }
  
}
