package cases

import "spaceofcheng/services/models/model"

// GetCaseArea 获取案场区域列表
func (c *CaseController) GetCaseArea() {
	caseids := c.GetString("caseid")
	if caseids == "" {
		cases := c.Context.Get("cases").([]model.SysUserCase)
		caseids = c.GetCaseIDs(cases)
	}
	page, _ := c.GetInt("page")
	pageSize, _ := c.GetInt("pagesize")
	areas, err := c.dao.GetCaseArea(caseids, page, pageSize)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(areas)
}

// GetCaseAreaByID 根据ID获取案场区域
func (c *CaseController) GetCaseAreaByID() {
	areaid := c.GetString(":areaid")
	area, err := c.dao.GetCaseAreaByID(areaid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(area)
}

// SaveCaseArea 保存案场区域
func (c *CaseController) SaveCaseArea() {
	area := model.SysCaseArea{}
	if err := c.ParseForm(&area); err != nil {
		c.ResponseError(err)
	}
	newinfo, err := c.dao.SaveCaseArea(area)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(newinfo)
}

// DelCaseArea 删除案场区域
func (c *CaseController) DelCaseArea() {
	areaid := c.GetString(":areaid")
	err := c.dao.DelCaseArea(areaid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON("删除成功！")
}

// GetAreaByCase 根据案场获取区域信息
func (c *CaseController) GetAreaByCase() {
	caseid := c.GetString("caseid")
	areas, err := c.dao.GetAreaByCase(caseid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(areas)
}
