import { Component, OnInit } from '@angular/core';
import { LocalStorage } from '../../core/services/local_storage.service';
import { ActivatedRoute } from '@angular/router';
import { Router } from "@angular/router";
import * as moment from 'moment';
import { HyperService } from '../../core/services/http.service';
import { Location } from '@angular/common';


@Component({
  selector: 'app-feedback-survey',
  templateUrl: './feedback-survey.component.html',
  styleUrls: ['./feedback-survey.component.scss']
})
export class FeedbackSurveyComponent implements OnInit {
  feedbacksurvey: any;
  sub: any;
  assessment_id: number;
  isloaded: boolean = false;
  ans_arr: any = [];
  start_time: any;
  end_time: any;
  current_feedback: any;
  feedback_is_attened: number;
  isWait: boolean = false;
  training_name: string = '';
  is_selected: any;
  check_mandatory: any;
  ans_chekcmandatory: any = [];
  data_loaded: boolean = false;
  feedback_id: any;
  sales_date: any;
  user_dashboard: any;
  percentage: any;
  loader:boolean;


  constructor(private route: ActivatedRoute, private router: Router, private _location: Location, private server: HyperService) {
      this.current_feedback = LocalStorage.getValue('current_feedback')
      this.feedback_is_attened = LocalStorage.getValue('feedback_is_attened');
      this.sub = this.route.params.subscribe(params => {
          this.feedback_id = params['id'];
          if (this.feedback_is_attened == 1) {
              this.getReview(this.current_feedback.result_id);
          } else {
              this.getFeedbackSurvey();
          }
      });
      let today = Date.now();
      this.start_time = moment(today).format('YYYY-MM-DD hh:mm:ss');
  }

  ngOnInit() {}


  getFeedbackSurvey() {
    this.loader=true;
      this.server.get("survey/question/" + this.feedback_id)
          .then((data) => {
              if (data.status == 200 && data.result.response != 'failed' && data.result.data != null) {
                  this.feedbacksurvey = data.result.data;
                  for (let i = 0; i < this.feedbacksurvey.length; i++) {
                      console.log(this.feedbacksurvey[i].questiontypeid)
                      let data = {
                          "aqid": this.feedbacksurvey[i].aqid,
                          "qid": this.feedbacksurvey[i].qid,
                          "survey_text": [],
                          "survey_rating": [],
                          "options": []

                      }
                      this.ans_arr.push(data);
                  }
                  this.loader=false;
                  this.data_loaded = true;
                  console.log('initarray', this.ans_arr)
                  this.isloaded = true;
                  LocalStorage.setValue('feedback', this.feedbacksurvey);
              } else if (data.result.status == 401 && data.result.response == 'failed') {
                  this.router.navigateByUrl('');
              } else {
                  this.feedbacksurvey = [];
              }
              console.log(this.feedbacksurvey)
          });
  }


  getReview(id) {
      this.data_loaded = false;
      this.server.get("survey/review/" + id)
          .then((data) => {
              if (data.status == 200 && data.result.response != 'failed' && data.result.data != null) {
                  this.feedbacksurvey = data.result.data;
                  console.log(this.feedbacksurvey)
                  for (let i = 0; i < this.feedbacksurvey.length; i++) {
                      if (this.feedbacksurvey[i].questiontypeid == 3) {
                          console.log(this.feedbacksurvey[i].rating_options)
                          this.is_selected = this.feedbacksurvey[i].rating_options.filter(item => item.selected == 1);
                          console.log('Selcerdeded', this.is_selected)
                          let data = {
                              "aqid": this.feedbacksurvey[i].aqid,
                              "qid": this.feedbacksurvey[i].qid,
                              "survey_text": [],
                              "survey_rating": (this.is_selected[0].id == undefined) ? '' : this.is_selected[0].id

                          }
                          this.ans_arr.push(data);
                          console.log(this.ans_arr)
                      } else if (this.feedbacksurvey[i].questiontypeid == 5||this.feedbacksurvey[i].questiontypeid ==6) {
                          console.log(this.feedbacksurvey[i].options)
                          this.is_selected = this.feedbacksurvey[i].options.filter(item => item.selected == 1);
                          console.log('Selcerdeded', this.is_selected)
                          let data = {
                              "aqid": this.feedbacksurvey[i].aqid,
                              "qid": this.feedbacksurvey[i].qid,
                              "options": (this.is_selected[0].optid == undefined) ? '' : this.is_selected[0].optid

                          }
                          this.ans_arr.push(data);
                          console.log(this.ans_arr)
                      } else {
                          let data = {
                              "aqid": this.feedbacksurvey[i].aqid,
                              "qid": this.feedbacksurvey[i].qid,
                              "survey_text": [this.feedbacksurvey[i].survey_text],
                              "survey_rating": []
                          }
                          this.ans_arr.push(data);
                      }
                      console.log('Final',this.ans_arr)
                  }
                  this.data_loaded = true;
                  this.isloaded = true;
                  LocalStorage.setValue('feedback', this.feedbacksurvey);
              } else if (data.result.status == 401 && data.result.response == 'failed') {
                  this.router.navigateByUrl('');
              } else {
                  this.feedbacksurvey = [];
              }
          });
  }


