/*
 * @description:用户相关
 * @Author: CP
 * @Date: 2020-08-27 17:03:34
 * @FilePath: \construction_management\web\controllers\account.go
 */
package controllers

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"go.mod/comm"
	"go.mod/services"
	"go.mod/web/utils"
)

type AccountController struct {
	//框架-web应用上下文环境
	Ctx iris.Context
	// 需要用的service
	ServiceProjectAccount services.ProjectAccountService
}

// 用户首页
func (c *AccountController) Get() mvc.Result {
	ErrMsg := c.Ctx.URLParam("errMsg")
	Success := c.Ctx.URLParam("success")

	return mvc.View{
		Name: "account/index.html",
		Data: iris.Map{
			"Title":   "账号资料",
			"Channel": "account",
			"Action":  "/account/save",
			"Menu":    "account",
			"ErrMsg":  ErrMsg,
			"Success": Success,
		},
		Layout: "account/layout.html",
	}
}

// 保存项目管理员信息
func (c *AccountController) PostSave() {
	ErrMsg := ""
	// 验证内容
	AccountData, err := c.ServiceProjectAccount.ValidRule(c.Ctx)
	if err != nil {
		ErrMsg = utils.FormValidError(err)
		comm.Redirect(c.Ctx.ResponseWriter(), "/account?errMsg="+ErrMsg)
	} else {
		// 获得更新用户ID
		id, err := c.Ctx.Values().GetInt("accountId")
		if err != nil {
			comm.Redirect(c.Ctx.ResponseWriter(), "/account?errMsg="+ErrMsg)
		}
		// 保存信息
		err = c.ServiceProjectAccount.Save(AccountData, id, 0)
		if err != nil {
			comm.Redirect(c.Ctx.ResponseWriter(), "/account?errMsg="+ErrMsg)
		}
		comm.Redirect(c.Ctx.ResponseWriter(), "/account?success=修改成功")
	}
}

// 账号安全
func (c *AccountController) GetSafe() mvc.Result {

	return mvc.View{
		Name: "account/safe.html",
		Data: iris.Map{
			"Title":   "账号安全",
			"Channel": "account",
			"Action":  "/account",
			"Menu":    "accountSafe",
		},
		Layout: "account/layout.html",
	}
}
