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_role_to_routes(e *echo.Echo) {
	group := e.Group("/sysRole")
	group.Use(middleware.AuthMiddleware)
	group.POST("/getPage", roleGetPage)
	group.POST("/getList", roleGetList)
	group.POST("/save", roleSave)
	group.POST("/update", roleUpdate)
	group.POST("/remove", roleRemove)
}

func roleGetPage(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.SysRole{})
	if err != nil {
		utils.PrintSqlErr(err)
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}
	list := utils.ConvertInterface[[]models.SysRole](result.Records)
	if len(list) == 0 {
		list = []models.SysRole{}
	}
	result.Records = list
	return c.JSON(http.StatusOK, result)

}

func roleGetList(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.SysRole{})
	if err != nil {
		utils.PrintSqlErr(err)
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}
	list := utils.ConvertInterface[[]models.SysRole](result)
	if len(list) == 0 {
		return c.JSON(http.StatusOK, []models.SysRole{})
	}
	return c.JSON(http.StatusOK, list)
}

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

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

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

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

	roleMenuParam := new(models.SysRolesMenus)
	roleMenuParam.RoleID = role.ID
	err := services.JdbcClient.JdbcRemove(roleMenuParam, tx)
	if err != nil {
		utils.PrintSqlErr(err)
		tx.Rollback()
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}

	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)
}
