import {Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef} from '@angular/core';
import { formatDate } from '@angular/common';
import {BaseForm} from '../../../../../../core/base/base-form';
import {SubjectsService} from '../../../../../../core/service/settings/subjects.service';
import {TestingCenterService} from '../../../../../../core/service/TestingCenter/testing-center.service';
import {FormArray, FormBuilder} from '@angular/forms';
import {MatDialog} from '@angular/material/dialog';
import {ActivatedRoute, Router} from '@angular/router';
import {Location} from '@angular/common';
import {SweetalertService} from '../../../../../../core/service/sweetalert/sweetalert';
import {TestingInformationService} from '../../../../../../core/service/testing-information/testing-information.service';
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 {tap} from 'rxjs/operators';
import {MatTableDataSource} from '@angular/material/table';
import {SetExamInformationDialogComponent} from '../../../../../set-exam-information/presenter/dialog/set-exam-information-dialog/set-exam-information-dialog.component';
import {SelectionModel} from '@angular/cdk/collections';
import {TestingFieldService} from '../../../../../../core/service/settings/testing-field.service';
import {BranchService} from '../../../../../../core/service/settings/branch.service';
import {CalendarTypeService} from '../../../../../../core/service/settings/calendar-type.service';
import {CandidateTypeService} from '../../../../../../core/service/enrollment-api/candidate-type.service';

