173 lines
5.3 KiB
Go
173 lines
5.3 KiB
Go
// telegram_service/keyboard/delegation.go - ПОЛНОСТЬЮ ЗАМЕНИТЬ
|
||
|
||
package keyboard
|
||
|
||
import (
|
||
"bitrix_balancer_api/core/lib/external/tgkit"
|
||
"bitrix_balancer_api/core/typescore"
|
||
"bitrix_balancer_api/telegram_service/types"
|
||
"context"
|
||
"fmt"
|
||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||
)
|
||
|
||
const delegationPerPage = 6
|
||
|
||
func BuildDelegationMainMenu() interface{} {
|
||
return tgkit.NewInlineKeyboard().
|
||
Row(tgkit.BtnStyled(tgkit.ButtonStyleSuccess, "Делегировать права", "deleg_create")).
|
||
Row(tgkit.Btn("📋 Делегированные права", "deleg_list")).
|
||
Build()
|
||
}
|
||
|
||
func BuildDelegationCategoryMenu(categories []*typescore.ApprovalTopicCategory, page int) interface{} {
|
||
total := len(categories)
|
||
totalPages := (total + delegationPerPage - 1) / delegationPerPage
|
||
if page < 0 {
|
||
page = 0
|
||
}
|
||
if page >= totalPages && totalPages > 0 {
|
||
page = totalPages - 1
|
||
}
|
||
start, end := page*delegationPerPage, page*delegationPerPage+delegationPerPage
|
||
if end > total {
|
||
end = total
|
||
}
|
||
|
||
kb := tgkit.NewInlineKeyboard()
|
||
for _, c := range categories[start:end] {
|
||
if c.ID == nil || c.Name == nil {
|
||
continue
|
||
}
|
||
kb.Row(tgkit.Btn(*c.Name, fmt.Sprintf("deleg_category:%d", *c.ID)))
|
||
}
|
||
if totalPages > 1 {
|
||
kb.Rows = append(kb.Rows, buildPageRow(page, totalPages, "deleg_category_page", "deleg_page_noop"))
|
||
}
|
||
return kb.Build()
|
||
}
|
||
|
||
func BuildDelegationTopicMenu(topics []*typescore.ApprovalTopic, categoryID uint64, page int) interface{} {
|
||
const perPage = 6
|
||
total := len(topics)
|
||
totalPages := (total + perPage - 1) / perPage
|
||
if page < 0 {
|
||
page = 0
|
||
}
|
||
if page >= totalPages && totalPages > 0 {
|
||
page = totalPages - 1
|
||
}
|
||
start, end := page*perPage, page*perPage+perPage
|
||
if end > total {
|
||
end = total
|
||
}
|
||
|
||
kb := tgkit.NewInlineKeyboard()
|
||
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleSuccess, "Вся категория", fmt.Sprintf("deleg_all_category:%d", categoryID)))
|
||
|
||
for _, t := range topics[start:end] {
|
||
if t.ID == nil || t.Name == nil {
|
||
continue
|
||
}
|
||
kb.Row(tgkit.Btn(*t.Name, fmt.Sprintf("deleg_topic:%d", *t.ID)))
|
||
}
|
||
if totalPages > 1 {
|
||
kb.Rows = append(kb.Rows, buildPageRow(page, totalPages,
|
||
fmt.Sprintf("deleg_topic_page:%d", categoryID), "deleg_page_noop"))
|
||
}
|
||
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "К категориям", "deleg_back_categories"))
|
||
return kb.Build()
|
||
}
|
||
|
||
func BuildDelegationUserMenu(users []*typescore.User, categoryID, topicID uint64, page int) interface{} {
|
||
const perPage = 8
|
||
total := len(users)
|
||
totalPages := (total + perPage - 1) / perPage
|
||
if page < 0 {
|
||
page = 0
|
||
}
|
||
if page >= totalPages && totalPages > 0 {
|
||
page = totalPages - 1
|
||
}
|
||
start, end := page*perPage, page*perPage+perPage
|
||
if end > total {
|
||
end = total
|
||
}
|
||
|
||
kb := tgkit.NewInlineKeyboard()
|
||
for _, u := range users[start:end] {
|
||
if u.ID == nil || u.FullName == nil {
|
||
continue
|
||
}
|
||
kb.Row(tgkit.Btn(*u.FullName, fmt.Sprintf("deleg_user:%d", *u.ID)))
|
||
}
|
||
if totalPages > 1 {
|
||
kb.Rows = append(kb.Rows, buildPageRow(page, totalPages,
|
||
fmt.Sprintf("deleg_user_page:%d:%d", categoryID, topicID), "deleg_page_noop"))
|
||
}
|
||
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "Назад", fmt.Sprintf("deleg_category:%d", categoryID)))
|
||
return kb.Build()
|
||
}
|
||
|
||
func BuildDelegatedRightsList(delegations []*typescore.ApprovalDelegation, page int, ctx context.Context, ipc *types.InternalProviderControl) interface{} {
|
||
const perPage = 6
|
||
total := len(delegations)
|
||
totalPages := (total + perPage - 1) / perPage
|
||
if page < 0 {
|
||
page = 0
|
||
}
|
||
if page >= totalPages && totalPages > 0 {
|
||
page = totalPages - 1
|
||
}
|
||
start, end := page*perPage, page*perPage+perPage
|
||
if end > total {
|
||
end = total
|
||
}
|
||
|
||
kb := tgkit.NewInlineKeyboard()
|
||
for _, d := range delegations[start:end] {
|
||
if d.ID == nil {
|
||
continue
|
||
}
|
||
scopeName := "—"
|
||
if d.TopicID != nil {
|
||
if t, _ := ipc.Database.ApprovalTopic.GetApprovalTopicByIDDB(ctx, *d.TopicID); t != nil && t.Name != nil {
|
||
scopeName = *t.Name
|
||
}
|
||
} else if d.CategoryID != nil {
|
||
if c, _ := ipc.Database.ApprovalTopicCategory.GetApprovalTopicCategoryByIDDB(ctx, *d.CategoryID); c != nil && c.Name != nil {
|
||
scopeName = *c.Name
|
||
}
|
||
}
|
||
kb.Row(tgkit.Btn(scopeName, fmt.Sprintf("deleg_detail:%d", *d.ID)))
|
||
}
|
||
if totalPages > 1 {
|
||
kb.Rows = append(kb.Rows, buildPageRow(page, totalPages, "deleg_list_page", "deleg_page_noop"))
|
||
}
|
||
return kb.Build()
|
||
}
|
||
|
||
func BuildDelegationActions(delegationID uint64) interface{} {
|
||
return tgkit.NewInlineKeyboard().
|
||
Row(tgkit.Btn("📅 Изменить срок", fmt.Sprintf("deleg_extend:%d", delegationID))).
|
||
Row(tgkit.BtnStyled(tgkit.ButtonStyleDanger, "Отозвать делегирование", fmt.Sprintf("deleg_revoke:%d", delegationID))).
|
||
Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "К списку", "deleg_list")).
|
||
Build()
|
||
}
|
||
|
||
func BuildDelegationExtendCancel(delegationID uint64) interface{} {
|
||
return tgkit.NewInlineKeyboard().
|
||
Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "Отмена", fmt.Sprintf("deleg_detail:%d", delegationID))).
|
||
Build()
|
||
}
|
||
|
||
func BuildDelegationBackToUsers(categoryID, topicID uint64) tgbotapi.InlineKeyboardMarkup {
|
||
callback := fmt.Sprintf("deleg_all_category:%d", categoryID)
|
||
if topicID > 0 {
|
||
callback = fmt.Sprintf("deleg_topic:%d", topicID)
|
||
}
|
||
return tgkit.NewInlineKeyboard().
|
||
Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "Назад", callback)).
|
||
Build()
|
||
}
|