<?php
$judul = "Tambah MOD";
$menu  = "mod";
include 'layout/header.php';

$users = mysqli_query($conn,"SELECT * FROM users WHERE status='aktif' ORDER BY nama ASC");

if(isset($_POST['simpan'])){
    $tanggal = mysqli_real_escape_string($conn, $_POST['tanggal']);
    $id_user = mysqli_real_escape_string($conn, $_POST['id_user']);

    $cek = mysqli_query($conn,"SELECT * FROM mod_harian WHERE tanggal='$tanggal'");

    if(mysqli_num_rows($cek) > 0){
        echo "<script>alert('MOD pada tanggal tersebut sudah ada');</script>";
    } else {
        mysqli_query($conn,"INSERT INTO mod_harian (tanggal, id_user) VALUES ('$tanggal', '$id_user')");
        header('Location: mod_harian.php');
        exit;
    }
}
?>

<?php include 'layout/sidebar.php'; ?>

<style>
    :root {
        --emerald-400: #34d399; --emerald-600: #059669;
        --surface: #03160e; --surface-1: #072216; --surface-2: #103524;
        --mint-border: rgba(16, 185, 129, 0.3);
        --text-primary: #f0fdf4; --text-secondary: #a7f3d0;
    }
    .card-custom { background-color: var(--surface-1); border: 1px solid var(--mint-border); border-radius: 12px; color: var(--text-primary); }
    .card-header { background: linear-gradient(135deg, var(--surface-2), #155235); color: var(--emerald-400); border-bottom: 1px solid var(--mint-border); font-weight: 700; }
    .form-label { color: var(--text-secondary); font-size: 0.85rem; }
    .form-control, .form-select { background-color: var(--surface); border: 1px solid var(--mint-border); color: var(--text-primary); }
    .form-control:focus, .form-select:focus { background-color: var(--surface-2); border-color: var(--emerald-400); box-shadow: 0 0 0 0.25rem rgba(52, 211, 153, 0.2); color: white; }
    .btn-save { background: var(--emerald-600); border: none; color: white; }
    .btn-save:hover { background: var(--emerald-400); }
</style>

<div class="container-fluid px-4 py-4">
    <div class="row">
        <div class="col-12 col-lg-8">
            <div class="card card-custom shadow-lg">
                <div class="card-header py-3">📝 Tambah MOD Harian</div>
                <div class="card-body p-4">
                    <form method="POST">
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Tanggal</label>
                                <input type="date" name="tanggal" class="form-control" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Pilih MOD</label>
                                <select name="id_user" class="form-select" required>
                                    <option value="">-- Pilih MOD --</option>
                                    <?php while($u=mysqli_fetch_assoc($users)): ?>
                                        <option value="<?= $u['id_user']; ?>">
                                            <?= $u['nama']; ?> (<?= ucfirst($u['role']); ?>)
                                        </option>
                                    <?php endwhile; ?>
                                </select>
                            </div>
                        </div>

                        <div class="pt-3 border-top mt-3" style="border-color: var(--mint-border);">
                            <button type="submit" name="simpan" class="btn btn-save px-4">Simpan MOD</button>
                            <a href="mod_harian.php" class="btn btn-outline-secondary px-4">Kembali</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'layout/footer.php'; ?>