import { formatDate } from '@angular/common';
import { Component, OnInit, ChangeDetectionStrategy, Input, ChangeDetectorRef } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { throwError } from 'rxjs';
import { catchError, concatMap, filter, switchMap, tap } from 'rxjs/operators';
import { BaseSubList } from 'src/app/core/base/base-sub-list';
import { GenerateGroupService } from 'src/app/core/service/student/generate-group.service';
import { ResetSeatService } from 'src/app/core/service/student/reset-seat.service';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { TestingInformationService } from 'src/app/core/service/testing-information/testing-information.service';
import { createQueryStringFromObject } from 'src/app/shared/util/func';

@Component({
  selector: 'app-view',
  templateUrl: './view.component.html',
  styleUrls: ['./view.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ViewComponent extends BaseSubList implements OnInit {
  testingCenterList: any = [];
  testingInfoData;
  query = {
    section: null,
    start_testing_date: null,
    end_testing_date: null,
    testing_information_uid: null
  }
  constructor(
    public activeRoute: ActivatedRoute,
    private studentGenerateGroupSV: GenerateGroupService,
    private router: Router,
    private cdr: ChangeDetectorRef,
    private testingInformationSV: TestingInformationService,
    private swSV: SweetalertService,
    private resetSeatSV: ResetSeatService
  ) {
    super(activeRoute)
   }

  ngOnInit(): void {
    this.studentGenerateGroupSV.search(`?testing_information_uid=${this.id}`).pipe(
      tap(x => this.testingCenterList = this.updateMatTable(x)),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
    this.testingInformationSV.get(this.id).pipe(
      tap(x => this.testingInfoData = x),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
  }

  edit(x){
    this.router.navigate(['create-exam-seat/seat',x.testing_information_uid,x.branch_uid,x.testing_field_uid,x.testing_center_uid,x.testing_date,x.section,x.total_seat_generate,x.total_student])
  }

  search(){
    this.query.testing_information_uid = this.id
    if(this.query.start_testing_date) this.query.start_testing_date = formatDate(this.query.start_testing_date, 'yyyy-MM-dd', 'en-US');
    if(this.query.end_testing_date) this.query.end_testing_date = formatDate(this.query.end_testing_date, 'yyyy-MM-dd', 'en-US');
    console.log(this.query)
    const query = createQueryStringFromObject(this.query)
    this.studentGenerateGroupSV.search(`?${query}`).pipe(
      tap(x => this.testingCenterList.data = x),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
  }

  clear(){
    this.studentGenerateGroupSV.search(`?testing_information_uid=${this.id}`).pipe(
      tap(x => this.testingCenterList.data = x),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
  }

  clearData(data){
    this.swSV.confirm('ต้องการจะล้างข้อมูลนี้ไหม').pipe(
      filter(x => x.isConfirmed),
      switchMap(x => {
        return this.resetSeatSV.reset(data).pipe(
          concatMap(x => this.studentGenerateGroupSV.search(`?testing_information_uid=${this.id}`)),
          tap(x => this.testingCenterList.data = x),
          tap(x => this.swSV.saveSuccess('ล้างข้อมูลสำเร็จ')),
          tap(x => this.cdr.detectChanges()),
          catchError(err => {
            this.swSV.errText(err.error)
            return throwError(err)
          })
        )
      })
    ).subscribe()
  }

}
