package goodsmanage

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

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

func HotSearchDel(c *gin.Context) {
	//var data models.HotSearch
	//err := c.ShouldBindJSON(&data)
	//if err != nil {
	//	logger.Errorf("err:", err)
	//	app.Error(c, http.StatusBadRequest, err, "参数错误")
	//	return
	//}

	req := struct {
		Id []uint32 `json:"id"`
	}{}
	if c.ShouldBindJSON(&req) != nil {
		logger.Errorf("para err")
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}
	data := models.HotSearch{}
	err := data.Del(req.Id)
	if err != nil {
		logger.Errorf("err:", err)
		app.Error(c, http.StatusBadRequest, err, "参数错误")
		return
	}
	app.OK(c, nil, "删除成功")
}

func HotSearchAdd(c *gin.Context) {
	req := struct {
		Keyword string `json:"keyword"`
		Sort    uint32 `json:"sort" ` // 排序
	}{}
	if c.ShouldBindJSON(&req) != nil {
		logger.Errorf("para err")
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}
	data := models.HotSearch{
		Keyword: req.Keyword,
		Sort:    req.Sort,
	}

	err := data.Create()
	if err != nil {
		logger.Errorf("err:", err)
		app.Error(c, http.StatusBadRequest, err, "参数错误")
		return
	}
	app.OK(c, nil, "添加成功")
}

func HotSearchModify(c *gin.Context) {
	req := struct {
		Id      uint32 `json:"id"`
		Keyword string `json:"keyword"`
		Sort    uint32 `json:"sort" ` // 排序
	}{}
	if c.ShouldBindJSON(&req) != nil {
		logger.Errorf("para err")
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}
	data := models.HotSearch{
		Keyword: req.Keyword,
		Sort:    req.Sort,
	}
	data.ID = req.Id

	err := data.Modify()
	if err != nil {
		logger.Errorf("err:", err)
		app.Error(c, http.StatusBadRequest, err, "参数错误")
		return
	}
	app.OK(c, nil, "修改成功")
}
