import {Component, AfterViewInit, Renderer, ElementRef, OnInit} from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { Router } from '@angular/router';
import { JhiEventManager } from 'ng-jhipster';

import { LoginService } from './login.service';
import { StateStorageService } from '../auth/state-storage.service';

import {
    AbstractControl,
    FormBuilder,
    FormGroup,
    Validators
} from '@angular/forms';
import {EmitService} from "../event/eventEmitter";
import {NzNotificationService} from 'ng-zorro-antd';
import {LocalStorageService} from 'ngx-webstorage';

@Component({
    selector: 'jhi-login-modal',
    templateUrl: './login.component.html'
})
export class JhiLoginModalComponent implements OnInit {
    validateForm: FormGroup;

    isSpinning = false;
    authenticationError: boolean;
    credentials: any;

    constructor(
        private fb: FormBuilder,
        private eventManager: JhiEventManager,
        private loginService: LoginService,
        private stateStorageService: StateStorageService,
        private elementRef: ElementRef,
        private renderer: Renderer,
        private router: Router,
        public activeModal: NgbActiveModal,
        public emitService:EmitService,
        public notification:NzNotificationService,
        public $localStorage:LocalStorageService
    ) {
        this.credentials = {};
        this.emitService.eventEmit.subscribe((value: any) => {
            if(value == "logout") {
                this.router.navigate(['app/login']);
            }else{
                // this.token = false;
            }
        });
    }

    submitForm() {
        for(let i in this.validateForm.controls) {
            this.validateForm.controls[ i ].markAsDirty();
            this.validateForm.controls[ i ].updateValueAndValidity();
        }
        if(this.validateForm.invalid){
            return false;
        }
        this.login();
    }

    ngOnInit(): void {
        this.validateForm = this.fb.group({
            loginName: [ null, [ Validators.required ] ],
            password: [ null, [ Validators.required ] ],
            rememberMe: [ true ]
        });
    }

    cancel() {
        this.credentials = {
            loginName: null,
            password: null,
            rememberMe: true
        };
        this.authenticationError = false;
        this.activeModal.dismiss('cancel');
    }

    login() {
        this.isSpinning = true;
        this.loginService.login(this.validateForm.value).then((res) => {
            this.isSpinning = false;
            if(res){
                this.router.navigate(['app/main/basic']);
                this.notification.create('success', '登录成功','',{nzDuration:2000});
                this.$localStorage.store('userInfo', this.validateForm.value);
            }else{
                this.notification.create('error', '登录失败', '用户名或密码错误',{nzDuration:2000});
            }
        }).catch(() => {
            this.authenticationError = true;
        });
    }

    requestResetPassword() {
        this.activeModal.dismiss('to state requestReset');
        this.router.navigate(['/reset', 'request']);
    }
}
