package devices

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

type ScreensApi struct{}

func (a ScreensApi) AddScreens(c *gin.Context) {
	var ReqScr devices.ReqScreens
	err := c.ShouldBindJSON(&ReqScr)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	screens := dao.Screens{
		ScreensName: ReqScr.ScreensName,
		Sn:          ReqScr.Sn,
		ProjectId:   ReqScr.ProjectId,
		IPAddress:   ReqScr.IpAddress,
		Remark:      ReqScr.Remark}

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

func (a ScreensApi) DelScreens(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 = ScreensService.DelScreens(reqId.ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

func (a ScreensApi) UpdateScreens(c *gin.Context) {
	var ReqScr devices.ReqScreens
	err := c.ShouldBindJSON(&ReqScr)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(ReqScr, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	screens := dao.Screens{
		ID:          ReqScr.ID,
		ScreensName: ReqScr.ScreensName,
		Sn:          ReqScr.Sn,
		ProjectId:   ReqScr.ProjectId,
		IPAddress:   ReqScr.IpAddress,
		Remark:      ReqScr.Remark}
	err = ScreensService.UpdateScreens(screens)
	if err != nil {
		global.GVA_LOG.Error("设置失败!", zap.Error(err))
		response.FailWithMessage("设置失败", c)
		return
	}
	response.OkWithMessage("设置成功", c)
}

func (a ScreensApi) GetScreensList(c *gin.Context) {
	var pageInfo devices.SearchInfo
	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
	}

	userID := utils.GetUserID(c)
	list, total, err := ScreensService.GetScreensList(pageInfo, userID)
	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)
}
