import {Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef} from '@angular/core';
import {CandidateService} from '../../../../core/service/report/candidate/candidate.service';
import {CandidateRegisteredByCenterService} from '../../../../core/service/report/candidate-registered-by-center/candidate-registered-by-center.service';
import {CandidateRegisteredBySubjectService} from '../../../../core/service/report/candidate-registered-by-subject/candidate-registered-by-subject.service';
import {CandidateSubjectService} from '../../../../core/service/report/candidate-subject/candidate-subject.service';
import {PaymentAndApplicationByCenterService} from '../../../../core/service/report/payment-and-application-by-center/payment-and-application-by-center.service';
import {PaymentAndApplicationBySubjectService} from '../../../../core/service/report/payment-and-application-by-subject/payment-and-application-by-subject.service';
import {SettingTestingCenterService} from '../../../../core/service/report/setting-testing-center/setting-testing-center.service';
import {SettingTestingRoomService} from '../../../../core/service/report/setting-testing-room/setting-testing-room.service';
import {SummaryCandidateService} from '../../../../core/service/report/summary-candidate/summary-candidate.service';
import {TestingCenterInformationService} from '../../../../core/service/report/testing-center-information/testing-center-information.service';
import {TestingRegistrationService} from '../../../../core/service/report/testing-registration/testing-registration.service';
import {TestingSeatAndRoomService} from '../../../../core/service/report/testing-seat-and-room/testing-seat-and-room.service';
import {SignatureExaminationService} from '../../../../core/service/report/signature-examination/signature-examination.service';
import {RegisterByPaymentService} from '../../../../core/service/report/register-by-payment/register-by-payment.service';
import {SweetalertService} from '../../../../core/service/sweetalert/sweetalert';
import {SeparatedByExamCenterService} from '../../../../core/service/report/separated-by-exam-center/separated-by-exam-center.service';
import {ExamTotalService} from '../../../../core/service/report/exam_total/exam-total.service';
import {TesingSubjectService} from '../../../../core/service/report/testing-subject/tesing-subject.service';
import {TestingRoomService} from '../../../../core/service/report/testing-room/testing-room.service';

