use std::sync::Arc; use egui::{Sense, mutex::RwLock}; use egui_extras::Column; use crate::{ database::DBOperator, misc, models::Order, tab::{TABS_CAN_BE_WINDOWS, Tab, TabTypes}, }; pub struct OrderTabViewer { db_oper: DBOperator, rt: tokio::runtime::Runtime, incoming_orders: Arc>>, outcoming_orders: Arc>>, incoming_order_page: i32, outcoming_order_page: i32, is_admin: bool, } impl OrderTabViewer { pub fn new(is_admin: bool) -> Self { Self { is_admin, ..Default::default() } } pub fn show_orders(&mut self, ui: &mut egui::Ui, incoming_orders: bool) { let orders = if incoming_orders { &self.incoming_orders } else { &self.outcoming_orders }; ui.horizontal(|ui| { if ui.button("Добавить").clicked() {} if ui.button("<-").clicked() {} ui.monospace(if incoming_orders { self.incoming_order_page.to_string() } else { self.outcoming_order_page.to_string() }); if ui.button("->").clicked() {} }); egui_extras::TableBuilder::new(ui) .columns(Column::auto(), 5) .striped(true) .vscroll(true) .sense(Sense::click()) .header(25.0, |mut heading| { heading.col(|ui| { ui.heading(if incoming_orders { "Заказчик" } else { "Клиент" }); }); heading.col(|ui| { ui.heading("Состав"); }); heading.col(|ui| { ui.heading("Стоимость"); }); heading.col(|ui| { ui.heading("Дата создания"); }); }) .body(|mut body| { for or in orders.read().iter() { body.row(25.0, |mut row| { let len = orders.read().iter().clone().len(); row.col(|ui| { ui.label(&or.client.name); }); row.col(|ui| { ui.label(match len { 0 => "Пусто".to_owned(), _ => format!("{} пт.", len), }); }); row.col(|ui| { ui.label(len.to_string()); }); row.col(|ui| { ui.label(or.order_date.date_naive().to_string()); }); row.col(|ui| if ui.button("Удалить").clicked() {}); }); } }); } } impl egui_dock::TabViewer for OrderTabViewer { type Tab = Tab; fn title(&mut self, tab: &mut Self::Tab) -> egui::WidgetText { tab.title.clone().into() } fn ui(&mut self, ui: &mut egui::Ui, tab: &mut Self::Tab) { match tab.tab_type { TabTypes::IncomingOrderList => { self.show_orders(ui, true); } TabTypes::OutcomingOrderList => { self.show_orders(ui, false); } _ => { ui.label("Nope"); } } } fn allowed_in_windows(&self, _tab: &mut Self::Tab) -> bool { TABS_CAN_BE_WINDOWS } fn is_closeable(&self, _tab: &Self::Tab) -> bool { false } } impl Default for OrderTabViewer { fn default() -> Self { let rt = tokio::runtime::Runtime::new().unwrap(); let db_oper = rt.block_on(async { DBOperator::new().await }); Self { incoming_orders: Arc::new(RwLock::new( rt.block_on(async { db_oper.get_orders(0, misc::ELEMENTS_PER_PAGE, true).await }), )), outcoming_orders: Arc::new(RwLock::new( rt.block_on(async { db_oper.get_orders(0, misc::ELEMENTS_PER_PAGE, false).await }), )), incoming_order_page: 0, outcoming_order_page: 0, db_oper, rt, is_admin: false, } } }