Inhalt

Aktueller Ordner: duesseldorfer-schuelerinventar-csharp-client/Duesk/Forms
⬅ Übergeordnet

MainForm.cs

using Duesk.Api;
using Duesk.Api.Models;

namespace Duesk.Forms;

public partial class MainForm : Form
{
    private DataGridView dgvProfiles;
    private ToolStrip toolStrip;
    private ToolStripButton btnNew;
    private ToolStripButton btnRefresh;
    private ToolStripButton btnGroups;
    private ToolStripButton btnLogout;
    private StatusStrip statusStrip;
    private ToolStripStatusLabel statusLabel;
    
    private List<Profile> _profiles = new List<Profile>();
    
    public MainForm()
    {
        InitializeComponent();
        LoadProfiles();
    }
    
    private void InitializeComponent()
    {
        this.Text = "DÜSK - Düsseldorfer Schülerinventar";
        this.Size = new Size(900, 600);
        this.StartPosition = FormStartPosition.CenterScreen;
        
        // Toolbar
        toolStrip = new ToolStrip();
        btnNew = new ToolStripButton("Neues Profil", null, (s, e) => NewProfile());
        btnRefresh = new ToolStripButton("Aktualisieren", null, (s, e) => LoadProfiles());
        btnGroups = new ToolStripButton("Gruppen", null, (s, e) => ManageGroups());
        btnLogout = new ToolStripButton("Abmelden", null, (s, e) => Logout());
        
        toolStrip.Items.Add(btnNew);
        toolStrip.Items.Add(btnRefresh);
        toolStrip.Items.Add(btnGroups);
        toolStrip.Items.Add(new ToolStripSeparator());
        toolStrip.Items.Add(btnLogout);
        
        // DataGridView
        dgvProfiles = new DataGridView
        {
            Dock = DockStyle.Fill,
            AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
            SelectionMode = DataGridViewSelectionMode.FullRowSelect,
            MultiSelect = false,
            ReadOnly = true,
            AllowUserToAddRows = false
        };
        
        dgvProfiles.Columns.Add("Name", "Name");
        dgvProfiles.Columns.Add("Group", "Gruppe");
        dgvProfiles.Columns.Add("Id", "ProfilID");
        
        dgvProfiles.CellDoubleClick += (s, e) => ViewProfile();
        
        // Context Menu
        var contextMenu = new ContextMenuStrip();
        contextMenu.Items.Add("Anzeigen", null, (s, e) => ViewProfile());
        contextMenu.Items.Add("Bearbeiten", null, (s, e) => EditProfile());
        contextMenu.Items.Add("Löschen", null, (s, e) => DeleteProfile());
        contextMenu.Items.Add("-");
        contextMenu.Items.Add("Zeitreihe", null, (s, e) => ShowTimeSeries());
        dgvProfiles.ContextMenuStrip = contextMenu;
        
        // Statusbar
        statusStrip = new StatusStrip();
        statusLabel = new ToolStripStatusLabel("Bereit");
        statusStrip.Items.Add(statusLabel);
        
        this.Controls.Add(dgvProfiles);
        this.Controls.Add(toolStrip);
        this.Controls.Add(statusStrip);
        
        this.FormClosed += (s, e) => Application.Exit();
    }
    
    private async void LoadProfiles()
    {
        statusLabel.Text = "Lade Profile...";
        Cursor = Cursors.WaitCursor;
        
        try
        {
            _profiles = await ApiClient.GetProfilesAsync() ?? new List<Profile>();
            
            dgvProfiles.Rows.Clear();
            foreach (var profile in _profiles)
            {
                dgvProfiles.Rows.Add(profile.Name, profile.GroupName, profile.ProfileId);
            }
            
            statusLabel.Text = $"{_profiles.Count} Profile geladen";
        }
        catch (Exception ex)
        {
            statusLabel.Text = "Fehler beim Laden";
            MessageBox.Show($"Fehler: {ex.Message}", "Fehler",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            Cursor = Cursors.Default;
        }
    }
    
    private Profile? GetSelectedProfile()
    {
        if (dgvProfiles.SelectedRows.Count == 0) return null;
        int index = dgvProfiles.SelectedRows[0].Index;
        return index < _profiles.Count ? _profiles[index] : null;
    }
    
    private void NewProfile()
    {
        var dialog = new ProfileEditForm(null);
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            LoadProfiles();
        }
    }
    
    private void EditProfile()
    {
        var profile = GetSelectedProfile();
        if (profile == null)
        {
            MessageBox.Show("Bitte wählen Sie ein Profil aus.", "Hinweis",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
        
        var dialog = new ProfileEditForm(profile);
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            LoadProfiles();
        }
    }
    
    private void ViewProfile()
    {
        var profile = GetSelectedProfile();
        if (profile == null)
        {
            MessageBox.Show("Bitte wählen Sie ein Profil aus.", "Hinweis",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
        
        var dialog = new ProfileDetailForm(profile);
        dialog.ShowDialog();
    }
    
    private async void DeleteProfile()
    {
        var profile = GetSelectedProfile();
        if (profile == null)
        {
            MessageBox.Show("Bitte wählen Sie ein Profil aus.", "Hinweis",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
        
        var result = MessageBox.Show($"Möchten Sie das Profil '{profile.Name}' wirklich löschen?",
            "Profil löschen", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        
        if (result == DialogResult.Yes)
        {
            statusLabel.Text = "Lösche Profil...";
            Cursor = Cursors.WaitCursor;
            
            var success = await ApiClient.DeleteProfileAsync(profile.ProfileId!);
            
            Cursor = Cursors.Default;
            
            if (success)
            {
                LoadProfiles();
            }
            else
            {
                MessageBox.Show("Fehler beim Löschen des Profils.", "Fehler",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
    
    private async void ShowTimeSeries()
    {
        var profile = GetSelectedProfile();
        if (profile == null)
        {
            MessageBox.Show("Bitte wählen Sie ein Profil aus.", "Hinweis",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
        
        if (string.IsNullOrEmpty(profile.GroupId))
        {
            MessageBox.Show("Dieses Profil gehört keiner Gruppe an.", "Hinweis",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
        
        // Alle Profile der gleichen Gruppe laden
        var allProfiles = await ApiClient.GetProfilesAsync();
        var groupProfiles = allProfiles?.Where(p => p.GroupId == profile.GroupId).ToList();
        
        if (groupProfiles == null || groupProfiles.Count == 0)
        {
            MessageBox.Show("Keine weiteren Profile in dieser Gruppe.", "Hinweis",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
        
        var dialog = new TimeSeriesForm(groupProfiles, profile.GroupName ?? "Gruppe");
        dialog.ShowDialog();
    }
    
    private void ManageGroups()
    {
        var dialog = new GroupManagerForm();
        dialog.ShowDialog();
        LoadProfiles(); // Gruppen haben sich möglicherweise geändert
    }
    
    private void Logout()
    {
        SessionManager.Instance.Clear();
        this.Close();
        new LoginForm().Show();
    }
}