import {Component, OnInit, ChangeDetectionStrategy, Inject, ChangeDetectorRef} from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import {TestingCenterService} from '../../../../../core/service/TestingCenter/testing-center.service';
import {AcademicYearService} from '../../../../../core/service/settings/academic-year.service';
import {AcademicSemesterService} from '../../../../../core/service/settings/academic-semester.service';

@Component({
  selector: 'app-copy',
  templateUrl: './copy.component.html',
  styleUrls: ['./copy.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class CopyComponent implements OnInit {
  Olddata;
  copycat;
  AcademicYear = [];
  AcademicsemesterMock = [];
  query = {
    academic_year_uid: null,
    academic_semester_uid: null,
    testing_center_uid: null,
    testing_center_name_th: null,
    testing_room_name_th: null
  };
  constructor(
    private swSV: SweetalertService,
    private dialogRef: MatDialogRef<CopyComponent>,
    private cdr: ChangeDetectorRef,
    private testingCenter: TestingCenterService,
    private academicYearSV: AcademicYearService,
    private academicSemSV: AcademicSemesterService,
    @Inject(MAT_DIALOG_DATA) public data
  ) { }

  ngOnInit(): void {
    this.academicYearSV.getAll().subscribe(
      result => {
        this.AcademicYear = result;
        this.cdr.detectChanges();
      }
    );
    this.academicSemSV.getAll().subscribe(
      result => {
        this.AcademicsemesterMock = result;
        this.cdr.detectChanges();
      }
    );
  }

  cancel(){
    this.dialogRef.close();
  }

  save(){
    for (var index in this.data){
      console.log(this.data[index].testing_center_uid);
      this.checkdata(this.data[index].testing_center_uid);
    }
    this.swSV.saveSuccess();
    this.cancel()
  }

  checkdata(id: number){
    this.testingCenter.get(id).pipe(
      // tap(x => console.log(x)),
    ).
    subscribe(
      res => {
        const copy = JSON.parse(JSON.stringify(res));
        copy.testing_center_uid = null;
        copy.academic_year_uid = this.query.academic_year_uid;
        copy.academic_semester_uid = this.query.academic_semester_uid;
        console.log(copy);
        const picked = (({
                           status_id,
                           branch_uid,
                           testing_room_name_th,
                           testing_room_name_en,
                           testing_center_uid,
                           testing_field_uid,
                           academic_year_uid,
                           academic_semester_uid,
                           testing_center_periods,
                           testing_center_rows,
                          }) => ({
                            status_id,
                            branch_uid,
                            testing_room_name_th,
                            testing_room_name_en,
                            testing_center_uid,
                            testing_field_uid,
                            academic_year_uid,
                            academic_semester_uid,
                            testing_center_periods,
                            testing_center_rows,
                          }))(copy);
        // console.log(picked.testing_center_periods);
        // const copyperiods = picked.testing_center_periods;
        for (var index in picked.testing_center_periods){
          console.log(picked.testing_center_periods[index]);
          picked.testing_center_periods[index] = (({
                                                     status_id,
                                                     testing_center_period_uid,
                                                      period_id,
                                                      start_time,
                                            }) => ({
                                              status_id,
                                              testing_center_period_uid,
                                              period_id,
                                              start_time,
          }))( picked.testing_center_periods[index]);
        }
        for (var index in picked.testing_center_rows){
          console.log(picked.testing_center_rows[index]);
          picked.testing_center_rows[index] = (({
                                                  status_id,
                                                  testing_center_row_uid,
                                                  row_name,
                                                  no_of_seats,
                                                   }) => ({
            status_id,
            testing_center_row_uid,
            row_name,
            no_of_seats,
          }))( picked.testing_center_rows[index]);
        }
        console.log(picked);
        this.testingCenter.add(picked).pipe(
          // tap(x => this.swSV.saveSuccess()),
          // tap(x => window.history.back()),
          catchError(err => {
            this.swSV.err();
            return throwError(err);
          })
        ).subscribe();
      }
    );
  }

}
