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_tenant_to_routes(e *echo.Echo) {
	group := e.Group("/tenant")
	group.Use(middleware.AuthMiddleware)
	group.POST("/getPage", tenantGetPage)
	group.POST("/getList", tenantGetList)
	group.POST("/save", tenantSave)
	group.POST("/update", tenantUpdate)
	group.POST("/remove", tenantRemove)
}

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

}

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

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

	tenant := new(models.Tenant)
	if err := c.Bind(tenant); err != nil {
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
	}
	status := models.Status_Enable
	tenant.Status = &status
	err := services.JdbcClient.JdbcInsert(tenant, tx)
	if err != nil {
		utils.PrintSqlErr(err)
		tx.Rollback()
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}

	dept := new(models.SysDept)
	dept.Name = tenant.Name
	pid := models.Common_Value_One_Int64
	dept.Pid = &pid
	dept.FirmFunctionary = tenant.ManagerName
	dept.FirmFunctionaryPhone = tenant.ManagerPhone
	deptSort := models.Common_Value_One_Int32
	dept.DeptSort = &deptSort
	dept.TenantId = tenant.ID
	err = services.JdbcClient.JdbcInsert(dept, tx)
	if err != nil {
		utils.PrintSqlErr(err)
		tx.Rollback()
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}
	tx.Commit()
	return c.JSON(http.StatusOK, tenant)
}

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

	tenant := new(models.Tenant)
	if err := c.Bind(tenant); err != nil {
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
	}
	err := services.JdbcClient.JdbcUpdateById(tenant, tx)
	if err != nil {
		utils.PrintSqlErr(err)
		tx.Rollback()
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}

	dept := new(models.SysDept)
	pid := models.Common_Value_One_Int64
	dept.Pid = &pid
	dept.TenantId = tenant.ID
	err = services.JdbcClient.GetJdbcModel(dept, tx)
	if err != nil {
		utils.PrintSqlErr(err)
		tx.Rollback()
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}
	dept.Name = tenant.Name
	dept.FirmFunctionary = tenant.ManagerName
	dept.FirmFunctionaryPhone = tenant.ManagerPhone
	err = services.JdbcClient.JdbcUpdateById(dept, tx)
	if err != nil {
		utils.PrintSqlErr(err)
		tx.Rollback()
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}
	tx.Commit()
	return c.JSON(http.StatusOK, tenant)
}

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

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

	err := services.JdbcClient.JdbcRemoveById(tenant, tx)
	if err != nil {
		utils.PrintSqlErr(err)
		tx.Rollback()
		return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	}
	// dept := new(models.SysDept)
	// pid := models.Common_Value_One_Int64
	// dept.Pid = &pid
	// dept.TenantId = tenant.ID
	// err = services.JdbcClient.JdbcRemove(dept,tx)
	// if err != nil{
	// 	utils.PrintSqlErr(err)
	// 	tx.Rollback()
	// 	return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
	// }

	tx.Commit()
	return c.JSON(http.StatusOK, tenant)
}
