package report

import (
	"github.com/gin-gonic/gin"
	"hongze/hongze_yb/controller/response"
	"hongze/hongze_yb/models/request"
	"hongze/hongze_yb/models/tables/rddp/classify_menu"
	reportService "hongze/hongze_yb/services/report"
	userService "hongze/hongze_yb/services/user"
	"hongze/hongze_yb/utils"
	"strconv"
)

// ClassifyList 专栏列表
func ClassifyList(c *gin.Context) {
	reqClassifyIdFirst := c.DefaultQuery("classify_id_first", "")
	if reqClassifyIdFirst == "" {
		response.Fail("请输入二级分类标识", c)
		return
	}

	classifyIdFirst, err := strconv.Atoi(reqClassifyIdFirst)
	if err != nil {
		response.Fail("请输入正确的二级分类标识", c)
		return
	}
	userinfo := userService.GetInfoByClaims(c)

	classList, err := reportService.GetClassListByClassifyId(userinfo, classifyIdFirst)
	if err != nil {
		response.Fail(err.Error(), c)
		return
	}
	response.OkData("查询成功", classList, c)
	return
}

// ClassifyDetail 专栏详情
func ClassifyDetail(c *gin.Context) {
	reqClassifyIdSecond := c.DefaultQuery("classify_id_second", "")
	if reqClassifyIdSecond == "" {
		response.Fail("请输入二级分类标识", c)
		return
	}

	classifyIdSecond, err := strconv.Atoi(reqClassifyIdSecond)
	if err != nil {
		response.Fail("请输入正确的二级分类标识", c)
		return
	}
	userinfo := userService.GetInfoByClaims(c)

	detail, err := reportService.GetClassifyDetail(userinfo, classifyIdSecond)
	if err != nil {
		response.Fail(err.Error(), c)
		return
	}
	response.OkData("查询成功", detail, c)
	return
}

// ClassifyDetailReports 专栏目录列表
func ClassifyDetailReports(c *gin.Context) {
	reqClassifyIdSecond := c.DefaultQuery("classify_id_second", "")
	reqPageIndex := c.DefaultQuery("current_index", "1")
	reqPageSize := c.DefaultQuery("page_size", strconv.Itoa(utils.PageSize20))

	pageIndex, err := strconv.Atoi(reqPageIndex)
	if err != nil {
		response.Fail("请输入正确的条数限制", c)
		return
	}

	pageSize, err := strconv.Atoi(reqPageSize)
	if err != nil {
		response.Fail("请输入正确的页码", c)
		return
	}

	if reqClassifyIdSecond == "" {
		response.Fail("请输入二级分类标识", c)
		return
	}

	classifyIdSecond, err := strconv.Atoi(reqClassifyIdSecond)
	if err != nil {
		response.Fail("请输入正确的二级分类标识", c)
		return
	}

	userinfo := userService.GetInfoByClaims(c)

	detail, err := reportService.GetClassifyReportList(userinfo, classifyIdSecond, pageIndex, pageSize)
	if err != nil {
		response.Fail(err.Error(), c)
		return
	}
	response.OkData("查询成功", detail, c)
	return
}

// ClassifySimpleList 简单查询二级分类名称列表
func ClassifySimpleList(c *gin.Context) {
	reqClassifyIdFirst := c.DefaultQuery("classify_id_first", "")
	if reqClassifyIdFirst == "" {
		response.Fail("请输入分类ID", c)
		return
	}
	classifyIdFirst, err := strconv.Atoi(reqClassifyIdFirst)
	if err != nil {
		response.Fail("分类ID格式错误", c)
		return
	}
	userinfo := userService.GetInfoByClaims(c)

	classList, err := reportService.GetClassifySecondSimpleList(userinfo, classifyIdFirst)
	if err != nil {
		response.Fail(err.Error(), c)
		return
	}
	response.OkData("查询成功", classList, c)
	return
}

// ClassifyFirstList Ficc 菜单
func ClassifyFirstList(c *gin.Context) {
	userinfo := userService.GetInfoByClaims(c)

	classList, err := reportService.GetClassifyFirstList(userinfo)
	if err != nil {
		response.Fail(err.Error(), c)
		return
	}
	response.OkData("查询成功", classList, c)
	return
}

// ClassifyMenuList 一级分类-子目录列表
func ClassifyMenuList(c *gin.Context) {
	var req request.ClassifyMenuReq
	if err := c.Bind(&req); err != nil {
		response.Fail("参数有误", c)
		return
	}
	if req.ClassifyId <= 0 {
		response.Fail("参数有误", c)
		return
	}

	list, e := classify_menu.GetMenuListByClassifyId(req.ClassifyId)
	if e != nil {
		response.FailMsg("获取失败", "获取一级分类子目录列表失败, Err: "+e.Error(), c)
		return
	}
	response.OkData("获取成功", list, c)
}
