/*
 * @description:登陆相关数据操作
 * @Author: CP
 * @Date: 2020-09-02 09:56:28
 * @FilePath: \construction_management\services\login_service.go
 */
package services

import (
	"log"
	"unicode"

	"github.com/kataras/iris/v12"
	"go.mod/dao"
	"go.mod/datasource"
	"go.mod/web/viewmodels"
)

//定义项目用户Service接口
type LoginService interface {
	ValidLogin(ctx iris.Context) error
}

//返回service操作类
type loginService struct {
	dao *dao.ProjectAccountDao
}

//创建项目用户service
func NewLoginService() LoginService {
	return &loginService{
		dao: dao.NewProjectAccountDao(datasource.InstanceDbMaster()),
	}
}

func Ucfirst(str string) string {
	for i, v := range str {
		return string(unicode.ToUpper(v)) + str[i+1:]
	}
	return ""
}

// 登陆验证
func (s *loginService) ValidLogin(ctx iris.Context) error {
	loginVaild := viewmodels.Login{}
	err := ctx.ReadForm(&loginVaild)
	if err != nil {
		log.Println("ReadForm转换异常, error=", err)
		return err
	}

	err = loginVaild.Validate()
	if err != nil {
		log.Println("登录验证, error=", err)
		return err
	}
	return nil
}

//fmt.Println(ctx.FormValues())

// list := ctx.FormValues()
// loginVaild := viewmodels.Login{}
// v := reflect.ValueOf(&loginVaild).Elem()
// for path, values := range list {
// 	//fmt.Println(path)
// 	//fmt.Println(values)
// 	if path != "csrf.Token" {

// 		v.FieldByName(Ucfirst(path)).Set(reflect.ValueOf(values[0]))

// 	}
// }
//fmt.Println(loginVaild)
// loginVaild := viewmodels.Login{
// 	Code:     ctx.FormValue("code"),
// 	Account:  ctx.FormValue("account"),
// 	Password: ctx.FormValue("password"),
// }
