import { Component, OnInit, ChangeDetectionStrategy, Inject } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { throwError } from 'rxjs';
import { catchError, filter, tap } from 'rxjs/operators';
import { RuStudentService } from 'src/app/core/service/ru-student/ru-student.service';
import { StudentUploadService } from 'src/app/core/service/student/student-upload.service';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';

@Component({
    selector: 'app-upload-dialog',
    templateUrl: './upload-dialog.component.html',
    styleUrls: ['./upload-dialog.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class UploadDialogComponent implements OnInit {
    selectfile;
    constructor(
        public dialog: MatDialog,
        public dialogRef: MatDialogRef<UploadDialogComponent>,
        @Inject(MAT_DIALOG_DATA) public data: any,
        private studentUploadSV: StudentUploadService,
        private swSV: SweetalertService,
        private ruStudentUploadSV: RuStudentService
    ) { }

    ngOnInit(): void {

    }

    onSelectFile(event,isWord?) {
        if (event.target.files && event.target.files[0]) {
            this.selectfile = event.target.files[0];
            if(isWord){
                return this.uploadWord()
            }
            this.uploadExcel()
        }
    }

    uploadWord(){
        this.swSV.loadingStart('กำลังอัปโหลดไฟล์')
        const formData: FormData = new FormData();
        formData.append('file', this.selectfile, this.selectfile.name);
        this.studentUploadSV.uploadStudentWord(formData,this.data.testing_information.testing_information_uid).pipe(
            tap(x => this.swSV.close()),
            tap(x => this.swSV.saveSuccess('นำเข้าสำเร็จ')),
            tap(x => this.dialogRef.close({data : x})),
            catchError(err => {
                this.swSV.close()
                this.swSV.errText(err.error)
                return throwError(err)
            })
        ).subscribe()
    }


    uploadExcel(){
        this.swSV.loadingStart('กำลังอัปโหลดไฟล์')
        const formData: FormData = new FormData();
        formData.append('file', this.selectfile, this.selectfile.name);
        if(this.data.student_type == 1){
            this.ruStudentUploadSV.uploadStudent(formData).pipe(
                tap(x => this.swSV.close()),
                tap(x => this.dialogRef.close({data : x})),
                catchError(err => {
                    this.swSV.close()
                    this.swSV.errText(err.error)
                    return throwError(err)
                })
            ).subscribe()
        }else{
            this.studentUploadSV.uploadStudent(formData,this.data.testing_information.testing_information_uid).pipe(
                tap(x => this.swSV.close()),
                tap(x => this.swSV.saveSuccess('นำเข้าสำเร็จ')),
                tap(x => this.dialogRef.close({data : x})),
                catchError(err => {
                    this.swSV.close()
                    this.swSV.errText(err.error)
                    return throwError(err)
                })
            ).subscribe()
        }
    }

}
