/*
 * @description:
 * @Author: CP
 * @Date: 2020-08-28 10:26:02
 * @FilePath: \construction_management\web\controllers\login.go
 */
package controllers

import (
	"fmt"

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

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

// 默认首页
func (c *LoginController) Get() mvc.Result {
	return mvc.Response{
		Path: "/login",
	}
}

//登录页面
func (c *LoginController) GetLogin() mvc.Result {

	// r := c.Ctx.GetReferrer()
	// fmt.Println(r)
	errMsg := c.Ctx.URLParam("err")
	return mvc.View{
		Name: "login/login.html",
		Data: iris.Map{
			"Title":   "用户登录",
			"Channel": "login",
			"Action":  "login",
			"errMsg":  errMsg,
		},
		Layout: "login/layout.html",
	}
}

//登陆
func (c *LoginController) PostLogin() mvc.Result {
	ErrMsg := ""
	//验证规则
	LoginData, err := c.ServiceLogin.ValidRule(c.Ctx)
	if err != nil {
		ErrMsg = utils.FormValidError(err)
	} else {
		//验证登陆用户
		_, err = c.ServiceLogin.ValidProjectAccount(LoginData, c.Ctx.ResponseWriter())
		if err != nil {
			ErrMsg = fmt.Sprintf("%s", err)
		} else {
			//验证通关
			comm.Redirect(c.Ctx.ResponseWriter(), "/bidsection")
		}
	}
	return mvc.View{
		Name: "login/login.html",
		Data: iris.Map{
			"Title":     "用户登录",
			"Channel":   "login",
			"Action":    "login",
			"LoginData": LoginData,
			"ErrMsg":    ErrMsg,
		},
		Layout: "login/layout.html",
	}
}

//自定义请求
func (c *LoginController) BeforeActivation(b mvc.BeforeActivation) {
	anyMiddlewareHere := func(ctx iris.Context) {
		ctx.Application().Logger().Warnf("Inside /custom_path")
		ctx.Next()
	}
	b.Handle("GET", "/something", "MyCustomHandlerc", anyMiddlewareHere)
}

func (c *LoginController) MyCustomHandlerc() string {

	//c.Ctx.Writef("You're welcome mate!")
	//refer := fmt.Sprintf("/login?err=%s", err)
	//refer = fmt.Sprintf("/login?err=%s", msg)
	// rs := make(map[string]interface{})
	// rs["code"] = 0
	// rs["msg"] = ""
	// datalist := c.ServiceGift.GetAll(true)
	// list := make([]models.LtGift, 0)
	// for _, data := range datalist {
	// 	// 正常状态的才需要放进来
	// 	if data.SysStatus == 0 {
	// 		list = append(list, data)
	// 	}
	// }
	// rs["gifts"] = list
	// return rs

	fmt.Println("out")
	return "MyCustomHandler says Hey"
	// c.Ctx.Header("Content-Type", "text/html")
	// return "out"
}
