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)
  }
  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;
              }
            }
    }

    console.log(this.allOptions)
    //  删掉未选中的疾病
    // for (let i = 0; i < this.allOptions.length; i++){
    // if (!this.allOptions[i]['selected']) {
    //   let index = this.allOptions.indexOf(this.allOptions[i])
    //   if (index > -1) {
    //     this.allOptions.splice(index, 1)
    //   }
    // }
    // }
    // if (!this.allOption[options['optionId']]) {
    //   this.allOption[options['optionId']] = {}
    // }
    // console.log(this.allOption)
    // this.allOption[options['optionId']] = {
    //   selected: !this.allOption[options['optionId']]['selected'],
    //   options
    // }
    // const options_ret = Object.keys(this.allOption)
    //   .filter(item => {
    //     return this.allOption[item]['selected']
    //   })
    //   .map(questionId => {
    //     return {
    //       options: this.allOption[questionId]
    //     }
    //   })
    
  //   const result = this.allOptions.filter(item=>{
  //     item.selected==true
  // })
  // console.log(result)
    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;
    })
  }
}
