import {Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef, ViewChild} from '@angular/core';
import { async } from '@angular/core/testing';
import {FormBuilder, FormGroup} from '@angular/forms';
import {ActivatedRoute, Router} from '@angular/router';
import {tap} from 'rxjs/operators';
import {TestingInformationService} from '../../../../../core/service/testing-information/testing-information.service';
import {MatTableDataSource} from '@angular/material/table';
import {SubjectsService} from '../../../../../core/service/settings/subjects.service';
import {TestingCenterService} from '../../../../../core/service/TestingCenter/testing-center.service';
import {TestingFieldService} from '../../../../../core/service/settings/testing-field.service';
import {BranchService} from '../../../../../core/service/settings/branch.service';
import {MatDialog} from '@angular/material/dialog';
import {formatDate, Location} from '@angular/common';
import {SweetalertService} from '../../../../../core/service/sweetalert/sweetalert';
import {EducationTypesService} from '../../../../../core/service/settings/education-types.service';
import {AcademicYearService} from '../../../../../core/service/settings/academic-year.service';
import {AcademicSemesterService} from '../../../../../core/service/settings/academic-semester.service';
import {CalendarTypeService} from '../../../../../core/service/settings/calendar-type.service';
import {MatSort} from '@angular/material/sort';
import {MatPaginator} from '@angular/material/paginator';
import {Observable} from 'rxjs';

@Component({
  selector: 'app-set-exam-information-view',
  templateUrl: './set-exam-information-view.component.html',
  styleUrls: ['./set-exam-information-view.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SetExamInformationViewComponent implements OnInit {
  data;
  TestinforUid;
  EducationTypeLists;
  eductionTypeList;
  academicYearList;
  calendarLists;
  academicSemList;
  saveUsername = true;
  subjectList;
  subjectList2;
  subjectList3;
  isDisable = true;
  selectCalendar;
  dataSource5;
  dataSource4;
  dataSource3;
  dataSource2;
  dataSource;
  setexamform: FormGroup;
  fixSubjectlist = false;
  Today;
  candidateTypes =
    [
      {
        candidate_type_uid: '6dff8faf-0b30-4fb1-8274-42df8debe8a0',
        candidate_type_name_th: 'นักศึกษา',
      },
      {
        candidate_type_id: 'd056ba6d-5a15-4df6-ade0-c9e8c239d1b1',
        candidate_type_name_th: 'บุคคลทั่วไป',
      },
    ];
  sectionsFake = [
    {
      "status_id": 1,
      "testing_center_period_uid": null,
      "period_id": 1,
      "is_check": true,
      "start_time": "09:00"
    },
    {
      "status_id": 1,
      "testing_center_period_uid": null,
      "period_id": 2,
      "is_check": true,
      "start_time": "12:00"
    },
    {
      "status_id": 1,
      "testing_center_period_uid": null,
      "period_id": 3,
      "is_check": true,
      "start_time": "15:00"
    },
    {
      "status_id": 1,
      "testing_center_period_uid": null,
      "period_id": 4,
      "is_check": true,
      "start_time": "18:00"
    }
  ];
  public model = [
    // new Date('2/8/2022'),
    // new Date('2/9/2022'),
    // new Date('2/11/2022'),
  ];
  @ViewChild(MatSort, { static: true }) sort: MatSort;
  @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  constructor(
    public _router: Router,
    public _activeRouter: ActivatedRoute,
    private TestInformationSV: TestingInformationService,
    private subjectSV: SubjectsService,
    private testcenterSV: TestingCenterService,
    private testFiledSV: TestingFieldService,
    private branchSV: BranchService,
    private testingFieldSV: TestingFieldService,
    private formBuilder: FormBuilder,
    private dialog: MatDialog,
    public router: Router,
    public activeRoute: ActivatedRoute,
    public location: Location,
    private swSV: SweetalertService,
    private cdRef: ChangeDetectorRef,
    private EductiontionTypeSV: EducationTypesService,
    private AcademicYearSV: AcademicYearService,
    private AcademicSemSV: AcademicSemesterService,
    private CalendarTypeSV: CalendarTypeService,
  ) {
  }

  ngOnInit(): void {
    this._activeRouter.params.subscribe(
      result => {
        const uid: any = result;
        this.TestinforUid = uid.id;
        this.getGeneralData(uid.id)
      }
    );
  }

  async getGeneralData(id) {
    await this.EductiontionTypeSV.getAll().subscribe(
      result => {
        console.log(result);
        this.eductionTypeList = result;
      }
    );
    await this.AcademicYearSV.getAll().subscribe(
      result => {
        this.academicYearList = result;
      }
    );
    await this.AcademicSemSV.getAll().subscribe(
      result => {
        this.academicSemList = result;
      }
    );
    await this.TestInformationSV.getDatatype().subscribe(
      result => {
        this.EducationTypeLists = result;
      }
    );
    await this.CalendarTypeSV.getAll().subscribe(
      result => {
        this.calendarLists = result;
      }
    );
    await this.branchSV.getAll().subscribe(
      result => {
        this.subjectList = result;
      }
    );
    this.testingFieldSV.getAll().subscribe(
      result => {
        this.subjectList2 = result;
      }
    );
    await this.testcenterSV.getAll().subscribe(
      result => {
        this.subjectList3 = result;
      }
    );
    // await this.subjectSV.getAll().subscribe(
    //   result => {
    //     // this.dataSource2 = new MatTableDataSource(result);
    //     this.dataSource2 = result;
    //   }
    // );
    await this.SetData(id);
  }
  SetData(id) {
    this.TestInformationSV.get(id).subscribe(
      result => {
        this.data = result;
        this.addEditBlockadd(result);
        this.dataSource2 =  this.data.testing_subjects;
        this.dataSource =  this.data.testing_rooms;
        this.dataSource3 =  this.data.testing_calendars;
        this.dataSource4 =  this.data.testing_periods;
        this.cdRef.detectChanges();
      }
    );
  }

  addEditBlockadd(x){
    // const form = this.setexamform.get('days_without_testings') as FormArray;
    console.log(x.days_without_testings);
    for (let i = 0; i < x.days_without_testings.length; i++) {
      x.days_without_testings[i].date_without_testing = formatDate(x.days_without_testings[i].date_without_testing, 'yyyy-MM-dd', 'en-US')
      this.model.push(x.days_without_testings[i].date_without_testing);
    }
  }

  searchSummarySubject(testdate){
    this.TestInformationSV.getSummarySubject(this.TestinforUid, testdate).subscribe(
      result => {
        this.dataSource5 = result;
        this.cdRef.detectChanges();
      }
    );
  }



}
