<?php
// login.php — Login Guru dengan verifikasi nombor telefon + logo/banner
require_once __DIR__.'/config.php';
session_start();
$msg='';

if ($_SERVER['REQUEST_METHOD']==='POST') {
  $guru_id = $_POST['guru_id'] ?? '';
  $no_tel  = trim($_POST['no_tel'] ?? '');

  if ($guru_id!=='' && $no_tel!=='') {
    $stmt=$mysqli->prepare("SELECT id,nama,no_tel FROM guru WHERE id=? LIMIT 1");
    $stmt->bind_param('i',$guru_id);
    $stmt->execute();
    $result=$stmt->get_result();
    if($row=$result->fetch_assoc()){
      if($row['no_tel']===$no_tel){
        // ✅ Nombor telefon sepadan
        $_SESSION['guru_id'] = (int)$row['id'];
        $_SESSION['guru_nama'] = $row['nama'];
        header('Location: guru.php');
        exit;
      } else {
        $msg="❌ Nombor telefon tidak sepadan dengan rekod.";
      }
    } else {
      $msg="❌ Nama guru tidak wujud.";
    }
    $stmt->close();
  } else {
    $msg="❌ Sila pilih nama dan isi nombor telefon.";
  }
}

// Ambil senarai guru ikut A-Z
$gurus = [];
$sql = "SELECT id, nama FROM guru ORDER BY nama ASC";
if ($res=$mysqli->query($sql)) {
  while($row=$res->fetch_assoc()) $gurus[]=$row;
  $res->free();
}
?>
<!DOCTYPE html>
<html lang="ms">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Login Guru</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
  .banner {
    background:#1976d2;
    color:#fff;
    padding:20px;
    text-align:center;
  }
  .banner img {
    height:80px;
    margin-bottom:10px;
  }
</style>
</head>
<body class="bg-light">

<!-- Banner -->
<div class="banner">
  <img src="logo.png" alt="Logo Sekolah">
  <h2 class="fw-bold">Sistem Keberadaan Guru</h2>
  <div>SMK MUHIBBAH SANDAKAN</div>
</div>

<div class="container" style="max-width:500px;margin-top:5vh">
  <div class="card shadow">
    <div class="card-header text-center bg-primary text-white fw-bold fs-4">
      👨‍🏫 Login Guru
    </div>
    <div class="card-body">
      <?php if($msg): ?><div class="alert alert-danger"><?=$msg?></div><?php endif; ?>
      <form method="post">
        <div class="mb-3">
          <label class="form-label fw-bold">Nama Guru</label>
          <select name="guru_id" class="form-select" required>
            <option value="">-- Pilih Nama --</option>
            <?php foreach($gurus as $g): ?>
              <option value="<?=$g['id']?>"><?=htmlspecialchars($g['nama'])?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="mb-3">
          <label class="form-label fw-bold">Nombor Telefon</label>
          <input type="text" name="no_tel" class="form-control" placeholder="cth: 0123456789" required>
        </div>
        <button type="submit" class="btn btn-primary w-100">Login</button>
      </form>
    </div>
  </div>
</div>
</body>
</html>
