import {Component} from '@angular/core';
import {App, IonicPage, NavController, NavParams} from 'ionic-angular';
import {CommonService} from "../../../core/common.service";
import {MineService} from "../mine.service";
import {LoginPage} from "../../login/login";

@Component({
    selector: 'page-change-password',
    templateUrl: 'change-password.html',
})
export class ChangePasswordPage {
    myreg = /^(?=[a-zA-Z]+\d+)\w{8,20}$/;

    passwordInfo = {
        oldPassword: "",
        newPassword1: "",
        newPassword2: "",
    };

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private app:App,
                private commonSer: CommonService,
                private mineSer:MineService) {
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad ChangePasswordPage');
    }

    sureModifyPassword() {
        if (this.passwordInfo.oldPassword == '' ||
            this.passwordInfo.newPassword1 == '' ||
            this.passwordInfo.newPassword1 == '') {
            this.commonSer.toast('请输入完整信息')
            return;
        }
        // if (!this.myreg.test(this.passwordInfo.newPassword1)) {
        //     this.commonSer.toast('新密码以英文字母开头，同时包含数字，长度8-20位！');
        //     return false;
        // }
        if (this.passwordInfo.newPassword1 != this.passwordInfo.newPassword2) {
            this.commonSer.toast("两次输入的密码不一致");
            return;
        }
        const data = {
            "oldpassword":this.passwordInfo.oldPassword,
            "newpassword":this.passwordInfo.newPassword1,
        };
        this.mineSer.updatePassword(data).subscribe(
            (res)=>{
                if(res.code == 0){
                    this.commonSer.toast('密码修改成功');
                    this.app.getRootNav().setRoot(LoginPage);
                }
            }
        )
    }

}
