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

@Component({
  selector: 'app-page11',
  templateUrl: './page11.component.html',
  styleUrls: ['./page11.component.css']
})
export class Page11Component implements OnInit {
  @Input() curPageData: Array<any>
  @Output() getAllAnswer = new EventEmitter<any>()
  // allOption: object
  allOptions: Array<any>
  constructor() {}

  ngOnInit() {
    // this.allOption = {}
    this.allOptions = []
    // console.log(this.curPageData)
    this.getAlloptions()
  }
  getAnswer(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']==67 && 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']==67){
                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;
    })
  }

  getAlloptions(){
    const questions = this.curPageData['questions']
    if(!questions){
      return
    }
    questions[0]['options'].forEach(option => {
      if(option['selected']){
        this.allOptions.push(option);
        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)
      }
    });
  }
}
