use leptos::*;
use chrono::Datelike;

fn human_datetime(datetime: &chrono::DateTime<chrono::Local>) -> String {
    format!(
        "{day} {month} {year}",
        day = datetime.day(),
        month = human_month(datetime.month()),
        year = datetime.year()
    )
}

fn human_month<'a>(month: u32) -> &'a str {
    match month {
        1 => "Jan.",
        2 => "Feb.",
        3 => "Mar.",
        4 => "Avr.",
        5 => "Mai",
        6 => "Juin",
        7 => "Juillet",
        8 => "Août",
        9 => "Sept.",
        10 => "Oct.",
        11 => "Nov.",
        12 => "Dec.",
        _ => "Invalide"
    }
}

#[component]
pub fn DateTime(
    datetime: chrono::DateTime<chrono::Local>
) -> impl IntoView {
    view! {
        <span class="icon_container">
            <leptos_icons::Icon icon=icondata::BiCalendarAltRegular />
            { human_datetime(&datetime) }
        </span>
    }
}