/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/
use crate::{
    Page,
    filter_sort::NodeFilterSortForm,
    locale::{Language, tr, trs},
    node_card::NodeCard,
};
use jellycommon::{
    api::{ApiItemsResponse, NodeFilterSort},
    routes::u_items_filter,
};
use markup::DynRender;

markup::define! {
    ItemsPage<'a>(lang: &'a Language, r: ApiItemsResponse, filter: &'a NodeFilterSort, page: usize) {
        .page.dir {
            h1 { "All Items" }
            @NodeFilterSortForm { f: filter, lang }
            ul.children { @for (node, udata) in &r.items {
                li {@NodeCard { node, udata, lang }}
            }}
            p.pagecontrols {
                span.current { @tr(**lang, "page.curr").replace("{cur}", &(page + 1).to_string()).replace("{max}", &r.pages.to_string()) " " }
                @if *page > 0 {
                    a.prev[href=u_items_filter(page - 1, filter)] { @trs(lang, "page.prev") } " "
                }
                @if page + 1 < r.pages {
                    a.next[href=u_items_filter(page + 1, filter)] { @trs(lang, "page.next") }
                }
            }
        }
    }
}

impl Page for ItemsPage<'_> {
    fn title(&self) -> String {
        tr(*self.lang, "home").to_string()
    }
    fn to_render(&self) -> DynRender<'_> {
        markup::new!(@self)
    }
}
