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

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

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