Files
homekeeper/beekeeper/R/screen_quick_insp.R
T
2026-07-06 19:25:38 +02:00

114 lines
3.8 KiB
R

screen_quick_insp_ui <- function(id) {
ns <- NS(id)
shiny::tagList(
section_card(NULL,
shiny::div(class = "row g-2 py-2",
shiny::div(class = "col-6",
shiny::dateInput(ns("date"), "Datum", value = Sys.Date(), language = "de", weekstart = 1)
),
shiny::div(class = "col-6",
shiny::textInput(ns("inspector"), "Imker/in")
)
)
),
shiny::uiOutput(ns("colony_cards")),
shiny::div(class = "d-grid mt-2 mb-5",
shiny::actionButton(ns("save_all"), "Alle speichern",
class = "btn btn-primary btn-lg")
)
)
}
screen_quick_insp_server <- function(id, db, nav) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
colonies <- reactive({
req(nav$apiary_id, nav$screen == "quick_inspection")
db_colonies_list(db, apiary_id = nav$apiary_id, include_closed = FALSE)
})
output$colony_cards <- renderUI({
cols <- colonies()
if (nrow(cols) == 0)
return(shiny::p(class = "text-muted text-center py-4", "Keine aktiven Völker."))
lapply(seq_len(nrow(cols)), function(i) {
row <- cols[i, ]
cid <- row$id
section_card(
shiny::div(class = "d-flex justify-content-between align-items-center",
shiny::tags$strong(row$name),
shiny::HTML(colony_status_badge(row$status))
),
# Königin / Eier toggles
shiny::div(class = "d-flex gap-4 py-2 border-bottom",
toggle_row_inline(ns(paste0("qk_", cid)), "K gesehen"),
toggle_row_inline(ns(paste0("qe_", cid)), "Eier")
),
# Brutwaben
shiny::div(
class = "d-flex justify-content-between align-items-center py-2 border-bottom",
shiny::tags$span("Brutwaben", class = "text-muted small"),
shiny::numericInput(ns(paste0("qb_", cid)), NULL,
value = NA, min = 0, max = 20, step = 1, width = "80px")
),
# Notizen
shiny::div(class = "py-2",
shiny::textAreaInput(ns(paste0("qn_", cid)), NULL,
placeholder = "Notizen...", rows = 2, width = "100%")
)
)
})
})
observeEvent(input$save_all, {
cols <- isolate(colonies())
if (nrow(cols) == 0) return()
n_saved <- 0L
for (i in seq_len(nrow(cols))) {
cid <- cols$id[i]
queen <- isTRUE(input[[paste0("qk_", cid)]])
eggs <- isTRUE(input[[paste0("qe_", cid)]])
brood <- suppressWarnings(as.integer(input[[paste0("qb_", cid)]]))
notes <- trimws(input[[paste0("qn_", cid)]] %||% "")
if (!queen && !eggs && is.na(brood) && nchar(notes) == 0) next
tryCatch({
db_inspections_insert(db,
colony_id = cid,
date = input$date,
inspector = input$inspector,
brood_frames = if (!is.na(brood)) brood else NA_integer_,
queen_seen = queen,
eggs_seen = eggs,
notes = if (nchar(notes) > 0) notes else NA_character_,
traits = list()
)
n_saved <- n_saved + 1L
log_debug("quick_insp_saved", colony_id = cid)
}, error = function(e) {
log_error("quick_insp_error", colony_id = cid, msg = conditionMessage(e))
})
}
if (n_saved > 0)
shiny::showNotification(
sprintf("%d Durchsicht(en) gespeichert.", n_saved),
type = "message", duration = 3
)
nav$screen <- "colonies"
})
})
}
# Compact inline toggle (no label on separate line)
toggle_row_inline <- function(input_id, label) {
shiny::div(class = "d-flex align-items-center gap-2",
shiny::checkboxInput(input_id, label = NULL, value = FALSE),
shiny::tags$label(`for` = input_id, class = "mb-0", label)
)
}