package cases

import (
	"spaceofcheng/services/models/model"
)

// GetTagList 获取标签列表
func (c *CaseController) GetTagList() {
	caseid := c.GetString("caseid")
	cases := c.Context.Get("cases").([]model.SysUserCase)
	caseIDs := c.GetCaseIDs(cases)

	page, _ := c.GetInt("page")
	pageSize, _ := c.GetInt("pagesize")

	list, err := c.dao.GetTagList(caseid, caseIDs, page, pageSize)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(list)
}

// GetTagByID 根据ID获取标签信息
func (c *CaseController) GetTagByID() {
	tagid := c.GetString(":tagid")
	info, err := c.dao.GetTagByID(tagid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(info)
}

// SaveCaseTag 保存标签
func (c *CaseController) SaveCaseTag() {
	tag := model.SysTag{}
	if err := c.ParseForm(&tag); err != nil {
		c.ResponseError(err)
	}
	caseids := c.GetString("caseids")
	newTag, err := c.dao.SaveTag(tag, caseids)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(newTag)
}

// DelCaseTag 删除标签
func (c *CaseController) DelCaseTag() {
	tagid := c.GetString(":tagid")
	err := c.dao.DelTag(tagid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON("删除成功！")
}
