package system

import (
	"github.com/gin-gonic/gin"
	"miniapp/model/common"
	"miniapp/model/common/request"
	r "miniapp/model/common/response"
)

type PosterApi struct {
}

// GetPosterList 获取海报列表
func (p PosterApi) GetPosterList(ctx *gin.Context) {
	var page request.PageInfo
	if err := ctx.ShouldBind(&p); err != nil {
		r.FailWithMessage("参数错误"+err.Error(), ctx)
		return
	}

	list, total, err := posterService.GetPosterList(page)
	if err != nil {
		r.FailWithMessage("获取海报列表失败", ctx)
		return
	}

	r.OkWithDetailed(r.PageResult{List: list, Total: total, Page: page.Page, PageSize: page.PageSize}, "获取成功", ctx)

}

// CreatePoster 创建海报
func (p PosterApi) CreatePoster(ctx *gin.Context) {
	var poster common.Poster
	if err := ctx.ShouldBindJSON(&poster); err != nil {
		r.FailWithMessage("参数错误"+err.Error(), ctx)
		return
	}

	err := posterService.AddPoster(&poster)
	if err != nil {
		r.FailWithMessage("创建失败"+err.Error(), ctx)
		return
	}

	r.OkWithMessage("创建成功", ctx)
}

// UpdatePoster 更新海报
func (p PosterApi) UpdatePoster(ctx *gin.Context) {
	var poster common.Poster
	if err := ctx.ShouldBindJSON(&poster); err != nil {
		r.FailWithMessage("参数错误"+err.Error(), ctx)
		return
	}

	err := posterService.UpdatePoster(&poster)
	if err != nil {
		r.FailWithMessage("更新失败"+err.Error(), ctx)
		return
	}

	r.OkWithMessage("更新成功", ctx)
}

// DeletePoster 删除海报
func (p PosterApi) DeletePoster(ctx *gin.Context) {
	var poster common.Poster
	if err := ctx.ShouldBind(&poster); err != nil {
		r.FailWithMessage("参数错误"+err.Error(), ctx)
		return
	}

	err := posterService.DeletePoster(&poster)
	if err != nil {
		r.FailWithMessage("删除失败"+err.Error(), ctx)
		return
	}

	r.OkWithMessage("删除成功", ctx)
}

// GetPosterById 根据id获取海报
func (p PosterApi) GetPosterById(ctx *gin.Context) {
	id := ctx.Param("id")
	if id == "" {
		r.FailWithMessage("参数错误", ctx)
		return
	}

	poster, err := posterService.GetPosterById(id)
	if err != nil {
		r.FailWithMessage("获取失败"+err.Error(), ctx)
		return
	}

	r.OkWithDetailed(poster, "获取成功", ctx)
}

// UpdatePosterSort 更新海报排序
func (p PosterApi) UpdatePosterSort(ctx *gin.Context) {
	var poster common.Poster
	if err := ctx.ShouldBind(&poster); err != nil {
		r.FailWithMessage("参数错误"+err.Error(), ctx)
		return
	}

	err := posterService.UpdatePoster(&poster)
	if err != nil {
		r.FailWithMessage("更新失败"+err.Error(), ctx)
		return
	}

	r.OkWithMessage("更新成功", ctx)
}
