/*
个人信息页面
*/
func (this *AdminController) Profile() {
	//判断是管理员还是普通用户
	sessionuid := this.GetSession("sessionuid")
	// cookieaccount := this.Ctx.GetCookie("cookieaccount")
	sessionaid := this.GetSession("sessionaid")
	cookieadmin := this.Ctx.GetCookie("cookieadmin")
	//管理员
	if sessionaid != nil {
		userinfo, err := models.Admininfowithaid(sessionaid)
		if err != nil {
			return
		}
		this.Data["userinfo"] = userinfo
		this.Data["user"] = false
	} else if len(cookieadmin) > 0 {

	} else if sessionuid != nil {
		//普通用户
		userinfo, err := models.Userinfowithuid(sessionuid)
		if err != nil {
			return
		}
		this.Data["userinfo"] = userinfo
		this.Data["user"] = true
	} else {

	}
	this.TplNames = "account/profile.html"
}
/*
修改个人密码
*/
func (this *AdminController) Modifypassword() {
	style, _ := this.GetInt32("type")
	oldpassword := this.Input().Get("password")
	newpassword := this.Input().Get("newpassword")
	newpassword2 := this.Input().Get("newpassword2")

	if style == 0 {
		//普通用户修改密码
		userid := this.GetSession("sessionuid")
		password := md5.New()
		salt1 := beego.AppConfig.String("salt1")
		salt2 := beego.AppConfig.String("salt2")
		io.WriteString(password, salt1)
		io.WriteString(password, oldpassword)
		io.WriteString(password, salt2)
		testpassword := fmt.Sprintf("%x", password.Sum(nil))
		//比较加密后的旧密码和数据库中密码是否一致
		userinfo, _ := models.Userinfowithuid(userid)
		if oldpassword == "" {
			this.Data["errmsg"] = "当前密码不能为空!"
			this.Profile()
		} else if newpassword == "" {
			this.Data["errmsg"] = "新密码不能为空!"
			this.Profile()
		} else if newpassword2 == "" {
			this.Data["errmsg"] = "请确认密码!"
			this.Profile()
		} else if userinfo.Password != testpassword {
			this.Data["errmsg"] = "原密码不正确!"
			this.Profile()
		} else if newpassword != newpassword2 {
			this.Data["errmsg"] = "两次输入的新密码不相同!"
			this.Profile()

		} else {
			//加密
			password1 := md5.New()
			io.WriteString(password1, salt1)
			io.WriteString(password1, newpassword)
			io.WriteString(password1, salt2)
			password1final := fmt.Sprintf("%x", password1.Sum(nil))
			err := models.Updateuserpassword(userid, password1final)
			if err != nil {
				return
			}
			this.Data["errmsg"] = "修改成功!"
		}
		this.Profile()
		// this.Redirect("/admin/modifypassword", 301)
	} else {
		//管理员修改密码
		adminid := this.GetSession("sessionaid")
		password := md5.New()
		salt1 := beego.AppConfig.String("salt1")
		salt2 := beego.AppConfig.String("salt2")
		io.WriteString(password, salt1)
		io.WriteString(password, oldpassword)
		io.WriteString(password, salt2)
		testpassword := fmt.Sprintf("%x", password.Sum(nil))
		//比较加密后的旧密码和数据库中密码是否一致
		userinfo, _ := models.Admininfowithaid(adminid)
		if oldpassword == "" {
			this.Data["errmsg"] = "当前密码不能为空!"
			this.Profile()
		} else if newpassword == "" {
			this.Data["errmsg"] = "新密码不能为空!"
			this.Profile()
		} else if newpassword2 == "" {
			this.Data["errmsg"] = "请确认密码!"
			this.Profile()
		} else if userinfo.Password != testpassword {
			this.Data["errmsg"] = "原密码不正确!"
			this.Profile()
		} else if newpassword != newpassword2 {
			this.Data["errmsg"] = "两次输入的新密码不相同!"
			this.Profile()

		} else {
			//加密
			password1 := md5.New()
			io.WriteString(password1, salt1)
			io.WriteString(password1, newpassword)
			io.WriteString(password1, salt2)
			password1final := fmt.Sprintf("%x", password1.Sum(nil))
			err := models.Updateadminpassword(adminid, password1final)
			if err != nil {
				return
			}
			this.Data["errmsg"] = "修改成功!"
		}
		this.Profile()

	}
}