  selectRadio(selected_ans, list, qus_index, option_index) {
      console.log(selected_ans, qus_index, option_index) 
      
      /////////////////////////////////Question Type 3 RADIO BUTTON////////////////////////
      if (list.questiontypeid == 3) {
          //Not answered
          if (this.ans_arr[qus_index].survey_rating == null || this.ans_arr[qus_index].survey_rating == '' || this.ans_arr[qus_index].survey_rating.length == 0) {
              this.ans_arr[qus_index].survey_rating = selected_ans.id

          }
          //Already answerd
          else {
              // this.ans_arr[qus_index].survey_rating.splice(0, 1);
              this.ans_arr[qus_index].survey_rating = selected_ans.id
          }
          console.log(this.ans_arr)

      ////////////////////////Question Type 5 RADIO BUTTON SINGLE/////////////////////////
      } else if (list.questiontypeid == 5) {
          //Not answered
          if (this.ans_arr[qus_index].options == null || this.ans_arr[qus_index].options == '' || this.ans_arr[qus_index].options.length == 0) {
              this.ans_arr[qus_index].options.push(selected_ans.optid) 
          }
          //Already answerd
          else {
              this.ans_arr[qus_index].options.splice(0, 1);
              this.ans_arr[qus_index].options.push(selected_ans.optid)
          }
          console.log(this.ans_arr)
      }

      ////////////////////////Question Type 6 CHECKBOX////////////////////////////////////
      else if (list.questiontypeid == 6) {
          //Not answered
          if (this.ans_arr[qus_index].options.indexOf(selected_ans.optid) > -1) {
              this.ans_arr[qus_index].options.splice(0, 1);
              this.feedbacksurvey[qus_index].is_ans = 0;
              // this.ans_arr[qus_index].options = selected_ans.optid
          }
          //Already answerd
          else {
              this.ans_arr[qus_index].options.push(selected_ans.optid)
              this.feedbacksurvey[qus_index].is_ans = 1;
          }
      }

      ////////////////////////Question Type 4 RADIO SINGLE////////////////////////////////
      if (list.questiontypeid == 4) {
          //Not answered
          if (this.ans_arr[qus_index].survey_text == null || this.ans_arr[qus_index].survey_text == '' || this.ans_arr[qus_index].survey_text.length == 0) {
              this.ans_arr[qus_index].survey_text = this.ans_arr[qus_index].survey_text;
          }
          //Already answerd
          else {
              this.ans_arr[qus_index].survey_text = this.ans_arr[qus_index].survey_text;
          }

      }

  }


  onSubmit() {
      console.log('myanswer', this.ans_arr)
      this.ans_chekcmandatory = this.ans_arr
      this.check_mandatory = this.ans_chekcmandatory.filter(question => question.survey_text == '' && question.survey_rating == '' && question.options == '');
      console.log('myanswer2', this.check_mandatory)
      if (this.check_mandatory.length > 0 || this.check_mandatory != '') {
          alert('Please answer all the questions')
      } else {
          this.isWait = true;
          let end = Date.now();
          this.end_time = moment(end).format('YYYY-MM-DD hh:mm:ss');
          this.server.post("survey/" + this.feedback_id, {
                  ans_arr: JSON.stringify(this.ans_arr),
                  startdatetime: this.start_time,
                  enddatetime: this.end_time
              })
              .then((data) => {
                  this.isWait = false;
                  if (data.status == 200 && data.result.response == "success") {
                      alert('Success')
                      this.feedback_is_attened = 1;
                      this.getReview(data.result.data.result_id);
                  } else if (data.result.response == "failed") {} else if (data.status == 500) {} else {
                      alert("Please check your internet connection");
                  }
              })
      }
  }

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

}