<?php
// auth/login.php
$pageTitle = 'Login';
require_once '../includes/config.php';
require_once '../includes/database.php';
require_once '../includes/functions.php';
require_once '../includes/auth.php';

// Mulai session jika belum dimulai
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// Redirect jika sudah login
if (isLoggedIn()) {
    if (isAdmin()) {
        redirect(SITE_URL . '/admin/dashboard.php');
    } else {
        redirect(SITE_URL . '/user/dashboard.php');
    }
}

// Inisialisasi auth class
$auth = new Auth();

// Proses login jika form disubmit
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    // Validasi input
    if (empty($username) || empty($password)) {
        setFlash('login_failed', 'Username dan password harus diisi', 'danger');
    } else {
        // Proses login
        $result = $auth->login($username, $password);
        
        // Jika login berhasil
if ($result['status']) {
    // Redirect berdasarkan role
    switch ($_SESSION['role']) {
        case 'admin':
            redirect(SITE_URL . '/admin/dashboard.php');
            break;
        case 'ketua':
            redirect(SITE_URL . '/ketua/dashboard.php');
            break;
        case 'bendahara':
            redirect(SITE_URL . '/bendahara/dashboard.php');
            break;
        case 'sekretaris':
            redirect(SITE_URL . '/sekretaris/dashboard.php');
            break;
        case 'pengawas':
            redirect(SITE_URL . '/pengawas/dashboard.php');
            break;
        case 'donatur':
            redirect(SITE_URL . '/user/dashboard.php');
            break;
        default:
            redirect(SITE_URL);
            break;
    }
    exit;
} else {
    setFlash('login_failed', $result['message'], 'danger');
}
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - <?= SITE_NAME ?></title>
    
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome Icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <!-- Custom CSS -->
    <link rel="stylesheet" href="<?= SITE_URL ?>/assets/css/style.css">
</head>
<body class="bg-light">
    <div class="container py-5">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="text-center mb-4">
                    <a href="<?= SITE_URL ?>">
                        <img src="<?= SITE_URL ?>/assets/img/logo.png" alt="<?= SITE_NAME ?>" height="60">
                    </a>
                </div>
                
                <div class="auth-form">
                    <h2 class="text-center mb-4">Login</h2>
                    
                    <?php flash(); ?>
                    
                    <form action="" method="post">
                        <div class="mb-3">
                            <label for="username" class="form-label">Username</label>
                            <div class="input-group">
                                <span class="input-group-text"><i class="fas fa-user"></i></span>
                                <input type="text" class="form-control" id="username" name="username" placeholder="Masukkan username" required>
                            </div>
                        </div>
                        
                        <div class="mb-3">
                            <label for="password" class="form-label">Password</label>
                            <div class="input-group">
                                <span class="input-group-text"><i class="fas fa-lock"></i></span>
                                <input type="password" class="form-control" id="password" name="password" placeholder="Masukkan password" required>
                            </div>
                        </div>
                        
                        <div class="mb-3 form-check">
                            <input type="checkbox" class="form-check-input" id="remember">
                            <label class="form-check-label" for="remember">Ingat saya</label>
                        </div>
                        
                        <div class="d-grid">
                            <button type="submit" class="btn btn-success">Login</button>
                        </div>
                    </form>
                    
                    <div class="mt-4 text-center">
                        <p>Belum punya akun? <a href="<?= SITE_URL ?>/auth/register.php" class="text-success">Daftar disini</a></p>
                        <p><a href="<?= SITE_URL ?>/auth/forgot-password.php" class="text-muted">Lupa password?</a></p>
                    </div>
                </div>
                
                <div class="text-center mt-4">
                    <a href="<?= SITE_URL ?>" class="text-muted"><i class="fas fa-arrow-left me-1"></i> Kembali ke Beranda</a>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>