(function() {
	Vue.component('change-password', {
		template: '<div id="password" class="pl30 hidden">\
						<div class="pb10 c-border-b c-f20">修改密码\</div>\
						<form id="passwordForm" class="" onsubmit="return false">\
							<div class="w3-row mt25 form-group">\
								<div class="w3-col c-f16 m5 c-t-right c-909090" style="width:80px;">旧密码</div>\
								<div class="w3-rest pl15">\
									<input  id="oldpassword" type="password" class="form-control" style="width:310px;" name="oldpassword" autocomplete="off"  maxlength="16"  minlength="8" v-model="oldPassword">\
								</div>\
							</div>\
							<div class="w3-row mt25 form-group">\
								<div class="w3-col c-f16 m5 c-t-right c-909090" style="width:80px;">新密码</div>\
								<div class="w3-rest pl15">\
									<input id="newpassword" type="password" class="form-control" style="width:310px;" name="newpassword" autocomplete="off"  maxlength="16"  minlength="8" v-model="newPassword">\
									<p class="remark">8-16位，不允许空格</p>\
								</div>\
							</div>\
							<div class="w3-row mt25 form-group">\
								<div class="w3-col c-f16 m5 c-t-right c-909090" style="width:80px;">确认密码</div>\
								<div class="w3-rest pl15">\
									<input  id="confirm_password" type="password" class="form-control" style="width:310px;" name="confirm_password" autocomplete="off"  maxlength="16"  minlength="8" v-model="confirmPassword">\
								</div>\
							</div>\
							<div class="c-t-center mt50">\
								<button type="submit" class="btn btn-theme c-f16" style="width:144px;height: 50px;" @click="changePassword()">确 定</button>\
								<button type="button" class="btn btn-default c-f16 ml20" style="width:144px;height: 50px;" @click="gotosetting()">取 消</button>\
							</div>\
						</form>\
					</div>',
		props: [],
		data: function() {
			return {
				oldPassword:"",
				newPassword:"",
				confirmPassword:"",				
			}
		},
		mounted: function() {

		},
		methods: {
			gotosetting: function() {
				$("#password").addClass("hidden")
				$("#security").removeClass("hidden")
			},
			changePassword:function(){
				var vm = this
				var flag = $("#passwordForm").valid();
				if(!flag) {
					//没有通过验证
					return;
				}
				var oauthInfo = JSON.parse(sessionStorage.getItem("oauthInfo"));
				mineAPI.checkOldPass({
					password:this.oldPassword
				}).then(function(res){
					mineAPI.passwordReset({
						userId: oauthInfo.id,
						password: vm.confirmPassword
					}).then(function(res) {
						if(res) {
							toastr.success("密码修改成功，请重新登录系统");							
							vm.existUser();
						} else {
							toastr.error("密码修改失败");
						}
					}).catch(function(err){
						toastr.error("密码修改失败");
					})
				}).catch(function(err){
					toastr.error("原密码错误！");
				})			
			},
			existUser: function() {
				sessionStorage.removeItem("oauthInfo");
				sessionStorage.removeItem("userAgent");
				window.location.href="../../../page/login/html/login.html"
			}
		},
		watch: {
			oldPassword: function(newVal, oldVal) {
				var val = newVal.toString();
				var reg = /\s+/g; //去除空格
				if(reg.test(val)) {
					this.oldPassword = oldVal;
				}
			},
			newPassword: function(newVal, oldVal) {
				var val = newVal.toString();
				var reg = /\s+/g; //去除空格
				if(reg.test(val)) {
					this.newPassword = oldVal;
				}
			},
			confirmPassword: function(newVal, oldVal) {
				var val = newVal.toString();
				var reg = /\s+/g; //去除空格
				if(reg.test(val)) {
					this.confirmPassword = oldVal;
				}
			}

		}
	})

})()