<?php
session_start();
if (empty($_SESSION['SESSION_USER']) && empty($_SESSION['SESSION_ID'])) {
    header('location:../../login/');
    exit;
} else {
    require_once '../../../library/sw-config.php';
    require_once '../../login/login_session.php';
    include('../../../library/sw-function.php');

    switch (@$_GET['action']) {
        /* -------  LOAD DATA AKTIVITAS ----------*/
        case 'aktivitas':
            $error = [];

            if (empty($_GET['id'])) {
                $error[] = 'ID tidak boleh kosong';
            } else {
                $id = mysqli_real_escape_string($connection, $_GET['id']);
            }

            // Ambil bulan & tahun
            if (!empty($_POST['month']) && !empty($_POST['year'])) {
                $bulan = $_POST['month'];
                $tahun = $_POST['year'];
            } else {
                $bulan = date("m");
                $tahun = date("Y");
            }

            if (empty($error)) {
                $filter = "id='$id' AND MONTH(tanggal)='$bulan' AND YEAR(tanggal)='$tahun'";
                $query  = "SELECT * FROM aktivitas WHERE $filter ORDER BY tanggal ASC";
                $result = $connection->query($query);

                echo '
                <div class="table-responsive">
                <table class="table table-bordered table-hover" id="swdatatable">
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>Tanggal</th>
                            <th>Nama</th>
                            <th>Aktivitas</th>
                            <th>Kendala</th>
                            <th>Solusi</th>
                            <th>Status</th>
                            <th>Foto</th>
                        </tr>
                    </thead>
                    <tbody>';

                if ($result && $result->num_rows > 0) {
                    $no = 1;
                    while ($row = $result->fetch_assoc()) {
                        echo '
                        <tr>
                            <td>'.$no++.'</td>
                            <td>'.tgl_indo($row['tanggal']).'</td>
                            <td>'.$row['nama'].'</td>
                            <td>
                                1. '.$row['aktivitas_1'].'<br>
                                2. '.$row['aktivitas_2'].'<br>
                                3. '.$row['aktivitas_3'].'<br>
                                4. '.$row['aktivitas_4'].'<br>
                                5. '.$row['aktivitas_5'].'
                            </td>
                            <td>'.$row['kendala'].'</td>
                            <td>'.$row['solusi'].'</td>
                            <td>'.$row['status'].'</td>
                            <td>';
                                if (!empty($row['foto'])) {
                                    echo '<a class="image-link" href="../../../uploads/'.$row['foto'].'" target="_blank">
                                            <img src="../../../uploads/'.$row['foto'].'" width="50">
                                          </a>';
                                }
                        echo '</td>
                        </tr>';
                    }
                } else {
                    echo '<tr><td colspan="8" class="text-center">Tidak ada data aktivitas</td></tr>';
                }

                echo '
                    </tbody>
                </table>
                </div>';

                echo '
                <script>
                    $("#swdatatable").dataTable({
                        "iDisplayLength": 20,
                        "aLengthMenu": [[20, 50, 100, -1], [20, 50, 100, "All"]]
                    });
                    $(".image-link").magnificPopup({type:"image"});
                </script>';
            } else {
                echo 'Data tidak ditemukan';
            }
        break;
    }
}
