import { Component, OnInit, ViewChild } from '@angular/core';
import { LocalStorage } from '../../core/services/local_storage.service';
import { HyperService } from '../../core/services/http.service';
import { Router, ActivatedRoute } from '@angular/router';
import * as moment from 'moment';
import { AddparticipantpopComponent } from '../addparticipantpop/addparticipantpop.component';
import { ConfirmpopComponent } from '../confirmpop/confirmpop.component';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ToasterService } from '@service/toaster.service';
import { Location } from '@angular/common';
import {Subject, Observable} from 'rxjs';
import {WebcamImage, WebcamInitError, WebcamUtil} from 'ngx-webcam';

declare var $:any;

@Component({
  selector: 'app-alltrainingfeedback',
  templateUrl: './alltrainingfeedback.component.html',
  styleUrls: ['./alltrainingfeedback.component.scss']
})
export class AllTrainingFeedbackComponent implements OnInit {
  sub: any;
  loader: boolean;
  @ViewChild(AddparticipantpopComponent) participant_pop: AddparticipantpopComponent;
  @ViewChild(ConfirmpopComponent) confirm_pop: ConfirmpopComponent;
  schedule_data: any;
  role_num;
  assessment_id;
  feedback_datas: any = [];
  feedback_question: any = [];
  trainerfeedbackquestion: any=[];
  trainingfeedbackquestion: any = [];
  anslist: any = [];
  trainingover_score : any = 0;
  trainerover_score : any = 0;
  remark;
  feedback_status: boolean;
  type;
  constructor(private server: HyperService,private _location: Location, private router: Router, private route: ActivatedRoute,public toaster : ToasterService) {
    this.sub = this.route.params.subscribe(params => {
      this.assessment_id = (params['id']);
    });
  }

  ngOnInit() {
    this.role_num = LocalStorage.getValue('user_data').role;
    LocalStorage.setValue('is_firsttime', false);
    this.getdetails();
    let data = LocalStorage.getValue('training_Feedbackdata');
    this.schedule_data = JSON.parse(data);
    this.schedule_data = this.schedule_data[0];
    // this.feedback_status = this.schedule_data.is_completed == 1 ? true : false;
  }

  getdetails(){
    this.loader = true; 
    this.server.get("crtraining/trainingalluserfeedbackquestion/"+this.assessment_id).then((data) => {
      this.loader = false;
      let array1 =[];
      let array2 = [];
      if (data.status == 200 && data.result.response == "success") {
        this.feedback_datas = data.result.data;
        this.feedback_question = this.feedback_datas.questions;
        // for(let x=0; x<this.feedback_datas.length; x++){
        //   this.feedback_datas[x].trainingques = [];
        //   this.feedback_datas[x].trainerques = [];
        //   this.feedback_question = this.feedback_datas[x].questions;
        // }
        this.feedback_question.forEach((question)=>{
          if(question.questiontype == 1){
            this.trainingfeedbackquestion.push(question)
          }else{
            this.trainerfeedbackquestion.push(question)
          }
        });
        // for(let j=0;j<this.feedback_datas.length; j++){
        //   for(let i=0; i< this.feedback_question.length;i++){
        //     if(this.feedback_question[i].questiontype == 1){
        //       this.feedback_datas[j].trainingques.push(this.feedback_question[i])
        //     }else{
        //       this.feedback_datas[j].trainerques.push(this.feedback_question[i])
        //     }
        //   }
        // }
      } else if (data.result.response == "failed" && data.status == 401) {
        this.router.navigateByUrl('');
      }
    })
  }

  
  backclicked(){
    this._location.back();
  }

  oncancel(){
    this.router.navigateByUrl('trainer');
  }
}
