97 lines
2.4 KiB
Go
97 lines
2.4 KiB
Go
package keyboard
|
|
|
|
import (
|
|
"bitrix_balancer_api/core/typescore"
|
|
"fmt"
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
)
|
|
|
|
const observerSelectionPerPage = 8
|
|
|
|
func BuildObserverSelectionMenu(
|
|
observers []*typescore.User,
|
|
selectedIDs []uint64,
|
|
page int,
|
|
) tgbotapi.InlineKeyboardMarkup {
|
|
total := len(observers)
|
|
if total == 0 {
|
|
return tgbotapi.NewInlineKeyboardMarkup(
|
|
tgbotapi.NewInlineKeyboardRow(
|
|
tgbotapi.NewInlineKeyboardButtonData("✅ Подтвердить без наблюдателей", "obs_select_confirm"),
|
|
),
|
|
)
|
|
}
|
|
|
|
totalPages := (total + observerSelectionPerPage - 1) / observerSelectionPerPage
|
|
if page < 0 {
|
|
page = 0
|
|
}
|
|
if page >= totalPages {
|
|
page = totalPages - 1
|
|
}
|
|
|
|
start := page * observerSelectionPerPage
|
|
end := min(start+observerSelectionPerPage, total)
|
|
|
|
// Множество выбранных для быстрой проверки
|
|
selectedSet := make(map[uint64]bool, len(selectedIDs))
|
|
for _, id := range selectedIDs {
|
|
selectedSet[id] = true
|
|
}
|
|
|
|
var rows [][]tgbotapi.InlineKeyboardButton
|
|
|
|
for _, u := range observers[start:end] {
|
|
if u.ID == nil || u.FullName == nil {
|
|
continue
|
|
}
|
|
|
|
label := *u.FullName
|
|
if selectedSet[*u.ID] {
|
|
label = "✅ " + label
|
|
}
|
|
|
|
rows = append(rows, tgbotapi.NewInlineKeyboardRow(
|
|
tgbotapi.NewInlineKeyboardButtonData(label, fmt.Sprintf("obs_select_toggle:%d", *u.ID)),
|
|
))
|
|
}
|
|
|
|
// Навигация
|
|
if totalPages > 1 {
|
|
var navRow []tgbotapi.InlineKeyboardButton
|
|
if page > 0 {
|
|
navRow = append(navRow, tgbotapi.NewInlineKeyboardButtonData(
|
|
"← Назад", fmt.Sprintf("obs_select_page:%d", page-1),
|
|
))
|
|
}
|
|
pageBtn := tgbotapi.NewInlineKeyboardButtonData(
|
|
fmt.Sprintf("%d / %d", page+1, totalPages), "obs_select_noop",
|
|
)
|
|
navRow = append(navRow, pageBtn)
|
|
if page < totalPages-1 {
|
|
navRow = append(navRow, tgbotapi.NewInlineKeyboardButtonData(
|
|
"→ Вперёд", fmt.Sprintf("obs_select_page:%d", page+1),
|
|
))
|
|
}
|
|
rows = append(rows, navRow)
|
|
}
|
|
|
|
// Кнопка подтверждения
|
|
confirmLabel := "✅ Подтвердить"
|
|
if len(selectedIDs) > 0 {
|
|
confirmLabel = fmt.Sprintf("✅ Подтвердить (%d выбрано)", len(selectedIDs))
|
|
}
|
|
rows = append(rows, tgbotapi.NewInlineKeyboardRow(
|
|
tgbotapi.NewInlineKeyboardButtonData(confirmLabel, "obs_select_confirm"),
|
|
))
|
|
|
|
return tgbotapi.NewInlineKeyboardMarkup(rows...)
|
|
}
|
|
|
|
func min(a, b int) int {
|
|
if a < b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|