import { Location } from '@angular/common';
import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef, Input } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { Observable, throwError } from 'rxjs';
import { tap, catchError } from 'rxjs/operators';
import { BaseFormUid } from 'src/app/core/base/base-form-uid';
import { CandidateTypeService } from 'src/app/core/service/enrollment-api/candidate-type.service';
import { DepartmentService } from 'src/app/core/service/settings/department.service';
import { FacultysService } from 'src/app/core/service/settings/facultys.service';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { UserService } from 'src/app/core/service/user/user.service';

@Component({
    selector: 'app-form',
    templateUrl: './form.component.html',
    styleUrls: ['./form.component.scss'],
    // changeDetection: ChangeDetectionStrategy.OnPush
})
export class FormComponent extends BaseFormUid implements OnInit {
    @Input() UsersData: any = [];
    form;
    passwordShowHide = 'password';
    checkPassword;
    userData;
    faculty$ = new Observable()
    candidateType$ = new Observable()
    department$ = new Observable()
    constructor(
        public fb: FormBuilder,
        public router: Router,
        public activeRoute: ActivatedRoute,
        public location: Location,
        private cdr: ChangeDetectorRef,
        private swSV: SweetalertService,
        private userSV: UserService,
        private candidateTypeSV: CandidateTypeService,
        private facultySV: FacultysService,
        private departmentSV: DepartmentService
    ) {
        super(router,activeRoute,fb,location);
        this.form = this.fb.group({
            user_uid: null,
            candidate_type_uid: null,
            examinee_type_uid: null,
            examinee_card: null,
            student_code: null,
            full_name: null,
            faculty_uid: null,
            department_uid: null,
            mobile_phone: null,
            email: null,
            password: null,
            faculty_name_th: null,
            department_name_th: null,
            candidate_type_name_th: null
        });
        // this.checkPassword = this.fb.group({
        //     password: null,
        //     passwordNew: null,
        //     passwordNewCon: null,
        // });
    }

    ngOnInit(): void {
        // this.candidateTypeSV.getAll().pipe(tap(x => this.candidateType = x)).subscribe()
        // this.facultySV.getAll().pipe(tap(x => this.faculty = x)).subscribe()
        // this.departmentSV.getAll().pipe(tap(x => this.department = x)).subscribe()
        this.faculty$ = this.facultySV.getAll()
        this.candidateType$ = this.candidateTypeSV.getAll()
        this.department$ = this.departmentSV.getAll()
        this.userSV.get(this.id).pipe(
            tap(x => this.form.patchValue(x)),
            tap(x => this.cdr.detectChanges())
        ).subscribe()
        this.cdr.detectChanges()
    }

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

    // save(){
    //     const form = this.form.getRawValue()
    //     const pass = this.checkPassword.getRawValue()
    //     if(pass.password != null && pass.passwordNew != null && pass.passwordNewCon != null){
    //         if((form.password == pass.password)&&(pass.passwordNew == pass.passwordNewCon)){
    //             form.password = pass.passwordNew
    //             this.userSV.update2(form).pipe(
    //                 tap(x => this.swSV.updateSuccess('บันทึกข้อมูลผู้สอบเสร็จสิ้น')),
    //                 catchError(err => {
    //                     this.swSV.err()
    //                     return throwError(err)
    //                 })
    //             ).subscribe()
    //         }else{
    //             this.swSV.errText('รหัสผ่านเก่าหรือรหัสผ่านใหม่ไม่ตรงกัน');
    //         }
    //     }else{
    //         this.userSV.update2(form).pipe(
    //             tap(x => this.swSV.updateSuccess('บันทึกข้อมูลผู้สอบเสร็จสิ้น')),
    //             catchError(err => {
    //                 this.swSV.err()
    //                 return throwError(err)
    //             })
    //         ).subscribe()
    //     }
    // }

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

    onShowHidepass(e) {
        if(e.target.checked){
            this.passwordShowHide = 'text';
        }else {
            this.passwordShowHide = 'password';
        }
    }

}
