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

@Component({
  selector: 'app-page7',
  templateUrl: './page7.component.html',
  styleUrls: ['./page7.component.css']
})
export class Page7Component implements OnInit {
  @Input() curPageData: Array<any>
  @Input() hasMate: boolean
  @Output() getAllAnswer = new EventEmitter<any>()
  curAllQues: Object
  selectedObj: Object
  selectedObj1:Object
  constructor() {}

  ngOnInit() {
    this.curAllQues = {}
    this.selectedObj = {}
    this.selectedObj1 = {}
    this.setOptionSelected()
  }
  getAnswer(question, options) {
    console.log(question,options)
    //获取当前页面问题id
    const questionId = question['questionId']
    if (!this.selectedObj[questionId]) {
      this.selectedObj[questionId] = options
      options['selected'] = true
    } else {
      if (this.selectedObj[questionId].optionId != options.optionId) {
        this.selectedObj[questionId]['selected'] = false
        options['selected'] = true
        this.selectedObj[questionId] = options
      }
    }
    this.curAllQues[questionId] = options
    const questions = Object.keys(this.curAllQues).map(questionId => {
      return { questionId, options: [this.curAllQues[questionId]] }
    })
    const ret = {
      pageId: 7,
      questions
    }
    this.getAllAnswer.emit(ret)
  }

  setOptionSelected() {
    const questions = this.curPageData['questions']
    // console.log(questions)
    if (!questions) {
      return
    }
    questions.map(question => {
      question['options'].forEach(option => {
        if (option['selected']) {
          // console.log(this.selectedObj)
          this.selectedObj[question['questionId']] = option
        }
      })
    })
  }
}