@Component({
  selector: 'app-change-exam-information-form',
  templateUrl: './change-exam-information-form.component.html',
  styleUrls: ['./change-exam-information-form.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ChangeExamInformationFormComponent extends BaseForm implements OnInit {
  selection = new SelectionModel(true, []);
  isInfo = false;
  dateBeforestartRegister;
  dateBeforestartTesting;
  subjectselectList;
  setexamform;
  setroomcenterForm;
  testingInfoData;
  eductionTypeList: any = [];
  academicYearList: any = [];
  calendarLists: any = [];
  EducationTypeLists: any = [];
  academicSemList: any = [];
  subjectList: any = [];
  subjectList2: any = [];
  subjectList3: any = [];
  dataSource3 = new MatTableDataSource();
  displayedColumns3: string[] = ['1', '2', '3', '4', '5', '6', '7'];
  candidateTypes: any = [];
  setupdata = {
    testing_center_uid: null,
    testing_center_name_th: null,
    testing_room_name_th: null,
    testing_auth_name: null,
    testing_field_name_th: null,
    branch_name_th: null,
    branch_uid: null,
    testing_field_uid: null,
    testing_room_uid:  null,
  };
  public model = [
  ];
  constructor(
    private subjectSV: SubjectsService,
    private testcenterSV: TestingCenterService,
    private testFiledSV: TestingFieldService,
    private branchSV: BranchService,
    private formBuilder: FormBuilder,
    private dialog: MatDialog,
    public router: Router,
    public activeRoute: ActivatedRoute,
    public location: Location,
    private swSV: SweetalertService,
    private cdRef: ChangeDetectorRef,
    private TestInformationSV: TestingInformationService,
    private EductiontionTypeSV: EducationTypesService,
    private AcademicYearSV: AcademicYearService,
    private AcademicSemSV: AcademicSemesterService,
    private CalendarTypeSV: CalendarTypeService,
    private CandidateTypeSV: CandidateTypeService,
  ) {
    super(
      router,
      activeRoute,
      formBuilder,
      location
    );
    this.setexamform = this.formBuilder.group({
      status_id: null,
      created_by: null,
      created_datetime: null,
      updated_by: null,
      updated_datetime: null,
      deleted_by: null,
      deleted_datetime: null,
      testing_information_uid: null,
      testing_information_name: null,
      testing_information_code: null,
      academic_semester_code: null,
      academic_year_code: null,
      testing_information_status: null,
      type_of_examinee: null,
      is_student_code: true,
      first_student_code: '',
      last_student_code: '',
      start_datetime: null,
      end_datetime: null,
      registration_start_datetime: null,
      registration_end_datetime: null,
      testing_start_datetime: null,
      testing_end_datetime: null,
      is_main: false,
      main_testing_information_uid: null,
      is_open: null,
      is_upload_document: false,
      is_register_verify: false,
      is_register: false,
      is_counter: false,
      days_without_testings: this.formBuilder.array([
        this.block_days(null)
      ]),
      testing_calendars: this.formBuilder.array([
        this.testing_calendars()
      ]),
      testing_subjects: this.formBuilder.array([
        this.testing_subjects()
      ]),
      testing_rooms: this.formBuilder.array([
        this.testing_rooms()
      ]),
      testing_periods: this.formBuilder.array([
        this.testing_periods()
      ])
    });
    this.setroomcenterForm = this.formBuilder.group({
      branch_name_th: null,
      branch_uid: null,
      testing_center_id: null,
      testing_field_name_th: null,
      testing_field_uid: null,
      testing_room_name_th: null,
      testing_auth_name: null,
      is_random_seat: null,
    });
  }
  block_days(blockdays?){
    return this.formBuilder.group({
      status_id: null,
      testing_information_uid: blockdays?.testing_information_uid,
      days_without_testing_uid: blockdays?.days_without_testing_uid,
      date_without_testing: blockdays?.date_without_testing,
    });
  }
  testing_periods(data?){
    return this.formBuilder.group({
      status_id: null,
      // created_by: null,
      // created_datetime: null,
      // updated_by: null,
      // updated_datetime: null,
      // deleted_by: null,
      // deleted_datetime: null,
      testing_information_uid: data?.testing_information_uid,
      testing_period_uid: data?.testing_period_uid,
      period_id: data?.period_id,
      start_time: data?.start_time,
    });
  }
  testing_calendars(calender?){
    return this.formBuilder.group({
      status_id: null,
      testing_calendar_uid: calender?.testing_calendar_uid,
      start_datetime: calender?.start_datetime,
      end_datetime: calender?.end_datetime,
      calendar_type_uid: calender?.calendar_type_uid,
      calendar_type_name_th: calender?.calendar_type_name_th,
      testing_information_uid: calender?.testing_information_uid,
    });
  }

  testing_subjects(data?){
    return this.formBuilder.group({
      testing_subject_uid: data?.testing_subject_uid,
      status_id: null,
      subject_uid: data?.subject_uid,
      amount: data?.amount,
      testing_information_uid: data?.testing_information_uid,
    });
  }

  testing_rooms(data?){
    return this.formBuilder.group({
      testing_room_uid: data?.testing_room_uid,
      testing_center_uid: data?.testing_center_uid,
      // testing_center_name_th: data?.testing_center_name_th,
      branch_uid: data?.branch_uid,
      branch_name_th: data?.branch_name_th,
      testing_field_uid: data?.testing_field_uid,
      testing_field_name_th: data?.testing_field_name_th,
      testing_room_name_th: data?.testing_room_name_th,
      testing_auth_name: data?.testing_auth_name,
      is_random_seat: data?.is_random_seat,
      testing_seats: this.formBuilder.array([
        this.testing_seats(data?.testing_seats)
      ]),
    });
  }

  testing_seats(data?){
    return this.formBuilder.group({
      status_id: null,
      // created_by: null,
      // created_datetime: null,
      // updated_by: null,
      // updated_datetime: null,
      // deleted_by: null,
      // deleted_datetime: null,
      testing_seat_uid: data?.testing_seat_uid,
      testing_center_row_uid: data?.testing_center_row_uid,
      is_specify_subject: data?.is_specify_subject,
      testing_room_uid: data?.testing_room_uid,
      testing_seat_subjects: this.formBuilder.array([
        this.testing_seat_subjects(data?.testing_seat_subjects)
      ]),
    });
  }

  testing_seat_subjects(data?){
    return this.formBuilder.group({
      status_id: null,
      testing_seat_subject_uid: data?.testing_seat_subject_uid,
      subject_uid: data?.subject_uid,
      start_seat_no: data?.start_seat_no,
      end_seat_no: data?.end_seat_no,
      testing_seat_uid: data?.testing_seat_uid,
    });
  }

  ngOnInit(): void {
    const form = this.setexamform.get('testing_calendars') as FormArray;
    const form2 = this.setexamform.get('testing_subjects') as FormArray;
    const form3 = this.setexamform.get('testing_rooms') as FormArray;
    const form4 = this.setexamform.get('testing_periods') as FormArray;
    const form5 = this.setexamform.get('days_without_testings') as FormArray;

    this.EductiontionTypeSV.getAll().subscribe(
      result => {
        this.eductionTypeList = result;
      }
    );
    this.AcademicYearSV.getAll().subscribe(
      result => {
        this.academicYearList = result;
      }
    );
    this.AcademicSemSV.getAll().subscribe(
      result => {
        this.academicSemList = result;
      }
    );
    this.CandidateTypeSV.getAll().subscribe(
      result => {
        this.candidateTypes = result;
      }
    );
    this.branchSV.getAll().pipe(
      tap(x => this.subjectList = x),
    ).subscribe();
    this.testFiledSV.getAll().pipe(
      tap(x => this.subjectList2 = x),
    ).subscribe();
    this.testcenterSV.getAll().pipe(
      tap(x => this.subjectList3 = x),
    ).subscribe();
    switch (this.state) {
      case 'edit':
        this.activeRoute.url.subscribe(params => {
          console.log(params[0].path);
          if (params[0].path === 'info'){
            this.isInfo = true
          }
        });
        this.TestInformationSV.get(this.id).pipe(
          tap(x => this.testingInfoData = x),
          // tap(x => this.add3Edit(x)),
          tap(x => {
            const subjectList: any = x;
            this.subjectselectList = subjectList.testing_subjects;
          }),
          tap(x =>  {
            form.clear();
            // form2.clear();
            form3.clear();
            form4.clear();
            form5.clear();
          }),
          // tap(x => this.setexamform.patchValue(x)),
          tap(x => {
            const data: any = x;
            data.registration_start_datetime = this.formatDate(data.registration_start_datetime);
            data.registration_end_datetime = this.formatDate(data.registration_end_datetime);
            this.dateBeforestartRegister = data.registration_end_datetime;
            data.testing_start_datetime = this.formatDate(data.testing_start_datetime);
            data.testing_end_datetime = this.formatDate(data.testing_end_datetime);
            this.dateBeforestartTesting = data.testing_end_datetime;
            this.setexamform.patchValue(data);
          }),
          tap(x => {
            const testInfo: any = x;
            if (testInfo.main_testing_information_uid != null){
              this.add3Edit(x);
            }
          }),
          tap(x => this.selectValueEdit(x)),
          tap(x => this.addPeriodEdit(x)),
          tap(x => this.addCalendaEdit(x)),
          tap(x => this.addEditBlockadd(x)),
          tap(x => this.cdRef.detectChanges()),
        ).subscribe();
        break;
      case 'add':
        break;
      case 'expand':
        this.TestInformationSV.get(this.id).pipe(
          // tap(x => this.add3Edit(x)),
          tap(x => {
            const subjectList: any = x;
            this.subjectselectList = subjectList.testing_subjects;
          }),
          tap(x =>  {
            // form.clear();
            // form2.clear();
            form3.clear();
          }),
          // tap(x => this.setexamform.patchValue(x)),
          tap(x => {
            const data: any = x;
            data.registration_start_datetime = this.formatDate(data.registration_start_datetime);
            data.registration_end_datetime = this.formatDate(data.registration_end_datetime);
            this.dateBeforestartRegister = data.registration_end_datetime;
            data.testing_start_datetime = this.formatDate(data.testing_start_datetime);
            data.testing_end_datetime = this.formatDate(data.testing_end_datetime);
            this.dateBeforestartTesting = data.testing_end_datetime;
            this.setexamform.patchValue(data);
          }),
          // tap(x => this.addCalendaEdit(x)),
          tap(x => {
            const testInfo: any = x;
            if (testInfo.main_testing_information_uid != null){
              this.add3Edit(x);
            }
          }),
          // tap(x => this.add3Edit(x)),
          tap(x => this.selectValueEdit(x)),
          tap(x => this.cdRef.detectChanges()),
        ).subscribe();
        break;
    }
  }

  addCalendaEdit(x){
    const form2 = this.setexamform.get('testing_calendars') as FormArray;
    for (let i = 0; i < x.testing_calendars.length; i++) {
      x.testing_calendars[i].end_datetime = this.formatDate(x.testing_calendars[i].start_datetime);
      x.testing_calendars[i].start_datetime = this.formatDate( x.testing_calendars[i].start_datetime);
      form2.push(this.testing_calendars(x.testing_calendars[i]));
    }
  }

  addEditBlockadd(x){
    const form = this.setexamform.get('days_without_testings') as FormArray;
    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')
      form.push(this.block_days(x.days_without_testings[i]));
      this.model.push(x.days_without_testings[i].date_without_testing);
    }
    // this.cdRef.detectChanges();
  }

  addPeriodEdit(x){
    console.log(x.testing_periods);
    const form = this.setexamform.get('testing_periods') as FormArray;
    for (let i = 0; i < x.testing_periods.length; i++) {
      form.push(this.testing_periods(x.testing_periods[i]));
    }
  }

  selectValueEdit(x){
    // this.selection.clear()
    // this.dataSource2.data.forEach(x => this.selection.select(x.testing_subjects[0]));
    const subjectForm = this.setexamform.get('testing_subjects') as FormArray;
    subjectForm.clear();
    for (let i = 0; i < x.testing_subjects.length ; i++) {
      this.selection.selected.push(x.testing_subjects[i]);
      subjectForm.push(this.testing_subjects(x.testing_subjects[i]));
      // this.selection.select(x.testing_subjects[i]);
    }
    // this.selection.clear() :
    // this.dataSource2.data.forEach(row => this.selection.select(row));
    console.log(this.selection.selected);
    this.cdRef.detectChanges();
  }

  save(){
    const form = this.setexamform.getRawValue();
    this.setexamform.get('testing_information_uid').setValue(null);
    this.setexamform.get('is_main').setValue(false);
    this.setexamform.get('main_testing_information_uid').setValue(form.testing_information_uid);
    const datestartRegister = this.Increasedate(this.dateBeforestartRegister);
    const datestartTesting = this.Increasedate(this.dateBeforestartTesting);
    this.setexamform.get('registration_start_datetime').setValue(datestartRegister);
    this.setexamform.get('testing_start_datetime').setValue(datestartTesting);
    // console.log(this.setexamform.getRawValue());
    this.TestInformationSV.add(this.setexamform.getRawValue()).pipe(
      tap(x => this.swSV.updateSuccess()),
    ).subscribe();
  }

  formatDate(date) {
    let d = new Date(date),
      month = '' + (d.getMonth() + 1),
      day = '' + d.getDate(),
      year = d.getFullYear();

    if (month.length < 2) {
      month = '0' + month;
    }
    if (day.length < 2) {
      day = '0' + day;
    }

    return [year, month, day].join('-');
  }

  setFormtestcentername(testcenterData, choose){
    console.log(testcenterData);
    console.log(choose);
    if (choose === 'branch_name_th'){
      this.setupdata.branch_name_th  = testcenterData.branch_name_th;
      this.setupdata.branch_uid = testcenterData.branch_uid;
      this.testFiledSV.getAll().pipe(
        tap(x => console.log(x)),
        tap(x => this.subjectList2 = x),
      ).subscribe(
        result => {
          this.subjectList2 = this.subjectList2.filter((x) => {
            return x.branch_uid === testcenterData.branch_uid;
          });
        }
      );
      // array.push(testcenterData);
      // this.subjectList3 = array;
    }
    if (choose === 'testing_field_name_th'){
      this.setroomcenterForm.get('testing_room_name_th').reset();
      this.setupdata.testing_field_name_th  = testcenterData.testing_field_name_th;
      this.setupdata.testing_field_uid  = testcenterData.testing_field_uid;
      this.testcenterSV.getAll().pipe(
        tap(x => console.log(x)),
        tap(x => this.subjectList3 = x),
      ).subscribe(
        result => {
          this.subjectList3 = this.subjectList3.filter((x) => {
            return x.testing_field_uid === testcenterData.testing_field_uid;
          });
        }
      );
    }
    if (choose === 'testing_room_name_th'){
      this.setupdata.testing_room_name_th  = testcenterData.testing_room_name_th;
      this.setupdata.testing_room_uid  = testcenterData.testing_center_uid;
      this.setupdata.testing_center_uid  = testcenterData.testing_center_uid;
    }
  }

  add3(){
    const form = this.setroomcenterForm.getRawValue();
    const form3 = this.setexamform.get('testing_rooms') as FormArray;
    this.setupdata.testing_auth_name = form.testing_auth_name;
    form3.push(this.testing_rooms(this.setupdata));
    const table = form3.getRawValue();
    this.dataSource3.data = table;
  }
  add3Edit(data){
    const form = this.setexamform.get('testing_rooms') as FormArray;
    for (let i = 0; i < data.testing_rooms.length; i++) {
      form.push(this.testing_rooms(data.testing_rooms[i]));
      const formArray = form.controls[i].get('testing_seats') as FormArray;
      const FormArraySeat = form.controls[i].get('testing_seat_subjects') as FormArray;
      formArray.clear();
      for (let j = 0; j < data.testing_rooms[i].testing_seats.length; j++) {
        formArray.push(this.testing_seats(data.testing_rooms[i].testing_seats[j]));
        const formArraySeatSubject = formArray.controls[j].get('testing_seat_subjects') as FormArray;
        formArraySeatSubject.clear();
        formArraySeatSubject.push(this.testing_seat_subjects(data.testing_rooms[i].testing_seats[j].testing_seat_subjects[0]));
      }
    }
    const table = form.getRawValue();
    console.log(table);
    this.dataSource3.data = table;
  }
  removeArray3(i){
    const form = this.setexamform.get('testing_rooms') as FormArray;
    form.removeAt(i);
    const table = form.getRawValue();
    this.dataSource3.data = table;
  }

  selectionValue(data, i){
    switch (this.state) {
      case 'edit':
        let israndom = true;
        if (data.is_random_seat === true){
          israndom = false;
        }else if (data.is_random_seat === false){
          israndom = true;
        }
        const form = this.setexamform.get('testing_rooms') as FormArray;
        form.at(i).get('is_random_seat').setValue(israndom);
        const table = form.getRawValue();
        this.dataSource3.data = table;
        break;
      case 'add':
        break;
    }
    console.log(i);
    console.log(data);
  }

  openDialogexam(id: any, index: any){
    const form = this.setexamform.get('testing_rooms') as FormArray;
    const form2 = this.setexamform.get('testing_subjects') as FormArray;
    const form2raw = form2.getRawValue();
    const centerList: any = form.getRawValue();
    const switchstate = centerList[0].testing_seat_uid;
    console.log(this.subjectselectList);
    switch (this.state) {
      case 'edit':
        console.log(6666);
        const uidInformtion = this.id;
        const dialogRefedit = this.dialog.open(SetExamInformationDialogComponent, {
          width: '1000px',
          height: '700px',
          data: {
            id : id,
            centerList : centerList[index],
            subjectlist : this.subjectselectList,
            state : switchstate === null ? 'add' : 'edit',
          }
        });
        dialogRefedit.afterClosed().subscribe(
          result => {
            const testseatdata = result.data;
            const data = testseatdata.getRawValue();
            const formArray3 = form.at(0).get('testing_seats') as FormArray;
            formArray3.clear();
            console.log(data);
            const testseat = data;
            const formArray = form.controls[index].get('testing_seats') as FormArray;
            for (let j = 0; j < testseat.testing_seat.length; j++) {
              formArray.push(testseatdata.get('testing_seat').controls[j]);
            }
            // for (let i = 0; i < form.getRawValue().length; i++) {
            //   const formArray = form.controls[i].get('testing_seats') as FormArray;
            //   for (let j = 0; j < testseat.testing_seat.length; j++) {
            //     formArray.push(testseatdata.get('testing_seat').controls[j]);
            //   }
            // }
            this.cdRef.detectChanges();
          }
        );
        break;
      case 'add':
        const dialogRefadd = this.dialog.open(SetExamInformationDialogComponent, {
          width: '1000px',
          height: '700px',
          data: {
            id,
            subjectlist : this.subjectselectList,
            state : this.state,
          }
        });
        dialogRefadd.afterClosed().subscribe(
          result => {
            const testseatdata = result.data;
            const data = testseatdata.getRawValue();
            const formArray3 = form.at(0).get('testing_seats') as FormArray;
            formArray3.clear();
            console.log(data);
            const testseat = data;
            for (let i = 0; i < form.getRawValue().length; i++) {
              const formArray = form.controls[i].get('testing_seats') as FormArray;
              for (let j = 0; j < testseat.testing_seat.length; j++) {
                formArray.push(testseatdata.get('testing_seat').controls[j]);
              }
            }
            this.cdRef.detectChanges();
          }
        );
        break;
      case 'expand':
        console.log(5555);
        const dialogRefaddd = this.dialog.open(SetExamInformationDialogComponent, {
          width: '1000px',
          height: '700px',
          data: {
            id,
            subjectlist : this.subjectselectList,
            state : this.state,
          }
        });
        dialogRefaddd.afterClosed().subscribe(
          result => {
            const testseatdata = result.data;
            const data = testseatdata.getRawValue();
            const formArray3 = form.at(0).get('testing_seats') as FormArray;
            formArray3.clear();
            console.log(data);
            const testseat = data;
            for (let i = 0; i < form.getRawValue().length; i++) {
              const formArray = form.controls[i].get('testing_seats') as FormArray;
              for (let j = 0; j < testseat.testing_seat.length; j++) {
                formArray.push(testseatdata.get('testing_seat').controls[j]);
              }
            }
            this.cdRef.detectChanges();
          }
        );
        break;
    }
  }


  Increasedate(date_str: any){
    const parts = date_str.split('-');
    const dt = new Date(
      parseInt(parts[0], 10),      // year
      parseInt(parts[1], 10) - 1,  // month (starts with 0)
      parseInt(parts[2], 10)       // date
    );
    dt.setDate(dt.getDate() + 1);
    parts[0] = '' + dt.getFullYear();
    parts[1] = '' + (dt.getMonth() + 1);
    if (parts[1].length < 2) {
      parts[1] = '0' + parts[1];
    }
    parts[2] = '' + dt.getDate();
    if (parts[2].length < 2) {
      parts[2] = '0' + parts[2];
    }
    return parts.join('-');
  }


  incr_dateRegis(setNew, date){
    let countRe = 0;
    let countTe = 0;
    let RegisStartTime = null;
    let TestStartTime = null;
    console.log(setNew);
    // Set registration_start_datetime , testing_start_datetime
    RegisStartTime = this.setexamform.get('registration_end_datetime').value;
    TestStartTime = this.setexamform.get('testing_end_datetime').value;
    let parts = date.split('-');
    if (setNew === 'registration_start_datetime'){
      parts = RegisStartTime.split('-');
    }else if (setNew === 'testing_start_datetime'){
      parts = TestStartTime.split('-');
    }
    const dt = new Date(
      parseInt(parts[0], 10),      // year
      parseInt(parts[1], 10) - 1,  // month (starts with 0)
      parseInt(parts[2], 10)       // date
    );
    dt.setDate(dt.getDate() + 1);
    parts[0] = '' + dt.getFullYear();
    parts[1] = '' + (dt.getMonth() + 1);
    if (parts[1].length < 2) {
      parts[1] = '0' + parts[1];
    }
    parts[2] = '' + dt.getDate();
    if (parts[2].length < 2) {
      parts[2] = '0' + parts[2];
    }
    parts.join('-');
    const newDate = parts.join('-');
    console.log(newDate);
    if (setNew === 'registration_start_datetime'){
      this.setexamform.get('registration_start_datetime').setValue(newDate);
    }else if (setNew === 'testing_start_datetime'){
      this.setexamform.get('testing_start_datetime').setValue(newDate);
    }
    countRe++;
    countTe++;
  }

  incr_dateTest(setNew, date){
    let countRe = 0;
    let countTe = 0;
    let RegisStartTime = null;
    let TestStartTime = null;
    console.log(setNew);
    // Set registration_start_datetime , testing_start_datetime
    RegisStartTime = this.setexamform.get('registration_end_datetime').value;
    TestStartTime = this.setexamform.get('testing_end_datetime').value;
    let parts = date.split('-');
    if (setNew === 'registration_start_datetime'){
      parts = RegisStartTime.split('-');
    }else if (setNew === 'testing_start_datetime'){
      parts = TestStartTime.split('-');
    }
    const dt = new Date(
      parseInt(parts[0], 10),      // year
      parseInt(parts[1], 10) - 1,  // month (starts with 0)
      parseInt(parts[2], 10)       // date
    );
    dt.setDate(dt.getDate() + 1);
    parts[0] = '' + dt.getFullYear();
    parts[1] = '' + (dt.getMonth() + 1);
    if (parts[1].length < 2) {
      parts[1] = '0' + parts[1];
    }
    parts[2] = '' + dt.getDate();
    if (parts[2].length < 2) {
      parts[2] = '0' + parts[2];
    }
    parts.join('-');
    const newDate = parts.join('-');
    console.log(newDate);
    if (setNew === 'registration_start_datetime'){
      this.setexamform.get('registration_start_datetime').setValue(newDate);
    }else if (setNew === 'testing_start_datetime'){
      this.setexamform.get('testing_start_datetime').setValue(newDate);
    }
    countRe++;
    countTe++;
  }

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

}
