import { Location } from '@angular/common';
import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { filter, switchMap, tap } from 'rxjs/operators';
import { sortByProperty } from 'src/app/core/base/base-form';
import { BaseFormUid } from 'src/app/core/base/base-form-uid';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { RandomQuestionsService } from 'src/app/core/service/testing-information/random-questions.service';

@Component({
  selector: 'app-view',
  templateUrl: './view.component.html',
  styleUrls: ['./view.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ViewComponent extends BaseFormUid implements OnInit {
  data;
  dataTable;
  column = ['1','2','3','4','5','6']
  constructor(
    public fb: FormBuilder,
    public router: Router,
    public activeRoute: ActivatedRoute,
    public location: Location,
    private randomQuestionSV: RandomQuestionsService,
    private cdr: ChangeDetectorRef,
    private swSV: SweetalertService
  ) {
    super(router,activeRoute,fb,location)
   }

  ngOnInit(): void {
    this.randomQuestionSV.get(this.id).pipe(
      tap((x:any) => this.data = x),
      tap((x:any) => this.dataTable = x.random_question_details.sort(sortByProperty('row_order'))),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
  }

  cancel(){
    this.location.back()
  }

  onDelete(){
    this.swSV.confirm().pipe(
      filter(x => x.isConfirmed),
      switchMap(x => {
        return this.randomQuestionSV.deleteData(this.id).pipe(
          tap(x => this.swSV.removeSuccess()),
          tap(x => this.location.back()),
          tap(x => this.cdr.detectChanges())
        )
      })
    ).subscribe()
  }

}
