<?php include "header.php";?>
<?php include"koneksi.php";?>

<?php 
if(isset($_POST['btnsimpan'])){
    $tgl=date('Y-m-d');

    // ✅ KAPITAL HURUF PERTAMA SAJA
    $nama   = ucfirst(strtolower(htmlspecialchars($_POST['nama'], ENT_QUOTES)));
    $alamat = ucfirst(strtolower(htmlspecialchars($_POST['alamat'], ENT_QUOTES)));
    $tujuan = ucfirst(strtolower(htmlspecialchars($_POST['tujuan'], ENT_QUOTES)));
    $notel  = htmlspecialchars($_POST['notel'], ENT_QUOTES);

    // ✅ QUERY BENAR
    $simpan= mysqli_query($koneksi, 
    "INSERT INTO tabtamu (tanggal,nama,alamat,tujuan,notel) 
     VALUES('$tgl','$nama','$alamat','$tujuan','$notel')");

    if ($simpan) {
        echo "<script>
        Swal.fire({
            title: 'Berhasil!',
            text: 'Data berhasil disimpan',
            icon: 'success',
            showConfirmButton: false,
            timer: 10000,
            didOpen: () => {
                var sound = new Audio('assets/sound/a.mp3');
                sound.play().catch(e => console.log(e));
            }
        }).then(() => {
            window.location='?';
        });
        </script>";
    } else {
        echo "<script>
        Swal.fire({
            title: 'Gagal!',
            text: 'Data gagal disimpan',
            icon: 'error',
            didOpen: () => {
                var sound = new Audio('assets/sound/error.mp3');
                sound.play().catch(e => console.log(e));
            }
        }).then(() => {
            window.location='?';
        });
        </script>";
    }
}
?>

<!-- HEADING -->
<div class="head text-center">  
    <img src="assets/img/logo smkn 57.png">
    <h2 class="text-white">SISTEM INFORMASI BUKU PENERIMAAN TAMU</h2>
</div>

<!-- FORM -->
<div class="row mt-2">
<div class="col-lg-7 mb-3">
<div class="card shadow bg-gradient-light">
<div class="card-body">

<div class="text-center">
<h1 class="h4 text-gray-900 mb-4">Identitas Pengunjung</h1>
</div>

<form method="POST">

<input type="text" class="form-control form-control-user mb-2" name="nama"
placeholder="Nama Pengunjung"
onkeyup="this.value = this.value.charAt(0).toUpperCase() + this.value.slice(1).toLowerCase()"
required>

<input type="text" class="form-control form-control-user mb-2" name="alamat"
placeholder="Alamat Pengunjung"
onkeyup="this.value = this.value.charAt(0).toUpperCase() + this.value.slice(1).toLowerCase()"
required>

<input type="text" class="form-control form-control-user mb-2" name="tujuan"
placeholder="Tujuan Pengunjung"
onkeyup="this.value = this.value.charAt(0).toUpperCase() + this.value.slice(1).toLowerCase()"
required>

<input type="text" class="form-control form-control-user mb-2" name="notel"
placeholder="Nomor Telepon" required>

<button type="submit" name="btnsimpan" class="btn btn-primary btn-user btn-block">
Simpan Data  
</button>

</form>

<hr>
<div class="text-center">
<a class="small">By Zbot Invasion | 2026 - <?=date('Y')?></a>
</div>

</div> 
</div>
</div>

<!-- STATISTIK (TETAP ASLI) -->
<div class="col-lg-5 mb-3">
<div class="card shadow">
<div class="card-body">

<div class="text-center">
<h1 class="h4 text-gray-900 mb-4"> Statistik Pengunjung</h1>
</div>

<?php
$hari_ini = date('Y-m-d');
$kemarin = date('Y-m-d', strtotime('-1 day'));
$awal_minggu = date('Y-m-d', strtotime('monday this week'));
$akhir_minggu = date('Y-m-d');
$awal_bulan = date('Y-m-01');
$akhir_bulan = date('Y-m-t');

$q_hari_ini = mysqli_query($koneksi, "SELECT COUNT(*) as total FROM tabtamu WHERE DATE(tanggal) = '$hari_ini'");
$d_hari_ini = mysqli_fetch_assoc($q_hari_ini);

$q_kemarin = mysqli_query($koneksi, "SELECT COUNT(*) as total FROM tabtamu WHERE DATE(tanggal) = '$kemarin'");
$d_kemarin = mysqli_fetch_assoc($q_kemarin);

$q_minggu = mysqli_query($koneksi, "SELECT COUNT(*) as total FROM tabtamu 
WHERE DATE(tanggal) BETWEEN '$awal_minggu' AND '$akhir_minggu'");
$d_minggu = mysqli_fetch_assoc($q_minggu);

$q_bulan = mysqli_query($koneksi, "SELECT COUNT(*) as total FROM tabtamu 
WHERE DATE(tanggal) BETWEEN '$awal_bulan' AND '$akhir_bulan'");
$d_bulan = mysqli_fetch_assoc($q_bulan);
?>

<table class="table table-bordered">
<tr>
    <td>📅 Hari ini</td>
    <td>: <?= $d_hari_ini['total']; ?></td>
</tr>

<tr>
    <td>⏪ Kemarin</td>
    <td>: <?= $d_kemarin['total']; ?></td>
</tr>

<tr>
    <td>📈 Minggu ini</td>
    <td>: <?= $d_minggu['total']; ?></td>
</tr>

<tr>
    <td>🗓️ Bulan ini</td>
    <td>: <?= $d_bulan['total']; ?></td>
</tr>
</table>

</div>
</div>
</div>
</div>

<!-- TABEL -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">
Data Pengunjung Hari Ini [<?=date ('d-m-Y')?>]
</h6>
</div>

<div class="card-body">

<a href="rekapitulasi.php" class="btn btn-success mb-3">
<i class="fa fa-table"></i> Rekapitulasi Tamu
</a>

<table class="table table-bordered">
<thead>
<tr>
<th>No.</th>
<th>Tanggal</th>
<th>Nama</th>
<th>Alamat</th>
<th>Tujuan</th>
<th>No HP</th>
</tr>
</thead>

<tbody>
<?php
$tgl=date('Y-m-d');
$tampil = mysqli_query($koneksi, 
"SELECT * FROM tabtamu 
WHERE tanggal BETWEEN '$tgl 00:00:00' AND '$tgl 23:59:59' 
ORDER BY id DESC");

$no = 1;
while ($data = mysqli_fetch_array($tampil)) {
?>
<tr>
<td><?= $no++ ?></td>
<td><?= date('d-m-Y', strtotime($data['tanggal'])) ?></td>
<td><?= $data['nama'] ?></td>
<td><?= $data['alamat'] ?></td>
<td><?= $data['tujuan'] ?></td>
<td><?= $data['notel'] ?></td>
</tr>
<?php } ?>
</tbody>

</table>
</div>
</div>

<?php include "footer.php";?>