import {Component, OnInit} from '@angular/core';
import {OverAllService} from '../../overAll/overAll.service';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {NzMessageComponent, NzMessageService} from 'ng-zorro-antd';
import {LayoutService} from '../../layouts/layout.service';
import {LoginService} from '../../shared';
import {Base64} from 'js-base64';
import {LocalStorageService} from 'ngx-webstorage';

@Component({
    selector: 'smart-modify-password',
    templateUrl: './modify-password.component.html',
    styles: []
})
export class ModifyPasswordComponent implements OnInit {

    isVisible = false;
    isOkLoading = false;
    title = '修改密码';
    validateForm: FormGroup;
    reg = new RegExp(/^(?=[a-zA-Z]+\d+)\w{8,20}$/);

    groupid;

    constructor(private fb: FormBuilder, private layoutSer: LayoutService,
                private $localStorage: LocalStorageService,
                private message: NzMessageService, private loginService: LoginService) {
    }

    ngOnInit() {
        this.initForm();
    }

    showModal(): void {
        this.isVisible = true;
    }

    handleOk() {
        for (let i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        if (this.validateForm.invalid) {
            return false;
        }
        if (this.validateForm.value.newPassword != this.validateForm.value.realPassword) {
            this.message.error('两次输入密码不一致');
            return false;
        }

        this.isOkLoading = true;
        this.getOldEncodepassword();
    }

    //获取旧密码加密
    getOldEncodepassword() {
        const data = {
            userName: this.$localStorage.retrieve('userInfo').userName,
            password: Base64.encode(this.validateForm.value.oldPassword)
        };
        this.layoutSer.encodePassword(data).subscribe(
            (res) => {
                this.validateForm.patchValue({oldPassword: res.data});
                this.getNewEncodepassword();
            }
        );
    }

    //获取新密码加密
    getNewEncodepassword() {
        const data = {
            userName: this.$localStorage.retrieve('userInfo').userName,
            password: Base64.encode(this.validateForm.value.realPassword)
        };
        this.layoutSer.encodePassword(data).subscribe(
            (res) => {
                this.validateForm.patchValue({realPassword: res.data});
                this.validateForm.patchValue({newPassword: res.data});
                this.update();
            }
        );
    }

    update() {
        let formData = new FormData();
        formData.append('newPassword', this.validateForm.value.realPassword);
        formData.append('oldPassword', this.validateForm.value.oldPassword);
        this.layoutSer.modifyPassword(formData).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('修改成功');
                    this.isVisible = false;
                    this.loginService.logout();
                    this.initForm();
                }
                this.isOkLoading = false;
            }
        );
    }

    handleCancel(): void {
        this.isVisible = false;
        this.isOkLoading = false;
        this.initForm();
    }

    //初始化
    initForm() {
        this.validateForm = this.fb.group({
            oldPassword: [null, [Validators.required]],
            newPassword: [null, [Validators.required, Validators.pattern(/^(?=[a-zA-Z]+\d+)\w{8,20}$/)]],
            realPassword: [null, [Validators.required, Validators.pattern(/^(?=[a-zA-Z]+\d+)\w{8,20}$/)]],
        });
    }
}
