package controllers

import (
	"boss/internal/datas"
	"boss/internal/models/user"
	"boss/internal/service"
	"boss/internal/utils/mfa"
	"strings"
)

type DeleteController struct {
	BaseController
}

func (c *DeleteController) Finish() {
	ctx := c.Ctx.Request.Context()
	se := new(service.DeleteService)
	se.Finish(ctx)
}

func (c *DeleteController) DeleteMenu() {
	ctx := c.Ctx.Request.Context()
	menuUid := c.GetString("menuUid")
	se := new(service.DeleteService)
	dataJSON := se.DeleteMenu(ctx, menuUid, c.GetSession("userID").(string))

	c.Data["json"] = dataJSON
	_ = c.ServeJSONP()
}

func (c *DeleteController) DeleteSecondMenu() {
	ctx := c.Ctx.Request.Context()
	secondMenuUid := strings.TrimSpace(c.GetString("secondMenuUid"))
	se := new(service.DeleteService)
	dataJSON := se.DeleteSecondMenu(ctx, secondMenuUid)

	c.Data["json"] = dataJSON
	_ = c.ServeJSON()
}

/*
* 删除权限项
 */
func (c *DeleteController) DeletePowerItem() {
	ctx := c.Ctx.Request.Context()
	powerID := strings.TrimSpace(c.GetString("powerID"))
	se := new(service.DeleteService)
	dataJSON := se.DeletePowerItem(ctx, powerID)
	c.GenerateJSON(dataJSON)
}

/*
* 删除角色
 */
func (c *DeleteController) DeleteRole() {
	ctx := c.Ctx.Request.Context()
	roleUid := strings.TrimSpace(c.GetString("roleUid"))
	se := new(service.DeleteService)
	dataJSON := se.DeleteRole(ctx, roleUid)
	c.GenerateJSON(dataJSON)
}

/*
* 删除操作员
 */
func (c *DeleteController) DeleteOperator() {
	ctx := c.Ctx.Request.Context()
	userId := strings.TrimSpace(c.GetString("userId"))
	se := new(service.DeleteService)
	dataJSON := se.DeleteOperator(ctx, userId)

	c.GenerateJSON(dataJSON)
}

func (c *DeleteController) DeleteBankCardRecord() {
	ctx := c.Ctx.Request.Context()
	uid := strings.TrimSpace(c.GetString("uid"))
	se := new(service.DeleteService)

	dataJSON := se.DeleteBankCardRecord(ctx, uid)

	c.GenerateJSON(dataJSON)
}

/*
* 删除通道操作
 */
func (c *DeleteController) DeleteRoad() {
	ctx := c.Ctx.Request.Context()
	roadUid := strings.TrimSpace(c.GetString("roadUid"))

	se := new(service.DeleteService)
	dataJSON := se.DeleteRoad(ctx, roadUid)

	c.GenerateJSON(dataJSON)
}

// DeleteRoadPool 删除通道池
func (c *DeleteController) DeleteRoadPool() {
	ctx := c.Ctx.Request.Context()
	roadPoolCode := strings.TrimSpace(c.GetString("roadPoolCode"))

	se := new(service.DeleteService)
	dataJSON := se.DeleteRoadPool(ctx, roadPoolCode)

	c.GenerateJSON(dataJSON)
}

// DeleteMerchant 删除商户
func (c *DeleteController) DeleteMerchant() {
	ctx := c.Ctx.Request.Context()
	merchantUid := strings.TrimSpace(c.GetString("merchantUid"))

	se := new(service.DeleteService)
	keyDataJSON := se.DeleteMerchant(ctx, merchantUid)

	c.GenerateJSON(keyDataJSON)
}

// DeleteAccount 删除账户
func (c *DeleteController) DeleteAccount() {
	ctx := c.Ctx.Request.Context()
	accountUid := strings.TrimSpace(c.GetString("accountUid"))
	totpCode := strings.TrimSpace(c.GetString("totpCode"))

	userID := c.GetSession("userID").(string)

	userInfo := user.GetUserInfoByUserID(ctx, userID)
	dataJSON := new(datas.BaseDataJSON)

	if userInfo.OtpSecret != "" && !mfa.ValidCode(totpCode, userInfo.OtpSecret) {
		dataJSON.Code = -1
		dataJSON.Msg = "二次验证失败"

		c.GenerateJSON(dataJSON)
		return
	}

	se := new(service.DeleteService)
	dataJSON = se.DeleteAccount(ctx, accountUid)

	c.GenerateJSON(dataJSON)
}

func (c *DeleteController) DeleteAgent() {
	ctx := c.Ctx.Request.Context()
	agentUid := strings.TrimSpace(c.GetString("agentUid"))

	se := new(service.DeleteService)
	keyDataJSON := se.DeleteAgent(ctx, agentUid)

	c.GenerateJSON(keyDataJSON)
}

func (c *DeleteController) DeleteAgentRelation() {
	ctx := c.Ctx.Request.Context()
	merchantUid := strings.TrimSpace(c.GetString("merchantUid"))

	se := new(service.DeleteService)

	keyDataJSON := se.DeleteAgentRelation(ctx, merchantUid)

	c.GenerateJSON(keyDataJSON)
}
