import { Component, OnInit, ChangeDetectionStrategy, Inject } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { throwError } from 'rxjs';
import { tap, catchError } from 'rxjs/operators';
import { FacultysService } from 'src/app/core/service/settings/facultys.service';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { TestingFeeService } from 'src/app/core/service/testing-information/testing-fee.service';

@Component({
    selector: 'app-exam-finance-dialog',
    templateUrl: './exam-finance-dialog.component.html',
    styleUrls: ['./exam-finance-dialog.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class ExamFinanceDialogComponent implements OnInit {
    form;
    statusMock = [{id:1, name:'ใช้งาน'},{id:2, name:'ยกเลิก'}]
    constructor(
        private fb: FormBuilder,
        private testingFeeSV: TestingFeeService, //change later
        private swSV: SweetalertService,
        private dialogRef: MatDialogRef<ExamFinanceDialogComponent>,
        @Inject(MAT_DIALOG_DATA) public data
    ) {
        this.form = this.fb.group({
            status_id: null,
            created_by: null,
            created_datetime: null,
            updated_by: null,
            updated_datetime: null,
            deleted_by: null,
            deleted_datetime: null,
            testing_subject_uid: null,
            subject_uid: null,
            amount: null,
            testing_information_uid: null,
        })
     }

    ngOnInit(): void {
        console.log(this.data)
        this.form.patchValue(this.data.info)
    }

    save(){
        const form = this.form.getRawValue()
            this.testingFeeSV.update2(form).pipe(
                tap(x => this.swSV.updateSuccess()),
                tap(x => this.dialogRef.close()),
                catchError(err => {
                    this.swSV.err()
                    this.dialogRef.close()
                    return throwError(err)
                })
            ).subscribe()
    }

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

}
