using System.Windows;

namespace ych
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            var login = new LoginWindow();

            // ВАЖНО: предотвращаем закрытие приложения
            this.ShutdownMode = ShutdownMode.OnExplicitShutdown;

            bool? result = login.ShowDialog();

            if (result == true)
            {
                try
                {
                    var main = new MainWindow(login.UserRole);
                    this.ShutdownMode = ShutdownMode.OnLastWindowClose;
                    main.Show();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Ошибка в MainWindow: " + ex.Message);
                    this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                    Shutdown();
                }
            }
            else
            {
                this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                Shutdown();
            }
        }
    }
}