package handlers

import (
	"net/http"

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

	"easydo-echo_win7/middleware"

	"github.com/labstack/echo-contrib/session"
	"github.com/labstack/echo/v4"
)

func Add_menu_to_routes(e *echo.Echo) {
	group := e.Group("/sysMenu")
	group.Use(middleware.AuthMiddleware)
	group.POST("/build", menuBuild)
	group.POST("/getList", menuGetList)
	group.POST("/save", menuSave)
	group.POST("/update", menuUpdate)
	group.POST("/remove", menuRemove)
}

func menuBuild(c echo.Context) error {
	sess, err := session.Get("auth_session", c)
	if err != nil {
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("用户请先登录", err.Error()))
	}

	user_role := new(models.SysUsersRoles)
	user_id, ok := sess.Values["user_id"].(int64)
	if !ok {
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("用户请先登录", err.Error()))
	}
	user_role.UserID = &user_id
	result, err := services.JdbcClient.GetJdbcListByObject(user_role)
	if err != nil {
		utils.PrintSqlErr(err)
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
	}
	user_role_list := utils.ConvertInterface[[]models.SysUsersRoles](result)
	role_id_list := utils.Map(user_role_list, func(role models.SysUsersRoles) int64 {
		return *role.RoleID
	})

	paramMap := map[string]interface{}{
		"roleIdIn": role_id_list,
	}
	result, err = services.JdbcClient.GetJdbcList(paramMap, models.SysRolesMenus{})
	if err != nil {
		utils.PrintSqlErr(err)
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
	}
	role_menu_list := utils.ConvertInterface[[]models.SysRolesMenus](result)
	menu_id_list := utils.Map(role_menu_list, func(role models.SysRolesMenus) int64 {
		return *role.MenuID
	})
	paramMap = map[string]interface{}{
		"idIn": menu_id_list,
	}
	result, err = services.JdbcClient.GetJdbcList(paramMap, models.SysMenu{})
	if err != nil {
		utils.PrintSqlErr(err)
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
	}
	menu_list := utils.ConvertInterface[[]models.SysMenu](result)
	if menu_list == nil {
		return c.JSON(http.StatusOK, []string{})
	}
	return c.JSON(http.StatusOK, menu_list)
}

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

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

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

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

	menu := new(models.SysMenu)
	if err := c.Bind(menu); err != nil {
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析错误", err.Error()))
	}
	menuParam := new(models.SysMenu)
	menuParam.Pid = menu.ID
	count, err := services.JdbcClient.GetJdbcCount(menuParam, tx)
	if err != nil {
		utils.PrintSqlErr(err)
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
	}
	if count > 0 {
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("存在下级子菜单，无法删除", ""))
	}
	roleMenuParam := new(models.SysRolesMenus)
	roleMenuParam.MenuID = menu.ID
	err = services.JdbcClient.JdbcRemove(roleMenuParam, tx)
	if err != nil {
		utils.PrintSqlErr(err)
		tx.Rollback()
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
	}

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