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++) {
      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++) {
      if (!this.allOptions[i]['selected']) {
        let index = this.allOptions.indexOf(this.allOptions[i])
        if (index > -1) {
          this.allOptions.splice(index, 1)
        }
      }
    }

    console.log(this.allOptions)

    // 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 ret = {
      pageId: this.curPageData['pageId'],
      questions: [
        {
          questionId: this.curPageData['questions'][0].questionId,
          options: this.allOptions
        }
      ]
    }
    this.getAllAnswer.emit(ret)
  }
}
