<?php
if ($mod == '') {
    header('location:../404');
    echo 'kosong';
} else {
    include_once 'mod/sw-header.php';
    include 'sw-config.php'; // 🔹 koneksi

    if (!isset($_COOKIE['COOKIES_MEMBER'])) {
        header("location:index");
    } else {
        // 🚀 Ambil nama dari login
        $employees_name = ucfirst($row_user['employees_name']);

        // 🚀 Proses simpan data aktivitas
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $tanggal     = trim($_POST['tanggal']);
            $nama        = $employees_name;
            $aktivitas_1 = trim($_POST['aktivitas_1']);
            $aktivitas_2 = trim($_POST['aktivitas_2']);
            $aktivitas_3 = trim($_POST['aktivitas_3']);
            $aktivitas_4 = trim($_POST['aktivitas_4']);
            $aktivitas_5 = trim($_POST['aktivitas_5']);
            $kendala     = trim($_POST['kendala']);
            $solusi      = trim($_POST['solusi']);
            $status      = trim($_POST['status']);

            // Upload Foto
            $foto = "";
            if (isset($_FILES['foto']) && $_FILES['foto']['error'] == 0) {
                $target = "uploads/" . basename($_FILES['foto']['name']);
                if (move_uploaded_file($_FILES['foto']['tmp_name'], $target)) {
                    $foto = $_FILES['foto']['name'];
                }
            }

            // 🚀 Simpan ke database
            $sql = "INSERT INTO aktivitas 
                    (tanggal, nama, aktivitas_1, aktivitas_2, aktivitas_3, aktivitas_4, aktivitas_5, kendala, solusi, status, foto, created_at)
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())";

            $stmt = $connection->prepare($sql);

            if ($stmt === false) {
                echo '<div class="alert alert-danger m-3">❌ Query Error: ' . htmlspecialchars($connection->error) . '</div>';
            } else {
                $stmt->bind_param(
                    "sssssssssss",
                    $tanggal, $nama, $aktivitas_1, $aktivitas_2, $aktivitas_3,
                    $aktivitas_4, $aktivitas_5, $kendala, $solusi, $status, $foto
                );

                if ($stmt->execute()) {
                    // ✅ Notifikasi sukses
                    echo '<div class="alert alert-success m-3 text-center">✅ Data aktivitas berhasil disimpan</div>';
                    echo '<meta http-equiv="refresh" content="2;url=?mod=aktivitas">';
                } else {
                    echo '<div class="alert alert-danger m-3">❌ Gagal menyimpan data: ' . htmlspecialchars($stmt->error) . '</div>';
                }
                $stmt->close();
            }
        }

        // 🚀 Form Input Aktivitas
        echo '
        <!-- App Capsule -->
        <div id="appCapsule">
            <div class="section mt-3 text-center">
                <h3>📝 Formulir Aktivitas</h3>
            </div>

            <div class="section mt-2 mb-2">
                <div class="card">
                    <div class="card-body">
                        <form action="" method="POST" enctype="multipart/form-data">

                            <!-- Tanggal -->
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Tanggal</label>
                                    <input type="date" name="tanggal" class="form-control" required>
                                </div>
                            </div>

                            <!-- Nama -->
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Nama</label>
                                    <input type="text" class="form-control" value="'.$employees_name.'" readonly>
                                    <input type="hidden" name="nama" value="'.$employees_name.'">
                                </div>
                            </div>

                            <!-- Aktivitas -->
                            <div class="form-group boxed"><div class="input-wrapper">
                                <label class="label">Aktivitas 1</label>
                                <input type="text" name="aktivitas_1" class="form-control">
                            </div></div>
                            <div class="form-group boxed"><div class="input-wrapper">
                                <label class="label">Aktivitas 2</label>
                                <input type="text" name="aktivitas_2" class="form-control">
                            </div></div>
                            <div class="form-group boxed"><div class="input-wrapper">
                                <label class="label">Aktivitas 3</label>
                                <input type="text" name="aktivitas_3" class="form-control">
                            </div></div>
                            <div class="form-group boxed"><div class="input-wrapper">
                                <label class="label">Aktivitas 4</label>
                                <input type="text" name="aktivitas_4" class="form-control">
                            </div></div>
                            <div class="form-group boxed"><div class="input-wrapper">
                                <label class="label">Aktivitas 5</label>
                                <input type="text" name="aktivitas_5" class="form-control">
                            </div></div>

                            <!-- Kendala -->
                            <div class="form-group boxed"><div class="input-wrapper">
                                <label class="label">Kendala</label>
                                <textarea name="kendala" class="form-control"></textarea>
                            </div></div>

                            <!-- Solusi -->
                            <div class="form-group boxed"><div class="input-wrapper">
                                <label class="label">Solusi</label>
                                <textarea name="solusi" class="form-control"></textarea>
                            </div></div>

                            <!-- Status -->
                            <div class="form-group boxed"><div class="input-wrapper">
                                <label class="label">Status</label>
                                <select class="form-control custom-select" name="status" required>
                                    <option value="Pending">Pending</option>
                                    <option value="Selesai">Selesai</option>
                                    <option value="Konsultasi">Konsultasi</option>
                                </select>
                            </div></div>

                            <!-- Foto -->
                            <div class="form-group boxed"><div class="input-wrapper">
                                <label class="label">Upload Foto</label>
                                <input type="file" name="foto" class="form-control">
                            </div></div>

                            <hr>
                            <button type="submit" class="btn btn-primary btn-block">✅ Simpan</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <!-- * App Capsule -->
        ';
    }

    include_once 'mod/sw-footer.php';
}
?>
