#include #include "ui.hpp" #include "imgui.h" #include "administration.hpp" #include "locales.hpp" static view_state current_view_state = view_state::LIST; static project selected_for_cancellation; static project active_project; void ui_setup_projects() { current_view_state = view_state::LIST; active_project = administration_project_create_empty(); } static void draw_project_form() { static const char* selected_country = NULL; if (ImGui::Button(localize("form.back"))) { current_view_state = view_state::LIST; active_project = administration_project_create_empty(); selected_country = 0; return; } ImGui::Spacing(); bool viewing_only = (current_view_state == view_state::VIEW); ImGui::BeginDisabled(); float widthAvailable = ImGui::GetContentRegionAvail().x; ImGui::SetNextItemWidth(widthAvailable*0.2f); ImGui::InputText(localize("project.form.identifier"), active_project.id, IM_ARRAYSIZE(active_project.id)); if (!viewing_only) ImGui::EndDisabled(); ImGui::SetNextItemWidth(widthAvailable*0.5f); ImGui::InputTextWithHint(localize("project.form.description"), localize("project.form.description"), active_project.description, IM_ARRAYSIZE(active_project.description)); ImGui::SameLine();ui_helper_draw_required_tag(); if (viewing_only) ImGui::EndDisabled(); if (!viewing_only) { bool can_save = administration_project_is_valid(active_project); if (!can_save) ImGui::BeginDisabled(); // Save button ImGui::Spacing(); if (ImGui::Button(localize("form.save"))) { if (current_view_state == view_state::CREATE) administration_project_add(active_project); else if (current_view_state == view_state::EDIT) administration_project_update(active_project); current_view_state = view_state::LIST; selected_country = 0; active_project = administration_project_create_empty(); } if (!can_save) ImGui::EndDisabled(); } else { // TODO list invoices connected to project. } } static void draw_project_list() { const u32 items_per_page = 50; static s32 current_page = 0; s32 max_page = (administration_project_count() + items_per_page - 1) / items_per_page; if (max_page == 0) max_page = 1; if (ImGui::Button(localize("form.create"))) { current_view_state = view_state::CREATE; active_project = administration_project_create_empty(); } if (current_page >= max_page-1) current_page = max_page-1; if (current_page < 0) current_page = 0; ImGui::SameLine(); bool enable_prev = current_page > 0; if (!enable_prev) ImGui::BeginDisabled(); if (ImGui::Button("<< Prev") && current_page > 0) current_page--; if (!enable_prev) ImGui::EndDisabled(); ImGui::SameLine(); ImGui::Text("(%d/%d)", current_page+1, max_page); ImGui::SameLine(); bool enable_next = current_page < max_page-1; if (!enable_next) ImGui::BeginDisabled(); if (ImGui::Button("Next >>") && current_page < max_page-1) current_page++; if (!enable_next) ImGui::EndDisabled(); ImGui::Spacing(); if (ImGui::BeginTable("TableProjects", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) { ImGui::TableSetupColumn(localize("project.table.identifier"), ImGuiTableColumnFlags_WidthFixed, 80); ImGui::TableSetupColumn(localize("project.table.status"), ImGuiTableColumnFlags_WidthFixed, 140); ImGui::TableSetupColumn(localize("project.table.description"), ImGuiTableColumnFlags_WidthStretch); ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 160); ImGui::TableHeadersRow(); project project_list[items_per_page]; u32 project_count = administration_project_get_partial_list(current_page, items_per_page, project_list); for (u32 i = 0; i < project_count; i++) { project c = project_list[i]; ImGui::TableNextRow(); ImGui::TableSetColumnIndex(0); ImGui::Text(c.id); ImGui::TableSetColumnIndex(1); ImGui::Text(localize(administration_project_get_status_string(c))); ImGui::TableSetColumnIndex(2); ImGui::Text(c.description); ImGui::TableSetColumnIndex(3); char btn_name[20]; snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.view"), i); if (ImGui::Button(btn_name)) { active_project = c; current_view_state = view_state::VIEW; } if (c.state == project_state::PROJECT_RUNNING) { ImGui::SameLine(); snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.change"), i); if (ImGui::Button(btn_name)) { active_project = c; current_view_state = view_state::EDIT; } ImGui::SameLine(); snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.cancel"), i); if (ImGui::Button(btn_name)) { selected_for_cancellation = c; ImGui::OpenPopup("ConfirmCancelProject"); } } } if (ImGui::BeginPopupModal("ConfirmCancelProject", nullptr, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoTitleBar)) { ImGui::Text(localize("form.confirmCancelProject")); ImGui::Separator(); if (ImGui::Button(localize("form.yes"), ImVec2(120, 0))) { administration_project_cancel(selected_for_cancellation); ImGui::CloseCurrentPopup(); } ImGui::SameLine(); if (ImGui::Button(localize("form.no"), ImVec2(120, 0))) { ImGui::CloseCurrentPopup(); } ImGui::EndPopup(); } ImGui::EndTable(); } } void ui_draw_projects() { switch(current_view_state) { case view_state::LIST: draw_project_list(); break; case view_state::CREATE: draw_project_form(); break; case view_state::EDIT: draw_project_form(); break; case view_state::VIEW: draw_project_form(); break; } }