package router

import (
	"exam_system/dao"
	"exam_system/result"
	"exam_system/utils"
	"exam_system/vo"
	"github.com/gin-gonic/gin"
	"strings"
)

func TestPaper(router *RouterPlus) {

	router.GET("/testPaper/:id", GetTestPaper)
	r := router.Group("/admin")
	{
		// 添加试卷
		r.POST("/testPaper", AdminAddTestPaper)

		// 获取试卷
		r.GET("/testPaper/:id", AdminGetTestPaper)

		// 获取试卷列表
		r.GET("/testPaper/list", AdminTestPaperList)

		// 修改试卷
		r.PUT("/testPaper", AdminUpdateTestPaper)

		// 删除试卷
		r.DELETE("/testPaper/:ids", AdminDeleteTestPaper)
	}

}

func GetTestPaper(c *gin.Context) *result.Result {
	id := c.Param("id")
	if id == "" {
		return result.UNKNOW_ERROR
	}
	return dao.TestPaperDetail2(id)
}

func AdminAddTestPaper(c *gin.Context) *result.Result {
	var testPaper vo.TestPaperVo
	err := c.ShouldBindJSON(&testPaper)
	if err != nil || testPaper.Name=="" || len(testPaper.Chapters)<1{
		return result.PARAM_ERROR
	}

	return dao.AddTestPaper(&testPaper)
}

func AdminGetTestPaper(c *gin.Context) *result.Result {
	id := c.Param("id")
	if id == "" {
		return result.UNKNOW_ERROR
	}
	return dao.TestPaperDetail(id)
}

func AdminTestPaperList(c *gin.Context) *result.Result {
	page, size, sort, query, err := utils.Page(c)
	if err != nil {
		return result.PARAM_ERROR
	}
	return dao.TestPaperList(page, size, sort, query)
}

func AdminUpdateTestPaper(c *gin.Context) *result.Result {
	var testPaper vo.TestPaperVo
	err := c.ShouldBindJSON(&testPaper)
	if err != nil || testPaper.Id == 0 || len(testPaper.Chapters)<1{
		return result.PARAM_ERROR
	}
	return dao.UpdateTestPaper(&testPaper)
}

func AdminDeleteTestPaper(c *gin.Context) *result.Result {
	idStr := c.Param("ids")
	if idStr == "" {
		return result.PARAM_ERROR
	}

	ids := strings.Split(idStr, ",")

	return dao.DeleteTestPapers(ids)
}
