<?php
session_start();

if (isset($_SESSION['id_admin'])) {
  header('location: ./dashboard.php');
}

if (isset($_POST['submit'])) {
  define('BASEPATH', dirname(__FILE__));
  include('../include/connection.php');

  $user = $_POST['username'];
  $pass = mysqli_real_escape_string($con, $_POST['pass']);

  if ($user == null || $pass == null) {

    echo '<script type="text/javascript">alert("Username / Password tidak boleh kosong");</script>';
  } else {

    $log = $con->prepare("SELECT * FROM t_admin WHERE username = ?") or die($con->error);
    $log->bind_param('s', $user);
    $log->execute();
    $log->store_result();
    $jml = $log->num_rows();
    $log->bind_result($id, $username, $fullname, $password);
    $log->fetch();

    if ($jml > 0) {

      if (password_verify($pass, $password)) {

        $_SESSION['id_admin']   = $id;
        $_SESSION['user']       = $fullname;

        header('location:./dashboard.php');
      } else {

        echo '<script type="text/javascript">alert("Password Salah");</script>';
      }
    } else {

      echo '<script type="text/javascript">alert("Username tidak dikenali");</script>';
    }
  }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no" name="viewport">
  <title>Login — E-Voting</title>

  <link rel="icon" href="../images/OSIS.png">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css">

  <style>
    body {
      margin: 0;
      padding: 0;
    }

    /* Background kanan */
    .bg-image {
      background: url('../images/waw - Copy.jpg') center/cover no-repeat;
      position: relative;
    }

    /* Overlay */
    .bg-image::before {
      content: "";
      position: absolute;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.5);
      top: 0;
      left: 0;
    }

    /* TEXT CENTER */
    .side-text {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      text-align: center;
      color: white;
      z-index: 2;
      width: 100%;
    }

    .side-text h1 {
      font-size: 42px;
      font-weight: 600;
    }

    .side-text p {
      font-size: 16px;
      opacity: 0.85;
    }

    /* Login card lebih clean */
    .login-box {
      padding: 40px;
    }

    .form-control {
      height: 45px;
      border-radius: 8px;
    }

    .btn-login {
      height: 45px;
      border-radius: 8px;
      background: #4e73df;
      border: none;
    }

    .btn-login:hover {
      background: #2e59d9;
    }
  </style>
</head>

<body>
  <div id="app">
    <section class="section">
      <div class="d-flex flex-wrap align-items-stretch">

        <!-- LEFT LOGIN -->
        <div class="col-lg-4 col-md-6 col-12 order-lg-1 min-vh-100 order-2 bg-white">
          <div class="login-box">

            <img src="../images/OSIS.png" width="70" class="rounded-circle mb-4">

            <h4>Selamat Datang <strong>Admin</strong></h4>
            <p class="text-muted">Silakan login untuk melanjutkan</p>

            <form method="post" action="">
              <div class="form-group">
                <label>Username</label>
                <input type="text" class="form-control" name="username" placeholder="Masukan Username">
              </div>

              <div class="form-group">
                <label>Password</label>
                <input type="password" class="form-control" name="pass" placeholder="Masukan Password">
              </div>

              <button type="submit" name="submit" class="btn btn-login btn-block text-white">
                Login
              </button>
            </form>

          </div>
        </div>

        <!-- RIGHT BACKGROUND -->
        <div class="col-lg-8 col-12 order-lg-2 order-1 min-vh-100"
          style="background: url('../images/kepsek.jpeg') center/cover no-repeat; position:relative;">

          <!-- Overlay -->
          <div style="position:absolute; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.5);"></div>

          <!-- Text -->
          <div class="side-text">
              <h1>
              <?php
              date_default_timezone_set("Asia/Jakarta");
              $hour = date("G");

              if ($hour <= 11) echo "Selamat Pagi";
              elseif ($hour <= 14) echo "Selamat Siang";
              elseif ($hour <= 17) echo "Selamat Sore";
              else echo "Selamat Malam";
              ?>
              </h1>
              <p>ADMIN PEMILIHAN OSIS 2026/2027</p>
          </div>

</div>

      </div>
    </section>
  </div>
</body>
</html>