@Component({
  selector: 'app-report',
  templateUrl: './report.container.html',
  styleUrls: ['./report.container.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ReportContainer implements OnInit {
  fileURL: any = null;
  constructor(
    private swSV: SweetalertService,
    private cdr: ChangeDetectorRef,
    private candidateReportSV: CandidateService,
    private candidateRegisBySubReportSV: CandidateRegisteredBySubjectService,
    private candidateRegisByCenReportSV: CandidateRegisteredByCenterService,
    private candidateSubjectSV: CandidateSubjectService,
    private paymentANDappByCenReportSV: PaymentAndApplicationByCenterService,
    private paymentANDappBySubReportSV: PaymentAndApplicationBySubjectService,
    private settingTestingCenterSV: SettingTestingCenterService,
    private settingTestingRoomSV: SettingTestingRoomService,
    private registerByPaymentReportSV: RegisterByPaymentService,
    private summaryCandidateSV: SummaryCandidateService,
    private signatureExamReportSV: SignatureExaminationService,
    private testingCenterInformationSV: TestingCenterInformationService,
    private testingRegisterReportSV: TestingRegistrationService,
    private testingSeatRoomReportSV: TestingSeatAndRoomService,
    private separatedByExamCenterSV: SeparatedByExamCenterService,
    private examTotalSV: ExamTotalService,
    private testingSubjectSV: TesingSubjectService,
    private testingRoomSV: TestingRoomService,
  ) { }

  ngOnInit(): void {
  }

  excel(choose){
    const reportId = choose.reportId;
    const query = choose.query;
    switch (reportId) {
      case 1: {
        console.log(query);
        this.candidateReportSV.getExcelReport(
          query.testing_information_uid,
          query.academic_year_uid,
          query.academic_semester_uid,
          this.convert(query.from_date),
          this.convert(query.to_date)).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 2: {
        console.log(query);
        this.summaryCandidateSV.getExcelReport(
          query.academic_year_uid,
          query.academic_semester_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 3: {
        console.log(query);
        this.testingRegisterReportSV.getExcelReport(
          query.testing_information_uid,
          query.academic_year_uid,
          query.academic_semester_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 4: {
        console.log(query);
        this.candidateRegisBySubReportSV.getExcelReport(
          query.subject_uid,
          query.academic_year_uid,
          query.academic_semester_uid,
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 5: {
        console.log(query);
        this.candidateRegisByCenReportSV.getExcelReport(
          query.academic_year_uid,
          query.academic_semester_uid,
          query.branch_uid,
          query.testing_field_uid,
          query.testing_center_uid,
          query.subject_uid).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 6: {
        console.log(query);
        this.paymentANDappBySubReportSV.getExcelReport(
          query.academic_year_uid,
          query.academic_semester_uid,
          query.subject_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 7: {
        console.log(query);
        this.paymentANDappByCenReportSV.getExcelReport(
          query.academic_year_uid,
          query.academic_semester_uid,
          query.branch_uid,
          query.testing_field_uid,
          query.testing_center_uid,
          query.subject_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 8: {
        console.log(query);
        this.registerByPaymentReportSV.getExcelReport(
          query.testing_information_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 9: {
        console.log(query);
        this.testingCenterInformationSV.getExcelReport(
          query.testing_information_uid,
          query.subject_uid,
          query.academic_year_uid,
          query.academic_semester_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 10: {
        console.log(query);
        this.testingSeatRoomReportSV.getExcelReport(
          query.testing_information_uid,
          query.branch_uid,
          query.period_id,
          this.convert(query.test_date),
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 12: {
        console.log(query);
        this.signatureExamReportSV.getExcelReport(
          query.testing_information_uid,
          query.branch_uid,
          query.testing_field_uid,
          query.testing_center_uid,
          query.period_id,
          this.convert(query.test_date),
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 13: {
        this.candidateSubjectSV.getExcelReport(
          query.academic_year_uid,
          query.academic_semester_uid,
          query.subject_uid
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 14: {
        console.log(query);
        this.settingTestingCenterSV.getExcelReport(
          query.testing_information_uid,
          query.academic_year_uid,
          query.academic_semester_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 15: {
        console.log(query);
        this.settingTestingRoomSV.getExcelReport(
          query.testing_information_uid,
          query.academic_year_uid,
          query.academic_semester_uid,
          query.branch_uid,
          query.testing_field_uid,
          query.testing_center_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 16: {
        console.log(query);
        this.settingTestingRoomSV.getExcelReport16(
          query.subject_uid,
          query.testing_information_uid,
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 17: {
        this.settingTestingRoomSV.getExcelReport17(
          query.testing_information_uid,
          query.academic_year_code,
          query.academic_semester_code,
          query.branch_uid,
          query.testing_center_uid,
          query.room_name_th,
        ).subscribe(
          res => {
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 18: {
        this.separatedByExamCenterSV.getExcelReport(
          query.testing_information_uid,
          query.subject_uid,
          query.branch_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล')
          });
        break;
      }
      case 19: {
        this.examTotalSV.getExcelReport(
          query.testing_information_uid,
        ).subscribe(
          res => {
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล')
          });
        break;
      }
      case 20: {
        this.testingRoomSV.getExcelReport(
          query.testing_information_uid,
        ).subscribe(
          res => {
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล')
          });
        break;
      }
      case 21: {
        this.testingSubjectSV.getExcelReport(
          query.testing_information_uid,
        ).subscribe(
          res => {
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล')
          });
        break;
      }
    }
  }

  search(choose){
    console.log(choose);
    const reportId = choose.reportId;
    const query = choose.query;
    switch (reportId) {
      case 1: {
        console.log(query);
        this.candidateReportSV.getPdfReport(
          query.testing_information_uid,
          query.academic_year_uid,
          query.academic_semester_uid,
          this.convert(query.from_date),
          this.convert(query.to_date)).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 2: {
        console.log(query);
        this.summaryCandidateSV.getPdfReport(
          query.academic_year_uid,
          query.academic_semester_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 3: {
        console.log(query);
        this.testingRegisterReportSV.getPdfReport(
          query.testing_information_uid,
          query.academic_year_uid,
          query.academic_semester_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 4: {
        console.log(query);
        this.candidateRegisBySubReportSV.getPdfReport(
          query.subject_uid,
          query.academic_year_uid,
          query.academic_semester_uid,
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 5: {
        console.log(query);
        this.candidateRegisByCenReportSV.getPdfReport(
          query.academic_year_uid,
          query.academic_semester_uid,
          query.branch_uid,
          query.testing_field_uid,
          query.testing_center_uid,
          query.subject_uid).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 6: {
        console.log(query);
        this.paymentANDappBySubReportSV.getPdfReport(
          query.academic_year_uid,
          query.academic_semester_uid,
          query.subject_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 7: {
        console.log(query);
        this.paymentANDappByCenReportSV.getPdfReport(
          query.academic_year_uid,
          query.academic_semester_uid,
          query.branch_uid,
          query.testing_field_uid,
          query.testing_center_uid,
          query.subject_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 8: {
        console.log(query);
        this.registerByPaymentReportSV.getPdfReport(
          query.testing_information_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 9: {
        console.log(query);
        this.testingCenterInformationSV.getPdfReport(
          query.testing_information_uid,
          query.subject_uid,
          query.academic_year_uid,
          query.academic_semester_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 10: {
        console.log(query);
        this.testingSeatRoomReportSV.getPdfReport(
          query.testing_information_uid,
          query.branch_uid,
          query.period_id,
          this.convert(query.test_date)
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 12: {
        console.log(query);
        this.signatureExamReportSV.getPdfReport(
          query.testing_information_uid,
          query.branch_uid,
          query.testing_field_uid,
          query.testing_center_uid,
          query.period_id,
          this.convert(query.test_date),
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 13: {
        this.candidateSubjectSV.getPdfReport(
          query.academic_year_uid,
          query.academic_semester_uid,
          query.subject_uid
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 14: {
        console.log(query);
        this.settingTestingCenterSV.getPdfReport(
          query.testing_information_uid,
          query.academic_year_uid,
          query.academic_semester_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล');
          });
        break;
      }
      case 15: {
        console.log(query);
        this.settingTestingRoomSV.getPdfReport(
          query.testing_information_uid,
          query.academic_year_uid,
          query.academic_semester_uid,
          query.branch_uid,
          query.testing_field_uid,
          query.testing_center_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล')
          });
        break;
      }
      case 16: {
        console.log(query);
        this.settingTestingRoomSV.getPdfReport16(
          query.subject_uid,
          query.testing_information_uid,
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล')
          });
        break;
      }
      case 17: {
        this.settingTestingRoomSV.getPdfReport17(
          query.testing_information_uid,
          query.academic_year_code,
          query.academic_semester_code,
          query.branch_uid,
          query.testing_center_uid,
          query.room_name_th,
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล')
          });
        break;
      }
      case 18: {
        this.separatedByExamCenterSV.getPdfReport(
          query.testing_information_uid,
          query.subject_uid,
          query.branch_uid,
          this.convert(query.from_date),
          this.convert(query.to_date),
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล')
          });
        break;
      }
      case 19: {
        this.examTotalSV.getPdfReport(
          query.testing_information_uid,
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล')
          });
        break;
      }
      case 20: {
        this.testingRoomSV.getPdfReport(
          query.testing_information_uid,
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล')
          });
        break;
      }
      case 21: {
        this.testingSubjectSV.getPdfReport(
          query.testing_information_uid,
        ).subscribe(
          res => {
            const fileURL = URL.createObjectURL(res);
            this.fileURL = fileURL;
            this.cdr.detectChanges();
            // window.open(fileURL, '_blank');
          },
          error => {
            this.swSV.errText('ไม่พบข้อมูล')
          });
        break;
      }
    }
  }

  convert(str) {
    var date = new Date(str),
      mnth = ("0" + (date.getMonth() + 1)).slice(-2),
      day = ("0" + date.getDate()).slice(-2);
    return [date.getFullYear(), mnth, day].join("-");
  }

}
