use yew::prelude::*; use web_sys::HtmlInputElement; use wasm_bindgen::JsCast; use crate::components::accounting::models::*; #[derive(Properties, PartialEq)] pub struct FinancialReportsTabProps { pub state: UseStateHandle, } #[function_component(FinancialReportsTab)] pub fn financial_reports_tab(props: &FinancialReportsTabProps) -> Html { let state = &props.state; let show_report_modal = use_state(|| false); let report_type = use_state(|| ReportType::ProfitLoss); let start_date = use_state(|| "".to_string()); let end_date = use_state(|| "".to_string()); let on_generate_report = { let state = state.clone(); let show_report_modal = show_report_modal.clone(); let report_type = report_type.clone(); let start_date = start_date.clone(); let end_date = end_date.clone(); Callback::from(move |_| { if start_date.is_empty() || end_date.is_empty() { web_sys::window() .unwrap() .alert_with_message("Please select both start and end dates") .unwrap(); return; } let new_report = FinancialReport { id: state.financial_reports.len() + 1, report_type: (*report_type).clone(), period_start: (*start_date).clone(), period_end: (*end_date).clone(), generated_date: js_sys::Date::new_0().to_iso_string().as_string().unwrap()[..10].to_string(), status: "Generated".to_string(), }; let mut new_state = (*state).clone(); new_state.financial_reports.push(new_report); state.set(new_state); show_report_modal.set(false); }) }; let on_export_report = { Callback::from(move |report_id: usize| { // Create CSV content for the report let csv_content = format!( "Financial Report Export\nReport ID: {}\nGenerated: {}\n\nThis is a placeholder for the actual report data.", report_id, js_sys::Date::new_0().to_iso_string().as_string().unwrap() ); // Create and download the file let blob = web_sys::Blob::new_with_str_sequence(&js_sys::Array::of1(&csv_content.into())).unwrap(); let url = web_sys::Url::create_object_url_with_blob(&blob).unwrap(); let document = web_sys::window().unwrap().document().unwrap(); let a = document.create_element("a").unwrap(); a.set_attribute("href", &url).unwrap(); a.set_attribute("download", &format!("financial_report_{}.csv", report_id)).unwrap(); a.dyn_ref::().unwrap().click(); web_sys::Url::revoke_object_url(&url).unwrap(); }) }; html! {

{"Financial Reports"}

if state.financial_reports.is_empty() {
{"No reports generated yet"}

{"Generate your first financial report to get started"}

} else {
{for state.financial_reports.iter().map(|report| { let report_id = report.id; let on_export = on_export_report.clone(); html! { } })}
{"Report Type"} {"Period"} {"Generated"} {"Status"} {"Actions"}
{format!("{:?}", report.report_type)} {format!("{} to {}", report.period_start, report.period_end)} {&report.generated_date} {&report.status}
}
// Report Generation Modal if *show_report_modal { }
} }