import {Component, OnInit, ChangeDetectionStrategy, Inject, ChangeDetectorRef} from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {BranchService} from '../../../../../../core/service/settings/branch.service';
import {SweetalertService} from '../../../../../../core/service/sweetalert/sweetalert';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
import {catchError, tap} from 'rxjs/operators';
import {throwError} from 'rxjs';
import {TestingFieldService} from '../../../../../../core/service/settings/testing-field.service';
import { makeRandom } from 'src/app/shared/util/func';

@Component({
    selector: 'app-testing-field-dialog',
    templateUrl: './testing-field-dialog.component.html',
    styleUrls: ['./testing-field-dialog.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class TestingFieldDialogComponent implements OnInit {
    form;
    statusMock = [{id: 1, name: 'ใช้งาน'}, {id: 2, name: 'ยกเลิก'}];

    regionMock = [{id: 1, name: 'ภาคทดสอบ 1'}, {id: 2, name: 'ภาคทดสอบ 2'}];

    provinceMock = [{id: 1, name: 'จังหวัด 1 '}, {id: 2, name: 'จังหวัด 2 '}];

    provinceMock2 = [{id: 3, name: 'จังหวัด 3 '}, {id: 4, name: 'จังหวัด 4'}];
    branchList: any = [];
    isEdit: boolean = false
    constructor(
        private fb: FormBuilder,
        private branchservice: BranchService,
        private testingfieldSV: TestingFieldService,
        private swSV: SweetalertService,
        private dialogRef: MatDialogRef<TestingFieldDialogComponent>,
        @Inject(MAT_DIALOG_DATA) public data,
        private cdr: ChangeDetectorRef
    ) {
        this.form = this.fb.group({
            status_id: 1,
            // created_by: null,
            // created_datetime: null,
            // updated_by: null,
            // updated_datetime: null,
            // deleted_by: null,
            // deleted_datetime: null,
            testing_field_uid: null,
            testing_field_name_th: [null,Validators.required],
            testing_field_name_en: [null,Validators.required],
            testing_field_code: [makeRandom()],
            branch_uid: [null,Validators.required],
        });
    }

    ngOnInit(): void {
        this.branchservice.getAllStatus1().pipe(
            tap(x => this.branchList = x),
        ).subscribe();
        if (this.data){
            this.isEdit = true
            this.testingfieldSV.get(this.data).pipe(
                tap(x => this.form.patchValue(x)),
                tap(x => this.cdr.detectChanges())
            ).subscribe();
        }
    }

    save(){
        const form = this.form.getRawValue();
        if (this.data){
            this.testingfieldSV.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();
        } else {
            this.testingfieldSV.add(form).pipe(
                tap(x => this.swSV.saveSuccess()),
                tap(x => this.dialogRef.close()),
                catchError(err => {
                    this.swSV.err();
                    this.dialogRef.close();
                    return throwError(err);
                })
            ).subscribe();
        }
    }

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

    setProvince(selectObject){
        switch (selectObject){
            case 1: {
                this.provinceMock = [{id: 1, name: 'จังหวัด 1 ของภาคทดสอบ 1'}, {id: 2, name: 'จังหวัด 2 ของภาคทดสอบ 1'}];
                break;
            }
            case 2: {
                this.provinceMock = [{id: 3, name: 'จังหวัด 1 ของภาคทดสอบ 2'}, {id: 4, name: 'จังหวัด 2 ของภาคทดสอบ 2'}];
                break;
            }
        }
    }

}
