import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {Router} from '@angular/router';
import {GlobalService} from '../../../classes/global.service';
import {AuthService} from '../../../classes/auth.service';
import {Dialog} from '../../../classes/dialog';

@Component({
    selector: 'app-new-password',
    templateUrl: './new-password.component.html',
    styleUrls: ['./new-password.component.scss']
})
export class NewPasswordComponent implements OnInit {
    public loading = false;
    passwordCtrl = new FormControl('', [Validators.required]);
    confirmPassCtrl = new FormControl('', [Validators.required]);
    public form: FormGroup;

    constructor(private fb: FormBuilder,
                private router: Router,
                private global: GlobalService,
                private auth: AuthService,
                private dialog: Dialog,
    ) {
    }

    ngOnInit() {
        this.form = this.fb.group({
            passwordCtrl: this.passwordCtrl,
            confirmPassCtrl: this.confirmPassCtrl,
        });
    }

    onSubmit() {
        this.loading = true;
        if (this.form.controls['passwordCtrl'].value == this.form.controls['confirmPassCtrl'].value) {
                this.auth.forgotPass4( this.global.emailData , this.global.smstoken , this.form.controls['passwordCtrl'].value  , '1')
                    .subscribe(
                        loggedIn => {
                            console.log('login' , loggedIn)
                            if (loggedIn) {
                                this.loading = false;
                                this.router.navigateByUrl('');
                            }

                        }, (error) => {
                            this.loading = false;
                            this.dialog.openForAlert('رمز عبور جدید را مجددا صحیح وارد نمایید.');
                        });
            }
        }

}

