package backstage_api

import (
	"fmt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"net/http"
	"strconv"
	"youngmini_server/app/model/backstage_model"
	"youngmini_server/app/service/backstage_service"
	"youngmini_server/library/response"
)

var Login = loginApi{}

type loginApi struct {
}

// Refresh 用于前台页面刷新
func (account *loginApi) Refresh(r *ghttp.Request) {
	response.JsonExit(r, http.StatusForbidden, "页面刷新成功")
}

// Creat 创建用户
func (account *loginApi) Creat(r *ghttp.Request) {
	var apiReq *backstage_model.CreatUser
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.User.CreatUser(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "用户创建成功")
}

// Account 账号登录
func (account *loginApi) Account(r *ghttp.Request) {
	var apiReq *backstage_model.SysUsersServiceSignReq
	// 用户名密码判空校验
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	// 判断用户名存在，密码是否正确
	user, err := backstage_service.User.GetUserInfo(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	createToken, err := backstage_service.Token.TokenCreate(apiReq, nil, r)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	// 返回登录成功状态码，并且携带token返回
	response.JsonExit(r, http.StatusOK, "登录成功", g.Map{
		"createToken": createToken,
		"userName":    user.UserName,
		"userCode":    user.User,
	})
}

// LeaveAccount 用户注销
func (account *loginApi) LeaveAccount(r *ghttp.Request) {
	authorization := r.Header.Get("Authorization")
	_, err := g.Redis().Do("DEL", authorization)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "注销成功")
}

// FindAccount 查询账号是否在数据库中存在
func (account *loginApi) FindAccount(r *ghttp.Request) {
	user := r.Get("user")
	str := fmt.Sprintf("%v", user)
	if str == "" || str == "<nil>" {
		response.JsonExit(r, http.StatusBadRequest, "请输入用户账号")
	}
	email, err := backstage_service.User.CheckName(str)
	if err != nil {
		// 没有这个用户为501
		response.JsonExit(r, http.StatusNotImplemented, err.Error())
	}
	response.JsonExit(r, http.StatusOK, email)
}

// SetEmail 发送邮箱验证码。
func (account *loginApi) SetEmail(r *ghttp.Request) {
	user := r.Get("user")
	str := fmt.Sprintf("%v", user)
	if str == "" || str == "<nil>" {
		response.JsonExit(r, http.StatusBadRequest, "请输入用户账号")
	}
	email, err := backstage_service.User.CheckName(str)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	// 此处调用邮箱发送验证码email
	err = backstage_service.User.SetEmail(email, str)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, "邮箱发送失败，请稍后重试")
	}
	response.JsonExit(r, http.StatusOK, "邮件发送成功，请查看邮箱")
}

// CheckVerification 修改密码验证通过进行密码修改。并且修改密码
func (account *loginApi) CheckVerification(r *ghttp.Request) {
	var apiReq *backstage_model.ChangePassword
	// 用户名，密码，验证码判空校验
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	// 验证用户验证码是否正确
	err := backstage_service.User.CheckVerification(apiReq.Verification, apiReq.User)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	bool := backstage_service.User.ChangePassword(apiReq.User, apiReq.Password)
	if !bool {
		response.JsonExit(r, http.StatusInternalServerError, "密码修改失败，请稍后重试")
	}
	response.JsonExit(r, http.StatusOK, "密码修改成功")
}

// FindAllNumber  查询用户总数
func (account *loginApi) FindAllNumber(r *ghttp.Request) {
	num := backstage_service.User.FindAll()
	response.JsonExit(r, http.StatusOK, strconv.Itoa(num))
}

// FindAll  账户查询所有信息接口
func (account *loginApi) FindAll(r *ghttp.Request) {
	var apiReq *backstage_model.UserFind
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	result, num, err := backstage_service.User.FindAllUser(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功", g.Map{
		"result": result, "num": num,
	})
}

// Update 更新用户
func (account *loginApi) Update(r *ghttp.Request) {
	var apiReq *backstage_model.UpdateUser
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	err := backstage_service.User.UpdateUser(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "用户更新成功")
}

// ChangeStatus  用户状态,禁用，正常
func (account *loginApi) ChangeStatus(r *ghttp.Request) {
	var apiReq *backstage_model.ChangeStatus
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.User.ChangeStatus(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "0")
}

// ChangeListStatus 批量禁用用户
func (account *loginApi) ChangeListStatus(r *ghttp.Request) {
	var apiReq *backstage_model.ChangeListStatus
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.User.ChangeListStatus(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "0")
}
