package new_mobile_api_controllers

import (
	"Xcx_New/enums"
	"Xcx_New/models"
	"Xcx_New/service"
	"Xcx_New/utils"
	"fmt"
	"github.com/jinzhu/gorm"
	"time"
)

type NewDialysisApiController struct {
	NewMobileBaseAPIAuthController
}

func (this *NewDialysisApiController) GetCodeInit() {
	redisClient := service.RedisClient()
	defer redisClient.Close()
	req := this.Ctx.Request
	addr := utils.GetIP(req)
	cur_time := time.Now().Format("2006-01-02")
	_, err := redisClient.Get("ip:host_" + cur_time + "_" + addr).Result()
	if err != nil {
		redisClient.Set("ip:host_"+cur_time+"_"+addr, 0, time.Second*24*60*60)
	}
	//将客户端的ip加密传给前端，作为短信验证的密钥，来验证短信发送的IP地址
	aespass := utils.AESEncrypt(addr)

	fmt.Println("hhhhhhh3223323232332", aespass)
	this.ServeSuccessJSON(map[string]interface{}{
		"aespass": aespass,
	})

}

func (this *NewDialysisApiController) GetUserRegister() {

	//用户绑定
	name := this.GetString("name")
	id_card_no := this.GetString("id_card_no")
	mobile := this.GetString("mobile")
	code := this.GetString("code")

	role := models.XcxAdminUserRole{
		PatientName: name,
		IdCardNo:    id_card_no,
		Mobile:      mobile,
		Code:        code,
		PatientId:   0,
		UserOrgId:   0,
		Status:      0,
		Ctime:       0,
		Mtime:       0,
		Appid:       "",
		Appsecret:   "",
		SessionKey:  "",
	}

	//查找该电话号码是否存在
	_, errcode := service.GetXcxMobileInformation(mobile)
	if errcode == gorm.ErrRecordNotFound {

		err := service.CreateXcxAdminUser(role)
		if err == nil {
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
			return
		}
		this.ServeSuccessJSON(map[string]interface{}{
			"role": role,
		})
	} else if errcode == nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}

	fmt.Println("roler", role)
}

func (this *NewDialysisApiController) GetCodeInfo() {

	mobile := this.GetString("phone")
	aespass := this.GetString("aespass")
	utils.TraceLog("mobile:%v aespass:%v", mobile, aespass)
	if utils.CellPhoneRegexp().MatchString(mobile) == false {
		this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeMobileFormat)
		this.ServeJSON()
		return
	}

	adminUser, _ := service.GetValidAdminUserByMobileReturnErr(mobile)
	if adminUser != nil {
		this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRegisterExist)
		this.ServeJSON()
		return
	}

	if err := service.SendVerificationCodeSMS(mobile, aespass); err != nil {
		this.Data["json"] = enums.MakeFailResponseJSON(err.Error(), 600)
		this.ServeJSON()
	} else {
		this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
			"msg": "短信发送成功,有效期为10分钟",
		})
		this.ServeJSON()
	}
}

func (this *NewDialysisApiController) GetLoginInfor() {
	fmt.Println("c出啊大发阿方阿道夫发 阿方阿")
	//mobile := this.GetString("mobile")
	//fmt.Println(mobile)
	role, err := service.GetLoginInfor("18923081560")

	fmt.Println("err2332323232", err)
	if err == nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}

	this.ServeSuccessJSON(map[string]interface{}{
		"role": role,
	})
}
