package controllers

import (
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/system"
	"hongze/hz_crm_api/utils"
)

type SysMenuButtonController struct {
	BaseAuthController
}

// @Title 获取菜单全部按钮
// @Description 获取菜单全部按钮接口
// @Param   RoleId   query   int  true       "角色ID"
// @Success 200 {object} system.MenuButtonsResp
// @router /menu/buttons [get]
func (this *SysRoleController) SysMenuButtons() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	list, err := system.GetButtonMenu()
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	listLen := len(list)
	for i := 0; i < listLen; i++ {
		item := list[i]
		menus, err := system.GetSysMenuButtonItems(item.MenuId)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取失败，Err:" + err.Error()
			return
		}
		list[i].List = menus
	}
	roleId, _ := this.GetInt("RoleId")
	checkList := make([]*int, 0)
	cl, err := system.GetRoleButtonCheck(roleId)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	for _, v := range cl {
		checkList = append(checkList, &v.MenuButtonId)
	}
	resp := new(system.MenuButtonsResp)
	resp.List = list
	resp.CheckList = checkList
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
