package controllers

import (
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/middleware/jwts"
	"LAPP_LF_MOM_BACKEND/web/models"
	"LAPP_LF_MOM_BACKEND/web/supports"
	"github.com/kataras/iris/v12"
	"log"
	"time"
)

// @Summary Menu列表数据
// @Description 获取JSON
// @Tags 菜单
// @Param menuName query string false "menuName"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
// @Router /admin/menu/menulist [get]
// @Security Bearer
func GetMenuList(ctx iris.Context) {
	var Menu models.Menu
	Menu.MenuName = ctx.URLParam("menuName")
	Menu.Visible = ctx.URLParam("visible")
	Menu.Title = ctx.URLParam("title")
	Menu.DataScope = "" //暂无
	result, err := Menu.SetMenu()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}

// @Summary Menu列表数据
// @Description 获取JSON
// @Tags 菜单
// @Param menuName query string false "menuName"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
// @Router /api/v1/menu [get]
// @Security Bearer
func GetMenu(ctx iris.Context) {
	var data models.Menu
	id, err := ctx.URLParamInt("id")
	data.MenuId = id
	result, err := data.GetByMenuId()
	utils.TrimStruct(&result, result)
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}

func GetMenuTreeRoleselect(ctx iris.Context) {
	var Menu models.Menu
	SysRole := new(models.Roletab)
	id,err := ctx.Params().GetInt("roleId")
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "格式错误！", nil)
		return
	}
	SysRole.RoleId = id
	result, err := Menu.SetMenuLable()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	var menuIds []int
	menuIds, err = SysRole.GetRoleMeunId()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}

	data := make(map[string]interface{})
	data["menus"] = result
	data["checkedKeys"] = menuIds
	supports.Ok(ctx, supports.OptionSuccess, data)
}

// @Summary 获取菜单树
// @Description 获取JSON
// @Tags 菜单
// @Accept  application/x-www-form-urlencoded
// @Product application/x-www-form-urlencoded
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/v1/menuTreeselect [get]
// @Security Bearer
func GetMenuTreeelect(ctx iris.Context) {
	var data models.Menu
	result, err := data.SetMenuLable()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}

// @Summary 创建菜单
// @Description 获取JSON
// @Tags 菜单
// @Accept  application/x-www-form-urlencoded
// @Product application/x-www-form-urlencoded
// @Param menuName formData string true "menuName"
// @Param Path formData string false "Path"
// @Param Action formData string true "Action"
// @Param Permission formData string true "Permission"
// @Param ParentId formData string true "ParentId"
// @Param IsDel formData string true "IsDel"
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/v1/menu [post]
// @Security Bearer
func InsertMenu(ctx iris.Context) {
	data := new(models.Menu)
	if err := ctx.ReadJSON(data); err != nil {
		log.Printf("json解析错误%v", err)
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	data.CreateBy = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	log.Println(data)
	result, err := data.Create()
	if err != nil {
		log.Printf("数据库插入错误%v", err)
		supports.Error(ctx, iris.StatusBadRequest, "添加失败！", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}

// @Summary 修改菜单
// @Description 获取JSON
// @Tags 菜单
// @Accept  application/x-www-form-urlencoded
// @Product application/x-www-form-urlencoded
// @Param id path int true "id"
// @Param data body models.Menu true "body"
// @Success 200 {string} string	"{"code": 200, "message": "修改成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "修改失败"}"
// @Router /api/v1/menu/{id} [put]
// @Security Bearer
func UpdateMenu(ctx iris.Context) {
	data := new(models.Menu)
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	data.UpdateBy = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	err := data.Update(data.MenuId)
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "更新失败:"+err.Error(), nil)
		return
	}
	supports.Ok(ctx, "修改成功", "")
}

// @Summary 删除菜单
// @Description 删除数据
// @Tags 菜单
// @Param id path int true "id"
// @Success 200 {string} string	"{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "删除失败"}"
// @Router /api/v1/menu/{id} [delete]
func DeleteMenu(ctx iris.Context) {
	var data models.Menu
	id := ctx.Params().GetString("id")
	data.UpdateBy = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	_, err := data.Delete(utils.ValueToInt(id, 0))
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, "删除成功", "")
}

// @Summary 根据角色名称获取菜单列表数据（左菜单使用）
// @Description 获取JSON
// @Tags 菜单
// @Param id path int true "id"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
// @Router /api/v1/menurole [get]
// @Security Bearer
func GetMenuRole(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	var Menu models.Menu
	result, err := Menu.SetMenuRole(user.Role)
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}

// @Summary 获取角色对应的菜单id数组
// @Description 获取JSON
// @Tags 菜单
// @Param id path int true "id"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
// @Router /api/v1/menuids/{id} [get]
// @Security Bearer
func GetMenuIDS(ctx iris.Context) {
	var data models.RoleMenu
	data.RoleName = ctx.URLParam("role")
	data.Lastmodifytime = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	result, err := data.GetIDS()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}
