package goodsmanage

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go-admin/app/admin/models"
	"go-admin/logger"
	"go-admin/tools"
	"go-admin/tools/app"
	"net/http"
)

func HomeCarouselList(c *gin.Context) {
	carousel := &models.HomeCarousel{}
	all, err := carousel.List()
	if err != nil {
		logger.Errorf("err:", err)
		app.Error(c, http.StatusInternalServerError, err, "查询失败")
		return
	}
	app.OK(c, all, "")
}

func HomeCarouselAdd(c *gin.Context) {
	m := &models.HomeCarousel{}
	if c.ShouldBindJSON(m) != nil {
		logger.Errorf("para err")
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}
	err := m.Add()
	if err != nil {
		logger.Errorf("err:", err)
		app.Error(c, http.StatusInternalServerError, err, "查询失败")
		return
	}
	app.OK(c, m, "")
}

func HomeCarouselDel(c *gin.Context) {
	req := struct {
		HomeCarouselId uint32 `json:"home_carousel_id"`
	}{}
	if c.ShouldBindJSON(&req) != nil {
		logger.Errorf("para err")
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}
	carousel := &models.HomeCarousel{}
	carousel.ID = req.HomeCarouselId
	err := carousel.Del()
	if err != nil {
		logger.Errorf("err:", err)
		app.Error(c, http.StatusInternalServerError, err, "查询失败")
		return
	}
	app.OK(c, nil, "")
}

func HomeCarouselModify(c *gin.Context) {
	carousel := &models.HomeCarouselModifyReq{}
	if c.ShouldBindJSON(carousel) != nil {
		logger.Errorf("para err")
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}
	err := carousel.Modify()
	if err != nil {
		logger.Errorf("err:", err)
		app.Error(c, http.StatusInternalServerError, err, "查询失败")
		return
	}
	app.OK(c, carousel, "")
}

// HomeCategoryList 首页分类-列表
// @Summary 首页分类-列表
// @Tags 首页分类，V1.4.5
// @Produce json
// @Accept json
// @Param request body models.HomeCategoryListReq true "首页分类-列表模型"
// @Success 200 {object} models.HomeCategoryListResp
// @Router /api/v1/home_category/list [post]
func HomeCategoryList(c *gin.Context) {
	req := &models.HomeCategoryListReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}

	resp, err := req.List()
	if err != nil {
		logger.Error("HomeCategoryList err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "获取失败"+err.Error())
		return
	}

	app.OK(c, resp, "OK")
	return
}

// HomeCategoryAdd 首页分类-新增
// @Summary 首页分类-新增
// @Tags 首页分类，V1.4.5
// @Produce json
// @Accept json
// @Param request body models.HomeCategoryAddReq true "首页分类-新增模型"
// @Success 200 {object} app.Response
// @Router /api/v1/home_category/add [post]
func HomeCategoryAdd(c *gin.Context) {
	req := new(models.HomeCategoryAddReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	err = models.CreateHomeCategory(req)
	if err != nil {
		logger.Error("CreateErpMarketingCoupon err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, err.Error())
		return
	}

	app.OK(c, nil, "新增成功")
	return
}

// HomeCategoryEdit 首页分类-编辑
// @Summary 首页分类-编辑
// @Tags 首页分类，V1.4.5
// @Produce json
// @Accept json
// @Param request body models.HomeCategoryEditReq true "首页分类-编辑模型"
// @Success 200 {object} models.HomeCategory
// @Router /api/v1/home_category/edit [post]
func HomeCategoryEdit(c *gin.Context) {
	req := new(models.HomeCategoryEditReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	// 更新订单信息
	homeCategory, err := models.EditHomeCategory(req)
	if err != nil {
		logger.Error("EditErpPurchaseOrder err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "编辑失败:"+err.Error())
		return
	}

	app.OK(c, homeCategory, "success")
}

// HomeCategoryDelete 首页分类-删除
// @Summary  首页分类-删除
// @Tags 首页分类，V1.4.5
// @Produce json
// @Accept json
// @Param request body models.HomeCategoryDeleteReq true " 首页分类-删除模型"
// @Success 200 {object} app.Response
// @Router /api/v1/home_category/delete [post]
func HomeCategoryDelete(c *gin.Context) {
	var req = new(models.HomeCategoryDeleteReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	err = models.DeleteHomeCategory(req)
	if err != nil {
		logger.Error("DeleteErpMonthEndClosing err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, err.Error())
		return
	}

	app.OK(c, nil, "删除成功")
	return
}
