package verify

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

// CaseController 信息
type VerifyController struct {
	dao *verify.VerifyServ
	controllers.BaseController
}

// Constructor 初始化 Controller
// @Title Constructor
// @Description 初始化 Controller, 系统自动调用
func (c *VerifyController) Constructor() {
	c.dao = verify.NewVerifyServ(c.Context)
}

// GetCustomerCourseDetailListById 根据id获取我的课程详情
func (c *VerifyController) GetCustomerCourseDetailListById() {
	customerCourseId := c.GetString(":customerCourseId")
	caseid := c.GetString("caseid")
	if caseid == "" {
		caseinfo := c.Context.Get("currentCase").(model.SysUserCase)
		caseid = caseinfo.CaseId
	}
	page, _ := c.GetInt("page")
	pageSize, _ := c.GetInt("pagesize")
	list, err := c.dao.GetCustomerCourseDetailById(customerCourseId, caseid, page, pageSize)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(list)

}

// GetCustomerCourseDetailListBytel 根据手机号获取我的课程详情id
func (c *VerifyController) GetCustomerCourseDetailListBytel() {
	caseid := c.GetString("caseid")
	if caseid == "" {
		caseinfo := c.Context.Get("currentCase").(model.SysUserCase)
		caseid = caseinfo.CaseId
	}
	tel := c.GetString(":tel")
	page, _ := c.GetInt("page")
	pageSize, _ := c.GetInt("pagesize")
	list, err := c.dao.GetCustomerCourseDetailByTel(tel, caseid, page, pageSize)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(list)

}

// VerifyCourse核销我的课程详情
func (c *VerifyController) VerifyCourse() {
	sysUser := c.Context.Get("user").(model.SysUser)
	userId := sysUser.UserId
	customerDetailId := c.GetString(":customerDetailId")
	customerCourseDetail, err := c.dao.GetCustomerCourseDetailByDetailId(customerDetailId)
	if err != nil {
		c.ResponseError(err)
	}

	customerCourse, err := c.dao.GetCustomerCourseByDetailId(customerCourseDetail.CustomerDetailId)
	if err != nil {
		c.ResponseError(err)
	}
	if customerCourse.CourseObtaimType == "card" {
		customerCard, err := c.dao.GetCustomerCardByDetailId(customerCourseDetail.CustomerDetailId)
		if err != nil {
			c.ResponseError(err)
		}
		if customerCard.VerifyStatus == "usable" {
			customerCard.VerifyStatus = "used"
			err := c.dao.UpdateCustomerCard(customerCard)
			if err != nil {
				c.ResponseError(err)
			}
		}
	}
	err = c.dao.UpdateCustomerCourse(customerCourse)
	if err != nil {
		c.ResponseError(err)
	}
	err = c.dao.UpdateCustomerCourseDetail(customerCourseDetail, userId)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON("核销成功")

}
