package course

import (
	"spaceofcheng/services/controllers"
	"spaceofcheng/services/models/model"
	"spaceofcheng/services/service/course"
)

// CourseController 商品
type CourseController struct {
	serv *course.CourseServ
	controllers.BaseController
}

// Constructor 初始化 Controller
// @Title Constructor
// @Description 初始化 Controller, 系统自动调用
func (c *CourseController) Constructor() {
	c.serv = course.NewCourseServ(c.Context)
}

// GetCourseList 获取课程列表
func (c *CourseController) GetCourseList() {
	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")
	name := c.GetString("name")
	typeid := c.GetString("typeid")
	courses, err := c.serv.GetCourseList(caseids, name, typeid, page, pageSize)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(courses)
}

// GetCourseByID 获取课程明细
func (c *CourseController) GetCourseByID() {
	courseid := c.GetString(":courseid")
	info, err := c.serv.GetCourseByID(courseid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(info)
}

// SaveCourse 保存课程
func (c *CourseController) SaveCourse() {
	course := model.TaCourse{}
	if err := c.ParseForm(&course); err != nil {
		c.ResponseError(err)
	}
	tagids := c.GetString("tagids")
	newinfo, err := c.serv.SaveCourse(course, tagids)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(newinfo)
}

// DeleteCourse 删除课程
func (c *CourseController) DeleteCourse() {
	courseid := c.GetString(":courseid")
	err := c.serv.DelCourse(courseid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON("删除成功！")
}

// CoursePublic 课程发布
func (c *CourseController) CoursePublic() {
	courseid := c.GetString(":courseid")
	err := c.serv.CoursePublic(courseid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON("操作成功！")
}

// CourseUnPublic 课程取消发布
func (c *CourseController) CourseUnPublic() {
	courseid := c.GetString(":courseid")
	err := c.serv.CourseUnPublic(courseid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON("操作成功！")
}

// GetCourseImgs 获取课程图片
func (c *CourseController) GetCourseImgs() {
	courseid := c.GetString(":courseid")
	imgs, err := c.serv.GetCourseImgs(courseid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(imgs)
}

// SaveCourseImg 保存课程图片
func (c *CourseController) SaveCourseImg() {
	img := model.TaCourseImg{}
	if err := c.ParseForm(&img); err != nil {
		c.ResponseError(err)
	}
	newimg, err := c.serv.SaveCourseImg(img)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(newimg)
}

// DelCourseImg 删除课程图片
func (c *CourseController) DelCourseImg() {
	courseid := c.GetString(":courseid")
	err := c.serv.DelCourseImg(courseid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON("操作成功！")
}

// GetCourseSchedule 获取课程排期
func (c *CourseController) GetCourseSchedule() {
	name := c.GetString("name")
	caseids := c.GetString("caseid")
	if caseids == "" {
		cases := c.Context.Get("cases").([]model.SysUserCase)
		caseids = c.GetCaseIDs(cases)
	}
	date := c.GetString("date")
	page, _ := c.GetInt("page")
	pageSize, _ := c.GetInt("pagesize")
	list, err := c.serv.GetCourseSchedule(name, caseids, date, page, pageSize)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(list)
}

// GetDetails 获取所有排课
func (c *CourseController) GetDetails() {
	caseid := c.GetString("caseid")
	date := c.GetString("date")
	list, err := c.serv.GetDetails(caseid, date)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(list)
}

// GetDetailByID 获取单个排课信息
func (c *CourseController) GetDetailByID() {
	detailid := c.GetString(":detailid")
	detail, err := c.serv.GetDetailByID(detailid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(detail)
}

// SaveDetail 保存排课信息
func (c *CourseController) SaveDetail() {
	detail := model.TaCourseDetail{}
	if err := c.ParseForm(&detail); err != nil {
		c.ResponseError(err)
	}
	newdetail, err := c.serv.SaveDetail(detail)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(newdetail)
}

// DelCourseDetail 删除课程明细
func (c *CourseController) DelCourseDetail() {
	detailid := c.GetString(":detailid")
	err := c.serv.DelCourseDetail(detailid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON("删除成功！")
}

// GetSelectCourseList 获取精选课程
// func (c *CourseController) GetSelectCourseList() {
// 	orgid := c.GetString("orgid")
// 	courses, err := c.serv.GetSelectCourseList(orgid)
// 	if err != nil {
// 		c.ResponseError(err)
// 	}
// 	c.ResponseJSON(courses)
// }

// GetCourseByLocation 根据位置获取课程信息
func (c *CourseController) GetCourseByLocation() {
	locationid := c.GetString("locationid")
	orgid := c.GetString("orgid")
	if locationid == "selected" {
		courses, err := c.serv.GetSelectCourseList(orgid)
		if err != nil {
			c.ResponseError(err)
		}
		c.ResponseJSON(courses)
	}
	courses, err := c.serv.GetCourseByLocation(orgid, locationid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(courses)
}
