use dioxus::prelude::*;
use dioxus::html::input_data::keyboard_types::Key;
use log::{info, LevelFilter};
use ProjectF::{Candidate, Login};

pub fn homepage(cx: Scope) -> Element {
    // let search_input = use_state(cx, String::new);
    let form_data = use_ref(cx, || Candidate::new());
    cx.render( rsx!(
        div {
            h1 { "Homepage" }
            input {
                "type": "text",
                value: "{form_data.read().name}",
                placeholder: "Search for doggo",
                oninput: move |evt| form_data.with_mut(|form_data| form_data.name = evt.value.clone()),
                onkeydown: move |evt| {
                    if evt.key() == Key::Enter {
                        info!("searching for doggo {}", form_data.read().name);
                    }
                }
            }
        }
        p {
            "You searched for: "
            b { "{form_data.read().name}" }
        }
    ))
}

pub fn login(cx: Scope) -> Element {
    // let login_data = use_ref(cx, || Login::new());
    let login_data = use_ref(&cx, || Login::new());
    let check_email = move |_| {
        let login_data_copy = login_data.clone();
        cx.spawn(async move {
            let client = reqwest::Client::new();
            let response = client.post("http://0.0.0.0:8777/api/login")
                .json(&*login_data_copy.read())
                .send()
                .await;
                // .unwrap()
                // .text()
            // .await;

            match response.unwrap().status() {
                reqwest::StatusCode::OK => info!("OK"),
                reqwest::StatusCode::NOT_FOUND => info!("KO"),
                reqwest::StatusCode::CONFLICT => info!("KO"),
                _ => info!("Unknown response"),
            }
            // match response {
            //     Ok(response) => info!("Response {}", response),
            //     Err(e) => info!("Error {}", e),
            // }
            // info!("Login {} {}", login_data_copy.read().email, login_data_copy.read().ine);
            // info!("Reponse {}", response.unwrap());
            ()
        });
    };
    cx.render(rsx! {
    input {
    "type": "text",
    value: "{login_data.read().email}",
    placeholder: "Email",
    oninput: move |evt| login_data.with_mut(|i| i.email = evt.value.clone()),
    onkeydown: move |evt| {
        if evt.key() == Key::Enter {
            info!("Input Email {}", login_data.read().email);
        }
    }
    }
    input {
    "type": "text",
    value: "{login_data.read().ine}",
    placeholder: "Numéro INE",
    oninput: move |evt| login_data.with_mut(|i| i.ine = evt.value.clone()),
    onkeydown: move |evt| {
        if evt.key() == Key::Enter {
            info!("Input INE {}", login_data.read().ine);
        }
    }
    }
        button {
        onclick: check_email,
        "Click to upload"
    }
})

}

pub fn other(cx: Scope) -> Element {
    cx.render( rsx!(
    div { h1 { "Other" } }
))
}

pub fn page_not_found(cx: Scope) -> Element {
    cx.render( rsx!(
    div { h1 { "Page not found" } }
))
}
