/* * advotracker - Hotline tackingtool for Advocats * * Copyright 2020 Ralf Zerres * SPDX-License-Identifier: (0BSD or MIT) */ //use locales::t; use orbtk::prelude::*; use serde::Deserialize; //use std::process; //use std::collections::HashMap; use std::time::SystemTime; use tracing::{error, info, trace}; use crate::{ data::{constants::*, structures::Email}, widgets::global_state::GlobalState, services::exports::send_ticketdata::sendticketdata, widgets::ticketdata::ticketdata_view::TicketdataView, //widgets::policycheck::policycheck_state::PolicycheckAction, Lang, }; /// Valid `actions` that are handled as state changes in the `Ticketdata` widget. #[derive(Debug, Clone)] pub enum TicketdataAction { /// Clear text in the form ClearForm(String), ParseEntry(Entity), SendForm(), UpdatePolicyCode(String) } /// Define valid environment variables provided via .env files /// located in the current call directory. /// This is primarily used in testing scenarios (eg. debugging). #[derive(Debug, Deserialize)] struct Environment { test_lang: String, rust_log: String, } /// Valid `structures` that are handled inside the state of the `Ticket` widget. #[derive(AsAny, Default)] pub struct TicketdataState { actions: Vec, button_menu: Entity, lang: Lang, target: Entity } impl GlobalState for TicketdataState {} /// Method definitions, that react on any given state change inside the `Ticketdata` widget. impl TicketdataState { /// Clear the text property of all children of the given form entity pub fn clear_form(entity: Entity, id: &str, ctx: &mut Context<'_>) { // form is identified by its id if ctx.entity_of_child(id).is_some() { let form_entity = ctx.entity_of_child(ID_TICKET_DATA_FORM_GRID).unwrap(); let form_container: WidgetContainer<'_> = ctx.get_widget(form_entity); // BUG: Why does name defer from id, they are using the same constant? info!("Form id: {:?}", form_container.get::("id")); info!("Form name: {:?}", form_container.get::("name")); //ctx.clear_children_of(form_entity); // complete form is cleared! // switch into the context of the form_container ctx.change_into(form_entity); if let Some(count) = ctx.widget().children_count() { for i in 0..count { if let Some(child) = &mut ctx.try_child_from_index(i) { let child_name: &str = child.get::("name"); info!("child({:?}) name: {:?}", i, child_name); let child_id: &str = child.get::("id"); info!("child({:?}) name: {:?}", i, child_id); // TODO: check the orbtk type // if let child_type: bool = child.get::("id") = std::any::type_name::().to_string() { match child_name { "TextBox" => TextBox::text_set(child, ""), "ticket_data_policy_code" => TextBox::text_set(child, ""), _ => info!("don't act on types other than 'TextBox'"), } // match child_id { // "ticket_data_policy_code" => TextBox::text_set(child, ""), // "ticket_data_policy_holder" => TextBox::text_set(child, ""), // "ticket_data_policy_deductible" => TextBox::text_set(child, ""), // "ticket_data_policy_callback_number" => TextBox::text_set(child, ""), // "ticket_data_policy_callback_date" => TextBox::text_set(child, ""), // "ticket_data_policy_callback_harm_type" => TextBox::text_set(child, ""), // "ticket_data_policy_callback_ivr_comment" => TextBox::text_set(child, ""), // _ => info!("don't act on child_id '{:?}", child_id), // } } } } // switch back to parent entity ctx.change_into(entity); Stack::visibility_set(&mut ctx.child(ID_TICKET_DATA_ACTION_STACK), Visibility::Collapsed); Button::background_set(&mut ctx.child(ID_TICKET_DATA_ACTION_BUTTON_SEND), String::from("#008000")); Button::icon_set(&mut ctx.child(ID_TICKET_DATA_ACTION_BUTTON_SEND), material_icons_font::MD_SEND); } } pub fn send_form(entity: Entity, ctx: &mut Context<'_>, _lang: Lang) { info!("TicketdataState: processing entity[{:?}]", entity); let mail_to_index = *TicketdataView::selected_index_ref(&ctx.widget()) as usize; let mail_to_item = TicketdataView::mail_to_ref(&ctx.widget())[mail_to_index].clone(); info!("mail_to item[{:?}]: {:?}]", mail_to_index, mail_to_item); // create Email structures let email = Email { mail_to: mail_to_item.to_string(), mail_cc: PROP_MAIL_CC_1.to_string(), mail_bcc: PROP_MAIL_BCC_1.to_string(), mail_from: PROP_MAIL_FROM.to_string(), mail_reply: PROP_MAIL_REPLY.to_string(), subject: PROP_MAIL_SUBJECT.to_string(), policy_code: ctx.child(ID_TICKET_DATA_POLICY_CODE).get::("text").to_string(), policy_holder: ctx.child(ID_TICKET_DATA_POLICY_HOLDER).get::("text").to_string(), deductible: ctx.child(ID_TICKET_DATA_DEDUCTIBLE).get::("text").to_string(), callback_number: ctx.child(ID_TICKET_DATA_CALLBACK_NUMBER).get::("text").to_string(), callback_date: ctx.child(ID_TICKET_DATA_CALLBACK_DATE).get::("text").to_string(), harm_type: ctx.child(ID_TICKET_DATA_HARM_TYPE).get::("text").to_string(), ivr_comment: ctx.child(ID_TICKET_DATA_IVR_COMMENT).get::("text").to_string(), }; trace!("eMail fields: {:?}", email); // send email via service let lang = Lang::De(""); if let Err(err) = sendticketdata(&email, &lang) { error!("sendticketdata error: {:?}", err); Button::icon_brush_set(&mut ctx.child(ID_TICKET_DATA_ACTION_BUTTON_SEND), String::from("#008000")); //Button::foreground_set(&mut ctx.child(ID_TICKET_DATA_ACTION_BUTTON_SEND), String::from("#CC000000")); Button::background_set(&mut ctx.child(ID_TICKET_DATA_ACTION_BUTTON_SEND), String::from("#CC000000")); Button::icon_set(&mut ctx.child(ID_TICKET_DATA_ACTION_BUTTON_SEND), material_icons_font::MD_THUMB_DOWN); //Button::icon_set(&mut ctx.child(ID_TICKET_DATA_ACTION_BUTTON_SEND), material_icons_font::MD_ERROR); } else { Button::icon_brush_set(&mut ctx.child(ID_TICKET_DATA_ACTION_BUTTON_SEND), String::from("#FF0000")); //Button::foreground_set(&mut ctx.child(ID_TICKET_DATA_ACTION_BUTTON_SEND), String::from("#FF0000")); Button::background_set(&mut ctx.child(ID_TICKET_DATA_ACTION_BUTTON_SEND), String::from("#FF0000")); Button::icon_set(&mut ctx.child(ID_TICKET_DATA_ACTION_BUTTON_SEND), material_icons_font::MD_THUMB_UP); }; } pub fn update_policy_code(_entity: Entity, _id: &str, ctx: &mut Context<'_>) { Stack::visibility_set(&mut ctx.child(ID_TICKET_DATA_ACTION_STACK), Visibility::Visible); //self.actions.push(TicketdataAction::UpdatePolicyCode(entity)); } } /// Supported methods handled inside the `TicketState` impl State for TicketdataState { /// Initialize the state of widgets inside `TicketState` fn init(&mut self, _: &mut Registry, ctx: &mut Context<'_>) { let time_start= SystemTime::now(); trace!(target: "advotracker", ticketdata_state = "init", status = "started"); // Initialize required menu button entity self.button_menu = ctx .entity_of_child(ID_TICKET_DATA_BUTTON_MENU) .expect("TicketState.init: Can't find resource entity 'ID_TICKET_DATA_BUTTON_MENU'."); // initialize the entity object, that will receive messages self.target = Entity::from(ctx.widget().try_clone::("target") .expect("TicketState.init: Can't find resource entity 'target'.")); // Get language from environment //self.lang = TicketdataState::get_lang(); //let self.lang = Lang::De(""); Stack::visibility_set(&mut ctx.child(ID_TICKET_DATA_ACTION_STACK), Visibility::Collapsed); Button::icon_set(&mut ctx.child(ID_TICKET_DATA_ACTION_BUTTON_CLEAR), material_icons_font::MD_CLEAR); Button::icon_set(&mut ctx.child(ID_TICKET_DATA_ACTION_BUTTON_SEND), material_icons_font::MD_SEND); let time_end = SystemTime::now(); let duration = time_end.duration_since(time_start); trace!(target: "advotracker", ticketdata_state = "init", status = "finished", duration = ?duration); } /// The reader component of the message system handing `TicketdataState`` fn messages( &mut self, mut messages: MessageReader, _registry: &mut Registry, ctx: &mut Context<'_>, ) { for message in messages.read::() { match message { TicketdataAction::ClearForm(id) => { TicketdataState::clear_form(ctx.entity(), &id, ctx); } TicketdataAction::SendForm() => { TicketdataState::send_form(ctx.entity(), ctx, self.lang); } TicketdataAction::UpdatePolicyCode(id) => { TicketdataState::update_policy_code(ctx.entity(), &id, ctx); } _ => { println!("messages: action not implemented!"); } } } } fn update(&mut self, _registry: &mut Registry, ctx: &mut Context<'_>) { let actions: Vec = self.actions.drain(..).collect(); for action in actions { match action { TicketdataAction::ClearForm(ref id) => { ctx.send_message(TicketdataAction::ClearForm(id.to_string()), self.target); } // TicketdataAction::SendForm() => { // //ctx.send_message(TicketdataAction::SendForm(), self.ID_TICKETDATA_FORM); // info!("update: got send_message {:?}", action); // } _ => { println!("TicketdataAction: action not implemented!"); } } } } }