package controllers

import (
	"boss/internal/datas"
	"boss/internal/service"
	"boss/internal/utils/valid"
)

// UpdateMerchantPassword 更新密码
func (c *UpdateController) UpdateMerchantPassword() {
	merchantUid := c.GetString("merchantUid")
	newPassword := c.GetString("newPassword")
	totpCode := c.GetString("totpCode")
	userID, ok := c.GetSession("userID").(string)
	dataJSON := new(datas.KeyDataJSON)
	if !ok || userID == "" {
		dataJSON.Code = 404
		dataJSON.Msg = "请重新登录!"
		c.GenerateJSON(dataJSON)
		return
	}
	// 判断totp是不是正确
	if service.ValidTotp(userID, totpCode) {
		dataJSON.Code = -1
		dataJSON.Msg = "二次验证错误"
		c.GenerateJSON(dataJSON)
		return
	}
	if len(newPassword) <= 8 {
		dataJSON.Code = -1
		dataJSON.Msg = "新密码长度必须大于等于8个字符!"
	} else if len(newPassword) > 16 {
		dataJSON.Code = -1
		dataJSON.Msg = "新密码长度不能大于16个字符!"
	} else if valid.HaveAlphaNumeric(newPassword) {
		dataJSON.Code = -1
		dataJSON.Msg = "新密码必须有数字和字母组成!"
	} else {
		dataJSON.Code = 200
		dataJSON.Msg = "密码修改成功!"
		service.UpdateMerchantPassword(merchantUid, totpCode)
	}
	c.GenerateJSON(dataJSON)
}
