import {Component, OnInit} from '@angular/core';
import {CommonService} from '../common.service';
import {Router, ActivatedRoute} from "@angular/router";
@Component({
  selector: 'app-job',
  templateUrl: './job.component.html',
  styleUrls: ['./job.component.css']
})
export class JobComponent implements OnInit {
  curPageData:any;
  pageAnswers: any;
  selectedOptionId: any;
  type: any;
  id: any;
  nextBtn: boolean;
  lastQuestions: Array<any>;
  targetObj: Object = {}

  constructor(private commonService: CommonService,
              private router: Router,
              private route: ActivatedRoute) {
    /**
     * 1:单身贵族
     * 2：二人世界
     * 3：独立带娃
     * 4：多口之家
     * @type {any}
     */
    /**
     * 1：您的工作
     * 2: 配偶的工作
     * @id
     *  */
    this.type = this.route.snapshot.queryParams['type'];
    this.id = this.route.snapshot['data']['0']['id'];
    this.pageAnswers = {
      pageId: '',
      questions: [{
        questionId: '',
        questionName: '',
        options: [{optionId: '', optionName: '', optionOrder: '', selected: ''}]
      }],
    };
  }

  ngOnInit() {
    this.nextBtn = false;
    this.commonService.surveyInfo().then(res => {
      if (res['success']) {
        if (this.id == 1) {
          this.curPageData = res['data'].survey.pages['3'];
          this.lastQuestions = this.commonService.todosCopy.filter(item => item.pageId == 4).pop();
          if (this.lastQuestions) {
            for (let i = 0; i < this.lastQuestions['questions'].length; i++) {
              for (let j = 0; j < this.curPageData['questions'].length; j++) {
                if (this.lastQuestions['questions'][i].questionId == this.curPageData['questions'][j].questionId) {
                  this.lastQuestions['questions'][i]['options'].forEach(option => {
                    if (Object.keys(option).indexOf('optionId') > -1) {
                      this.targetObj = option
                    }
                    this.selectedJob(this.targetObj);

                  });
                }
              }
            }
          } else {
            return;
          }
        } else {
          this.curPageData = res['data'].survey.pages['4'];
          this.lastQuestions = this.commonService.todosCopy.filter(item => item.pageId == 5).pop();
          if (this.lastQuestions) {
            for (let i = 0; i < this.lastQuestions['questions'].length; i++) {
              for (let j = 0; j < this.curPageData['questions'].length; j++) {
                if (this.lastQuestions['questions'][i].questionId == this.curPageData['questions'][j].questionId) {
                  this.lastQuestions['questions'][i]['options'].forEach(option => {
                    if (Object.keys(option).indexOf('optionId') > -1) {
                      this.targetObj = option;
                    }
                    this.selectedJob(this.targetObj);

                  });
                }
              }
            }
          } else {
            return;
          }
        }
      }
    });
  }

  selectedJob(option) {
    const options = this.curPageData['questions'][0]['options']
    for (let i = 0; i < options.length; i++) {
      if (option['optionId'] == options[i]['optionId']) {
        options[i]['selected'] = true;
      } else {
        options[i]['selected'] = false;
      }
    }
    this.pageAnswers = {
      pageId: this.curPageData['pageId'],
      questions: [{
        questionId: this.curPageData['questions']['0']['questionId'],
        questionName: this.curPageData['questions']['0']['questionName'],
        options: [option]
      }],
    };
    this.commonService.addAnswer(this.pageAnswers);
    if (this.pageAnswers['questions'].length > 0) {
      this.nextBtn = true;
    } else {
      this.nextBtn = false;
    }
  }

  //   下一步
  next() {
    // 先判断下一步能不能点
    // 如果是二人世界或是多口之家跳转到配偶页
    // 先判断当前页是您的页还是配偶页
    if (this.nextBtn == true) {
      if (this.id == 1) {
        if (this.type == 2 || this.type == 4) {
          this.router.navigate(['/spouse_job'], {queryParams: {type: this.type}})
        } else {
          this.router.navigate(['/transit1'], {queryParams: {type: this.type}})
        }
      } else {
        this.router.navigate(['/transit1'], {queryParams: {type: this.type}})
      }
    } else {
      return;
    }
  }

  //  上一步
  before() {
    this.commonService.before();
  }
}
