use leptos::*;
use leptos_router::*;
use crate::backend::data::User;
use crate::backend::user::UpdateProfile;
use crate::components::modal_box::{DialogOpener, ModalBody, ModalDialog, ModalFooter};
use crate::components::server_err::ServerErr;
use crate::components::validation_err::ValidationErr;
use crate::locales::trl;
use crate::validator::Validator;

#[component]
pub fn ProfileEdit(user: ReadSignal<User>, opener: DialogOpener) -> impl IntoView {
    let update_user = create_server_action::<UpdateProfile>();
    let upd_val = update_user.value();
    let validator = Validator::new();

    view! {
        <ActionForm
                on:submit=move |ev| {
                    let act = UpdateProfile::from_event(&ev);
                    if !act.is_err() {
                        validator.check(&act.unwrap().user, &ev);
                    }
                }
                action=update_user>
        <ModalDialog opener=opener title="Edit profile">
            <ModalBody>
                <ServerErr result={upd_val} opener=opener/>
                <ValidationErr validator=validator />
                <input type="hidden" value={move || user.get().login} name="user[login]"/>
                <div class="row">
                    <div class="col mb-3">
                        <label  for="name" class="form-label">"Full name"</label>
                        <input
                            type="text"
                            id="name"
                            class="form-control"
                            placeholder="Enter Full name"
                            prop:value={move || user.get().full_name}
                            name="user[full_name]"
                        />
                    </div>
                </div>
                <div class="row">
                    <div class="col mb-3">
                        <label  for="email" class="form-label">"Email"</label>
                        <input
                            type="text"
                            id="name"
                            class="form-control"
                            placeholder="Enter email"
                            prop:value={move || user.get().email.unwrap_or("".to_string())}
                            name="user[email]"
                        />
                    </div>
                </div>
                <div class="row">
                    <div class="col mb-3">
                        <input
                            class="form-check-input"
                            type="checkbox"
                            id="getMail"
                            prop:value={move || if user.get().get_emails {"true"} else {"false"}}
                            prop:checked={move || user.get().get_emails}
                            name="user[get_emails]"
                        />
                        <label class="form-check-label" for="getMail">"Get emails"</label>
                    </div>
                </div>
            </ModalBody>
            <ModalFooter>
                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"
                    on:click=move |_| {validator.reset(); opener.hide()}>
                    {trl("Close")}
                </button>
                <button type="submit" class="btn btn-primary">
                    {trl("Save changes")}
                </button>
            </ModalFooter>
        </ModalDialog>
        </ActionForm>
    }
}