// Package controller 控制层
package controller

import (
	"server/common"
	"server/modules/yx/entity"
	"server/modules/yx/service"
	"strconv"

	"github.com/gin-gonic/gin"
)

type YxVolunteerRecordController struct {
	service *service.YxVolunteerRecordService
}

func NewYxVolunteerRecordController() *YxVolunteerRecordController {
	return &YxVolunteerRecordController{service: service.NewYxVolunteerRecordService()}
}

// RegisterRoutes 注册路由
func (ctrl *YxVolunteerRecordController) RegisterRoutes(rg *gin.RouterGroup) {
	group := rg.Group("/yx-volunteer-records")
	{
		group.GET("", ctrl.List)
		group.GET("/:id", ctrl.Get)
		group.POST("", ctrl.Create)
		group.PUT("/:id", ctrl.Update)
		group.DELETE("/:id", ctrl.Delete)
	}
}

// List 获取志愿明细列表
// @Summary      获取志愿明细列表
// @Tags         志愿明细
// @Param        page  query  int  false  "页码"  default(1)
// @Param        size  query  int  false  "每页数量"  default(10)
// @Success      200  {object}  common.Response
// @Router       /yx-volunteer-records [get]
func (ctrl *YxVolunteerRecordController) List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
	items, total, err := ctrl.service.List(page, size)
	if err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	common.Success(c, gin.H{
		"items": items,
		"total": total,
	})
}

// Get 获取单个志愿明细
// @Summary      获取单个志愿明细
// @Tags         志愿明细
// @Param        id    path  string  true  "ID"
// @Success      200  {object}  common.Response
// @Router       /yx-volunteer-records/{id} [get]
func (ctrl *YxVolunteerRecordController) Get(c *gin.Context) {
	id := c.Param("id")
	item, err := ctrl.service.GetByID(id)
	if err != nil {
		common.Error(c, 404, "未找到记录")
		return
	}
	common.Success(c, item)
}

// Create 创建志愿明细
// @Summary      创建志愿明细
// @Tags         志愿明细
// @Param        item  body  entity.YxVolunteerRecord  true  "志愿明细信息"
// @Success      200  {object}  common.Response
// @Router       /yx-volunteer-records [post]
func (ctrl *YxVolunteerRecordController) Create(c *gin.Context) {
	var item entity.YxVolunteerRecord
	if err := c.ShouldBindJSON(&item); err != nil {
		common.Error(c, 400, "参数错误")
		return
	}
	if err := ctrl.service.Create(&item); err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	common.Success(c, item)
}

// Update 更新志愿明细
// @Summary      更新志愿明细
// @Tags         志愿明细
// @Param        id    path  string  true  "ID"
// @Param        item  body  entity.YxVolunteerRecord  true  "志愿明细信息"
// @Success      200  {object}  common.Response
// @Router       /yx-volunteer-records/{id} [put]
func (ctrl *YxVolunteerRecordController) Update(c *gin.Context) {
	var item entity.YxVolunteerRecord
	if err := c.ShouldBindJSON(&item); err != nil {
		common.Error(c, 400, "参数错误")
		return
	}
	item.ID = c.Param("id")
	if err := ctrl.service.Update(&item); err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	common.Success(c, item)
}

// Delete 删除志愿明细
// @Summary      删除志愿明细
// @Tags         志愿明细
// @Param        id    path  string  true  "ID"
// @Success      200  {object}  common.Response
// @Router       /yx-volunteer-records/{id} [delete]
func (ctrl *YxVolunteerRecordController) Delete(c *gin.Context) {
	id := c.Param("id")
	if err := ctrl.service.Delete(id); err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	common.Success(c, nil)
}
