using System;
using System.Windows.Forms;

namespace ProviderApp
{
    public partial class MainForm : Form
    {
        private UserControl currentControl = null;

        public MainForm()
        {
            InitializeComponent();
            btnTariffs.Click += (s, e) => LoadControl(new TariffsForm(), "Тарифы");
            btnClients.Click += (s, e) => LoadControl(new ClientsForm(), "Клиенты");
            btnContracts.Click += (s, e) => LoadControl(new ContractsForm(), "Договоры");
            btnAnalytics.Click += (s, e) => LoadControl(new AnalyticsForm(), "Аналитика");
            btnRefresh.Click += BtnRefresh_Click;
            btnLogout.Click += BtnLogout_Click;

            // Загружаем Dashboard стартовой страницей
            LoadControl(new DashboardForm(), "Главная");
        }

        private void LoadControl(UserControl control, string title)
        {
            if (currentControl != null)
            {
                panelContent.Controls.Remove(currentControl);
                currentControl.Dispose();
            }
            currentControl = control;
            control.Dock = DockStyle.Fill;
            panelContent.Controls.Add(control);
            control.BringToFront();
        }

        private void BtnRefresh_Click(object sender, EventArgs e)
        {
            // Обновляем текущую форму
            if (currentControl != null)
            {
                if (currentControl is DashboardForm)
                    ((DashboardForm)currentControl).RefreshData();
                else if (currentControl is TariffsForm)
                    ((TariffsForm)currentControl).RefreshData();
                else if (currentControl is ClientsForm)
                    ((ClientsForm)currentControl).RefreshData();
                else if (currentControl is ContractsForm)
                    ((ContractsForm)currentControl).RefreshData();
                else if (currentControl is AnalyticsForm)
                    ((AnalyticsForm)currentControl).RefreshData();
            }
        }

        private void BtnLogout_Click(object sender, EventArgs e)
        {
            AuthHelper.Logout();
            Application.Restart();
        }
    }
}