<?php
session_start();

require_once '../koneksi.php';
require_once '../auth.php';

cekAdminAtauGuru();

$level = isset($_SESSION['role']) ? strtolower($_SESSION['role']) : '';
$id_guru_login = isset($_SESSION['id_guru']) ? $_SESSION['id_guru'] : 0;

$query_guru = mysqli_query($koneksi, "SELECT nama, jk FROM master_guru WHERE id_guru = '$id_guru_login'");
$guru = mysqli_fetch_assoc($query_guru);

$jk_raw = trim($guru['jk'] ?? '');
$sapaan = (strtoupper(substr($jk_raw, 0, 1)) === 'L') ? 'Bapak' : 'Ibu';
$nama_guru = $guru['nama'] ?? 'Guru';

$sql_count = "SELECT COUNT(*) as total FROM tujuan_pembelajaran";
$q_count = mysqli_query($koneksi, $sql_count);
$data = mysqli_fetch_assoc($q_count);
$total_tp = isset($data['total']) ? $data['total'] : 0;

include 'layout.php';
?>

<div class="container-fluid mt-4">
    <div class="alert alert-info shadow-sm mb-4">
        <h4 class="mb-0">Selamat datang, <?= $sapaan ?> <?= htmlspecialchars($nama_guru) ?>!</h4>
    </div>

    <div class="card p-4 shadow-sm">
        <h3>Dashboard <?= ucfirst($level) ?></h3>
        <hr>
        <div class="row">
            <div class="col-md-4">
                <div class="card bg-primary text-white p-3 shadow-sm mb-3">
                    <h5>Total Tujuan Pembelajaran</h5>
                    <h2 class="mt-2"><?= $total_tp ?></h2>
                </div>
            </div>

            <div class="col-md-4">
                <div class="card bg-success text-white p-3 shadow-sm mb-3">
                    <h5>Jadwal Pelajaran</h5>
                    <p class="mb-1">Kelola jadwal mengajar</p>
                    <div class="mt-2">
                        <a href="jadwal_pelajaran.php" class="btn btn-light btn-sm"><i class="fa fa-calendar"></i> Buka Jadwal</a>
                        <a href="input_jadwal.php" class="btn btn-primary btn-sm"><i class="fa fa-plus"></i> Tambah</a>
                    </div>
                </div>
            </div>

            <div class="col-md-4">
                <div class="card bg-warning text-dark p-3 shadow-sm mb-3">
                    <h5>Tabungan Siswa</h5>
                    <p class="mb-1">Kelola data tabungan siswa</p>
                    <div class="mt-2">
                        <a href="tabungan_siswa.php" class="btn btn-dark btn-sm text-white">
                            <i class="fa fa-money-bill-wave"></i> Buka Tabungan
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'layout_footer.php'; ?>