package project

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"server/dao"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
	"server/model/project"
	"server/utils"
)

type ProjectApi struct{}

func (a ProjectApi) AddProject(c *gin.Context) {
	var ReqPro project.ReqProject
	err := c.ShouldBindJSON(&ReqPro)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	project := dao.Project{ProjectName: ReqPro.ProjectName, StartTime: ReqPro.StartTime, Remark: ReqPro.Remark}

	err = ProjectService.AddProject(project)
	if err != nil {
		global.GVA_LOG.Error(err.Error(), zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("添加成功", c)
}

func (a ProjectApi) DelProject(c *gin.Context) {
	var reqId request.GetById
	err := c.ShouldBindJSON(&reqId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(reqId, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = ProjectService.DelProject(reqId.ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

func (a ProjectApi) UpdateProject(c *gin.Context) {
	var ReqPro project.ReqProject
	err := c.ShouldBindJSON(&ReqPro)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(ReqPro, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	project := dao.Project{GVA_MODEL: global.GVA_MODEL{
		ID: ReqPro.ID,
	}, ProjectName: ReqPro.ProjectName, StartTime: ReqPro.StartTime, Remark: ReqPro.Remark}
	err = ProjectService.UpdateProject(project)
	if err != nil {
		global.GVA_LOG.Error("设置失败!", zap.Error(err))
		response.FailWithMessage("设置失败", c)
		return
	}
	response.OkWithMessage("设置成功", c)
}

func (a ProjectApi) GetProjectAndDetails(c *gin.Context) {
	var pageInfo request.PageInfo
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(pageInfo, utils.PageInfoVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := ProjectService.GetProjectAndDetails(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

func (a ProjectApi) GetProjectList(c *gin.Context) {
	list, err := ProjectService.GetProjectList()
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(list, "获取成功", c)
}

func (a ProjectApi) ChangeProjects(c *gin.Context) {
	var reqChange project.ReqChangeProjects
	err := c.ShouldBindJSON(&reqChange)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = ProjectService.ChangeProjects(reqChange)
	if err != nil {
		global.GVA_LOG.Error("设置失败!", zap.Error(err))
		response.FailWithMessage("设置失败", c)
		return
	}
	response.OkWithMessage("设置成功", c)
}

func (a ProjectApi) GetProjectListByUserID(c *gin.Context) {
	userID := utils.GetUserID(c)
	list, err := ProjectService.GetProjectListByUserID(userID)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(list, "获取成功", c)
}
