package handlers

import (
	"net/http"

	"easydo-echo_win7/models"
	"easydo-echo_win7/services"
	"easydo-echo_win7/utils"

	// "github.com/labstack/echo-contrib/session"
	"easydo-echo_win7/middleware"

	"github.com/labstack/echo/v4"
)

func Add_device_to_routes(e *echo.Echo) {
	group := e.Group("/device")
	group.Use(middleware.AuthMiddleware)
	group.POST("/getPage", deviceGetPage)
	group.POST("/getList", deviceGetList)
	group.POST("/save", deviceSave)
	group.POST("/update", deviceUpdate)
	group.POST("/remove", deviceRemove)
}

func deviceGetPage(c echo.Context) error {
	var paramMap map[string]interface{}
	if err := c.Bind(&paramMap); err != nil {
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
	}
	result, err := services.JdbcClient.GetJdbcPage(paramMap, models.Device{})
	if err != nil {
		utils.PrintSqlErr(err)
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}
	list := utils.ConvertInterface[[]models.Device](result.Records)
	if len(list) == 0 {
		list = []models.Device{}
	}
	result.Records = list
	return c.JSON(http.StatusOK, result)

}

func deviceGetList(c echo.Context) error {
	var paramMap map[string]interface{}
	if err := c.Bind(&paramMap); err != nil {
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
	}
	result, err := services.JdbcClient.GetJdbcList(paramMap, models.Device{})
	if err != nil {
		utils.PrintSqlErr(err)
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}
	list := utils.ConvertInterface[[]models.Device](result)
	if len(list) == 0 {
		return c.JSON(http.StatusOK, []models.Device{})
	}
	return c.JSON(http.StatusOK, list)
}

func deviceSave(c echo.Context) error {
	device := new(models.Device)
	if err := c.Bind(device); err != nil {
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
	}
	err := services.JdbcClient.JdbcInsert(device)
	if err != nil {
		utils.PrintSqlErr(err)
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}
	return c.JSON(http.StatusOK, device)
}

func deviceUpdate(c echo.Context) error {
	device := new(models.Device)
	if err := c.Bind(device); err != nil {
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
	}
	err := services.JdbcClient.JdbcUpdateById(device)
	if err != nil {
		utils.PrintSqlErr(err)
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}
	return c.JSON(http.StatusOK, device)
}

func deviceRemove(c echo.Context) error {
	tx, _ := services.MYSQL_DB.Beginx()

	role := new(models.Device)
	if err := c.Bind(role); err != nil {
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
	}

	err := services.JdbcClient.JdbcRemoveById(role, tx)
	if err != nil {
		utils.PrintSqlErr(err)
		tx.Rollback()
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}
	tx.Commit()
	return c.JSON(http.StatusOK, role)
}
