package apis

import (
	"fmt"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"

	"go-admin/app/operate/models"
	"go-admin/app/operate/service"
	"go-admin/app/operate/service/dto"
	"go-admin/common/actions"
)

type OrgTeamClub struct {
	api.Api
}

// Get <球队>获取俱乐部详情
// @Summary <球队>获取俱乐部详情
// @Description <球队>获取俱乐部详情
// @Tags <球队>俱乐部
// @Param id path string false "id"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /api/v1/org-team-club [get]
// @Security Bearer
func (e OrgTeamClub) Get(c *gin.Context) {
	req := dto.OrgClubGetReq{}
	s := service.OrgClub{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	sOrgTeamUser := service.OrgTeamUser{}
	e.MakeContext(c).MakeOrm().MakeService(&sOrgTeamUser.Service)
	_, teamId := sOrgTeamUser.GetTeamId(user.GetUserId(c))

	_, clubId := sOrgTeamUser.GetClubId(teamId)

	var object models.OrgClub

	p := actions.GetPermissionFromContext(c)

	req.Id, _ = strconv.Atoi(clubId)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取俱乐部失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// Update <球队>修改俱乐部
// @Summary <球队>修改俱乐部
// @Description <球队>修改俱乐部
// @Tags <球队>俱乐部
// @Accept application/json
// @Product application/json
// @Param data body dto.OrgClubUpdateReq true "body"
// @Success 200 {string} string  	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/org-team-club/{id} [put]
// @Security Bearer
func (e OrgTeamClub) Update(c *gin.Context) {
	req := dto.OrgClubUpdateReq{}
	s := service.OrgClub{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Update(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改俱乐部 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}
