package comment

import (
	"github.com/gin-gonic/gin"
	"hongze/hongze_yb/controller/response"
	reqComment "hongze/hongze_yb/models/request/comment"
	commentService "hongze/hongze_yb/services/comment"
	userService "hongze/hongze_yb/services/user"
	"hongze/hongze_yb/utils"
	"strconv"
)

// Comment 发布留言
func Comment(c *gin.Context)  {
	// 是否为备用小程序
	copyYb := c.Request.Header.Get("CopyYb")
	var req reqComment.ReqComment
	if c.ShouldBind(&req) !=nil {
		response.Fail("入参错误", c)
		return
	}
	userInfo := userService.GetInfoByClaims(c)
	data, err := commentService.Comment(userInfo, req, copyYb)
	if err != nil {
		response.Fail(err.Error(),c)
		return
	}
	response.OkData("留言成功", data, c)
	return
}

// Delete 删除留言
func Delete(c *gin.Context) {
	var req *reqComment.ReqDel
	if c.ShouldBind(&req) != nil {
		response.Fail("参数异常", c)
		return
	}
	userInfo := userService.GetInfoByClaims(c)
	err := commentService.Delete(userInfo, req)
	if err != nil {
		response.Fail(err.Error(),c)
		return
	}
	response.Ok("删除留言成功", c)
	return
}

// HotList 获取精选留言
func HotList(c *gin.Context)  {
	reqReportId := c.DefaultQuery("report_id", "")
	reqReportChapterId := c.DefaultQuery("report_chapter_id", "")
	reqOldReportId := c.DefaultQuery("old_report_id", "")
	reqOldReportChapterId := c.DefaultQuery("old_report_chapter_id", "")
	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
	}
	var (
		reportId int
		reportChapterId int
		oldReportId int
		oldReportChapterId int
	)
	if reqReportId != ""{
		reportId, err = strconv.Atoi(reqReportId)
		if err != nil {
			response.Fail("报告ID格式有误", c)
			return
		}
	}

	if reqReportChapterId != "" {
		reportChapterId, err = strconv.Atoi(reqReportChapterId)
		if err != nil {
			response.Fail("章节ID格式有误", c)
			return
		}
	}

	if reqOldReportId != ""{
		oldReportId, err = strconv.Atoi(reqOldReportId)
		if err != nil {
			response.Fail("老报告ID格式有误", c)
			return
		}
	}

	if reqOldReportChapterId != "" {
		oldReportChapterId, err = strconv.Atoi(reqOldReportChapterId)
		if err != nil {
			response.Fail("章节ID格式有误", c)
			return
		}
	}

	userinfo := userService.GetInfoByClaims(c)

	list, err := commentService.List(userinfo, reportId, reportChapterId, oldReportId,oldReportChapterId,true, pageIndex, pageSize)
	if err != nil {
		response.Fail(err.Error(), c)
		return
	}
	response.OkData("查询成功", list, c )
	return
}

// MyList 获取我的留言
func MyList(c *gin.Context)  {
	reqReportId := c.DefaultQuery("report_id", "")
	reqReportChapterId := c.DefaultQuery("report_chapter_id", "")

	reqOldReportId := c.DefaultQuery("old_report_id", "")
	reqOldReportChapterId := c.DefaultQuery("old_report_chapter_id", "")
	var (
		reportId int
		reportChapterId int
		oldReportId int
		oldReportChapterId int
		err error
	)
	if reqReportId != ""{
		reportId, err = strconv.Atoi(reqReportId)
		if err != nil {
			response.Fail("报告ID格式有误", c)
			return
		}
	}

	if reqReportChapterId != "" {
		reportChapterId, err = strconv.Atoi(reqReportChapterId)
		if err != nil {
			response.Fail("章节ID格式有误", c)
			return
		}
	}

	if reqOldReportId != ""{
		oldReportId, err = strconv.Atoi(reqOldReportId)
		if err != nil {
			response.Fail("老报告ID格式有误", c)
			return
		}
	}

	if reqOldReportChapterId != "" {
		oldReportChapterId, err = strconv.Atoi(reqOldReportChapterId)
		if err != nil {
			response.Fail("章节ID格式有误", c)
			return
		}
	}

	userinfo := userService.GetInfoByClaims(c)

	list, err := commentService.MyList(userinfo, reportId, reportChapterId, oldReportId,oldReportChapterId)
	if err != nil {
		response.Fail(err.Error(), c)
		return
	}
	response.OkData("查询成功", list, c )
	return
}