<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();
include "config/koneksi.php";

$error = "";

if (isset($_POST['login'])) {

    $email = $_POST['email'] ?? '';
    $password = $_POST['password'] ?? '';

    if ($email == '' || $password == '') {
        $error = "Email dan password wajib diisi";
    } else {

        $query = mysqli_query($conn, "SELECT * FROM admin WHERE email='$email'");
        $data = mysqli_fetch_assoc($query);

        if ($data && password_verify($password, $data['password'])) {

            $_SESSION['login'] = true;
            $_SESSION['id'] = $data['id'];
            $_SESSION['nama'] = $data['nama'];
            $_SESSION['role'] = $data['role'];

            if ($data['role'] == 'superadmin') {
                header("Location: /superadmin/index");
                exit;
            } elseif ($data['role'] == 'admin1') {
                header("Location: /admin/surat_masuk");
                exit;
            } elseif ($data['role'] == 'admin2') {
                header("Location: /admin2/surat_diterima");
                exit;
            } elseif ($data['role'] == 'admin3') {
                header("Location: /admin3/laporan_masuk");
                exit;
            }

        } else {
            $error = "Email atau password salah";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Login Admin</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" rel="stylesheet">

<style>

body{
    min-height:100vh;
    background:linear-gradient(135deg,#f8f9fa,#eef3f8);
    display:flex;
    justify-content:center;
    align-items:center;
    font-family:'Segoe UI',sans-serif;
}

.login-card{
    width:450px;
    background:#fff;
    border-radius:25px;
    padding:40px;
    box-shadow:0 15px 40px rgba(0,0,0,.08);
    border-top:6px solid #ff7a00;
}

.logo-wrapper{
    width:150px;
    height:150px;
    margin:auto;
    border-radius:50%;
    background:white;
    display:flex;
    align-items:center;
    justify-content:center;
    box-shadow:0 5px 20px rgba(0,0,0,.08);
    margin-bottom:20px;
}

.logo{
    width:120px;
    height:auto;
}

.app-title{
    font-size:30px;
    font-weight:700;
    color:#0d6efd;
    margin-top:15px;
}

.app-subtitle{
    font-size:15px;
    color:#666;
    margin-bottom:15px;
}

.form-control{
    height:50px;
    border-radius:12px;
}

.input-group-text{
    border-radius:12px 0 0 12px;
}

.btn-login{
    background:linear-gradient(90deg,#ff7a00,#ff9800);
    border:none;
    color:white;
    font-weight:600;
    height:52px;
    border-radius:14px;
    transition:.3s;
}

.btn-login:hover{
    transform:translateY(-3px);
    box-shadow:0 8px 20px rgba(255,122,0,.3);
    color:white;
}

.footer-text{
    font-size:12px;
    color:#888;
    margin-top:20px;
}

.alert{
    border-radius:12px;
}
.btn-kembali{
    background:#6c757d;
    border:none;
    color:white;
    font-weight:600;
    height:52px;
    border-radius:14px;
    transition:.3s;
    display:flex;
    align-items:center;
    justify-content:center;
    text-decoration:none;
}

.btn-kembali:hover{
    transform:translateY(-3px);
    box-shadow:0 8px 20px rgba(108,117,125,.3);
    color:white;
}

</style>
</head>

<body>

<div class="login-card">

    <div class="text-center">

        <div class="logo-wrapper">
            <img src="assets/bws1.png" class="logo" alt="Logo BWS">
        </div>
     

        <div class="app-title">
            Sistem Informasi Pelayanan Terpadu Sigap & Amanah
        </div>

        <div class="app-subtitle">
            Balai Wilayah Sungai Kalimantan IV
        </div>

    </div>

    <?php if(!empty($error)){ ?>
        <div class="alert alert-danger">
            <?= $error ?>
        </div>
    <?php } ?>

    <form method="POST">

        <div class="mb-3">
            <label class="mb-2">Email</label>

            <div class="input-group">
                <span class="input-group-text">
                    <i class="fas fa-envelope"></i>
                </span>

                <input
                    type="email"
                    name="email"
                    class="form-control"
                    placeholder="Masukkan email"
                    required>
            </div>
        </div>

        <div class="mb-4">
            <label class="mb-2">Password</label>

            <div class="input-group">
                <span class="input-group-text">
                    <i class="fas fa-lock"></i>
                </span>

                <input
                    type="password"
                    name="password"
                    class="form-control"
                    placeholder="Masukkan password"
                    required>
            </div>
        </div>
<div class="d-grid gap-2">

    <button type="submit" name="login" class="btn btn-login w-100">
        <i class="fas fa-sign-in-alt me-2"></i>
        LOGIN
    </button>

    <a href="../index" class="btn-kembali w-100">
        <i class="fas fa-arrow-left me-2"></i>
        KEMBALI
    </a>

</div>

    </form>

    <div class="footer-text text-center">
        Sistem Informasi Pelayanan Terpadu Sigap & Amanah<br>
        © <?= date('Y') ?> Balai Wilayah Sungai Kalimantan IV
    </div>

</div>

</body>
</html>

