import 'package:flutter/material.dart';
import 'admin_page.dart';
import 'user_page.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(debugShowCheckedModeBanner: false, home: LoginPage());
  }
}

class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    TextEditingController adminNameController = TextEditingController();
    TextEditingController adminPasswordController = TextEditingController();

    return Scaffold(
      appBar: AppBar(
        title: Text(''),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () {
                showDialog(
                  context: context,
                  barrierDismissible: false,
                  builder: (context) => AlertDialog(
                    title: Text('Login Admin'),
                    content: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        TextField(
                          controller: adminNameController,
                          decoration: const InputDecoration(
                            labelText: 'Nama',
                          ),
                        ),
                        TextField(
                          controller: adminPasswordController,
                          decoration: const InputDecoration(
                            labelText: 'Password',
                          ),
                          obscureText: true,
                        ),
                      ],
                    ),
                    actions: [
                      TextButton(
                        onPressed: () {
                          Navigator.of(context).pop();
                          adminNameController.clear();
                          adminPasswordController.clear();
                        },
                        child: Text('Batal'),
                      ),
                      TextButton(
                        onPressed: () {
                          String adminName = adminNameController.text;
                          String adminPassword = adminPasswordController.text;
                          if (adminName == 'admin' &&
                              adminPassword == 'admin') {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => AdminPage()),
                            ).then((_) {
                              adminNameController.clear();
                              adminPasswordController.clear();
                            });
                          } else {
                            showDialog(
                              context: context,
                              builder: (context) => AlertDialog(
                                title: Text('Login Gagal'),
                                content: Text('Nama atau Password salah.'),
                                actions: [
                                  TextButton(
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                    },
                                    child: Text('OK'),
                                  ),
                                ],
                              ),
                            ).then((_) {
                              adminNameController.clear();
                              adminPasswordController.clear();
                            });
                          }
                        },
                        child: Text('Login'),
                      ),
                    ],
                  ),
                );
              },
              child: Text('Login sebagai Admin'),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => UserPage()),
                );
              },
              child: Text('Login sebagai User'),
            ),
            SizedBox(height: 20),
          ],
        ),
      ),
    );
  }
}
