use chrono::Local;
use leptos::*;
use crate::backend::data::WeekHours;
use crate::backend::opening_hours::{DeleteClosingTime, UpdateClosingTime, UpdateHours};
use crate::components::data_form::{DataForm, QuestionDialog};
use crate::components::modal_box::DialogOpener;
use crate::locales::trl;

#[component]
pub fn EditHours(opener: DialogOpener, hours: ReadSignal<WeekHours>) -> impl IntoView {
    let update_hours = create_server_action::<UpdateHours>();

    view! {
        <DataForm opener=opener action=update_hours title="Edit hours">
            <input type="hidden" value={move || hours.get().day().to_string()} name="hours[day]"/>
            <div class="row">
                <div class="col mb-3">
                    <label  for="hours" class="form-label">{trl("Hours")}</label>
                    <input
                        type="text"
                        id="hours"
                        class="form-control"
                        placeholder="12:00 - 15:00, 17:00 - 21:00"
                        prop:value={move || hours.get().hours().to_string()}
                        name="hours[hours]"
                    />
                </div>
            </div>
        </DataForm>
    }
}

#[component]
pub fn closing_days(opener: DialogOpener) -> impl IntoView {
    let update_days = create_server_action::<UpdateClosingTime>();

    view! {
        <DataForm opener=opener action=update_days title="Closing days">
            <input type="hidden" value=0 name="time[id]"/>
            <div class="row">
                <div class="col mb-3">
                    <label  for="from_day" class="form-label">{trl("From")}</label>
                    <input
                        type="date"
                        id="from_day"
                        class="form-control"
                        prop:value={move || Local::now().date_naive().format("%Y-%m-%d").to_string()}
                        name="time[from_date]"
                    />
                    <label  for="to_day" class="form-label">{trl("To")}</label>
                    <input
                        type="date"
                        id="to_day"
                        class="form-control"
                        prop:value={move || Local::now().date_naive().format("%Y-%m-%d").to_string()}
                        name="time[to_date]"
                    />
                </div>
            </div>
        </DataForm>
    }
}

#[component]
pub fn del_closing_days(opener: DialogOpener) -> impl IntoView {
    let delete = create_server_action::<DeleteClosingTime>();

    view! {
        <QuestionDialog opener=opener action=delete title="Delete closing days">
            <div>{trl("Are you sure you want to delete closing days?")}</div>
        </QuestionDialog>
    }
}