<?php
// index.php (Halaman Login)
session_start();
require 'config/database.php'; // Pastikan path file database benar

// Ambil pengaturan sekolah untuk identitas login
$nama_sekolah = 'MA Darul Ulum Palangka Raya';
$logo_file = 'assets/logo.png';

try {
    $stmt_p = $pdo->query("SELECT nama_sekolah, logo_sekolah FROM pengaturan_aplikasi LIMIT 1");
    $pengaturan = $stmt_p->fetch();
    if ($pengaturan) {
        $nama_sekolah = htmlspecialchars($pengaturan['nama_sekolah']);
        if(!empty($pengaturan['logo_sekolah'])) {
            $logo_file = 'assets/img/' . $pengaturan['logo_sekolah'];
        }
    }
} catch (PDOException $e) {
    error_log("Gagal mengambil pengaturan: " . $e->getMessage());
}

// Cek jika sudah login
if (isset($_SESSION['user_logged_in'])) {
    if ($_SESSION['role'] === 'admin' || $_SESSION['role'] === 'operator') {
        header("Location: admin/dashboard.php"); exit();
    } else {
        header("Location: nasabah/dashboard.php"); exit();
    }
}

$error = '';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST['username'] ?? '';
    $password_input = $_POST['password'] ?? '';

    try {
        // 1. CEK DI TABEL USERS (Admin & Operator)
        $stmt_u = $pdo->prepare("SELECT id, username, password, nama_lengkap, role FROM users WHERE username = ?");
        $stmt_u->execute([$username]);
        $user = $stmt_u->fetch();

        if ($user && password_verify($password_input, $user['password'])) {
            session_regenerate_id(true);
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['nama_lengkap'] = $user['nama_lengkap'];
            $_SESSION['role'] = $user['role'];
            $_SESSION['user_logged_in'] = true;
            header("Location: admin/dashboard.php");
            exit();
        }

        // 2. CEK DI TABEL SISWA (Nasabah)
        $stmt_s = $pdo->prepare("SELECT id_siswa, nis, nama_siswa, password, username FROM siswa WHERE username = ?");
        $stmt_s->execute([$username]);
        $siswa = $stmt_s->fetch();

        if ($siswa && password_verify($password_input, $siswa['password'])) {
            session_regenerate_id(true);
            $_SESSION['id_siswa'] = $siswa['id_siswa']; // ID Asli Siswa
            $_SESSION['username'] = $siswa['nis'];
            $_SESSION['nama_lengkap'] = $siswa['nama_siswa'];
            $_SESSION['role'] = 'siswa';
            $_SESSION['user_logged_in'] = true;
            header("Location: nasabah/dashboard.php");
            exit();
        }

        // 3. Jika gagal
        $error = "Username atau password salah.";

    } catch (PDOException $e) {
        $error = "Terjadi kesalahan database: " . $e->getMessage();
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <title>Login - MMS</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;500;600&display=swap" rel="stylesheet">
    <style>
      *, *:before, *:after { padding: 0; margin: 0; box-sizing: border-box; }
      body {
          background-color: #080710;
          display: flex; justify-content: center; align-items: center;
          min-height: 100vh; font-family: 'Poppins', sans-serif; padding: 20px;
      }
      .background { position: absolute; width: 100%; height: 100%; max-width: 430px; max-height: 520px; }
      .background .shape { height: 200px; width: 200px; position: absolute; border-radius: 50%; }
      .shape:first-child { background: linear-gradient(#1845ad, #23a2f6); left: -80px; top: -80px; }
      .shape:last-child { background: linear-gradient(to right, #ff512f, #f09819); right: -30px; bottom: -80px; }
      
      form {
          width: 100%; max-width: 400px;
          background-color: rgba(255, 255, 255, 0.13);
          border-radius: 10px; backdrop-filter: blur(10px);
          border: 2px solid rgba(255, 255, 255, 0.1);
          box-shadow: 0 0 40px rgba(8, 7, 16, 0.6);
          padding: 50px 35px; position: relative; z-index: 1;
      }
      form * { font-family: 'Poppins', sans-serif; color: #ffffff; outline: none; border: none; }
      .logo-container { text-align: center; margin-bottom: 10px; }
      .logo-container img { max-height: 80px; width: auto; }
      h3 { font-size: 22px; font-weight: 500; text-align: center; margin-bottom: 10px; }
      p.school-name { text-align: center; font-size: 13px; opacity: 0.8; margin-bottom: 20px; }
      label { display: block; margin-top: 20px; font-size: 16px; font-weight: 500; }
      input {
          display: block; height: 50px; width: 100%;
          background-color: rgba(255, 255, 255, 0.07);
          border-radius: 3px; padding: 0 10px; margin-top: 8px;
          font-size: 14px; font-weight: 300;
      }
      button {
          margin-top: 40px; width: 100%;
          background-color: #ffffff; color: #080710;
          padding: 15px 0; font-size: 18px; font-weight: 600;
          border-radius: 5px; cursor: pointer; transition: 0.3s;
      }
      button:hover { background-color: #e5e5e5; }
      .error-msg { color: #ff512f; text-align: center; font-size: 14px; margin-top: 15px; }
    </style>
</head>
<body>
    <div class="background">
        <div class="shape"></div>
        <div class="shape"></div>
    </div>
    
    <form method="POST">
        <div class="logo-container">
            <img src="<?= $logo_file ?>" alt="Logo">
        </div>
        <h3>LOGIN AREA</h3>
        <p class="school-name"><?= $nama_sekolah ?></p>

        <?php if (!empty($error)): ?>
            <div class="error-msg"><?= htmlspecialchars($error) ?></div>
        <?php endif; ?>

        <label for="username">Username</label>
        <input type="text" placeholder="Masukkan NIS atau Username" id="username" name="username" required>

        <label for="password">Password</label>
        <input type="password" placeholder="Isi Password" id="password" name="password" required>

        <button type="submit">Log In</button>
    </form>
</body>
</html>