<?php
include 'koneksi.php';  // Menghubungkan ke database

// Menangani input pencarian
$search_query = "";
if (isset($_POST['search'])) {
    $search_query = mysqli_real_escape_string($conn, $_POST['search']);
}

// Query untuk menghitung total nominal berdasarkan hasil pencarian
$query_total = "SELECT SUM(i.nominal) AS total_iuran
                FROM iuran i
                JOIN anggota a ON i.id_anggota = a.id_anggota
                WHERE a.nama_anggota LIKE '%$search_query%' OR i.id_iuran LIKE '%$search_query%'";
$result_total = mysqli_query($conn, $query_total);

if (!$result_total) {
    die("Query Total Error: " . mysqli_error($conn));
}

$row_total = mysqli_fetch_assoc($result_total);
$total_iuran = $row_total['total_iuran'] ? number_format($row_total['total_iuran'], 0, ',', '.') : '0';

// Query untuk mengambil data iuran dengan filter pencarian
$query = "SELECT i.id_iuran, i.id_anggota, i.tanggal_iuran, i.nominal, i.date_created, a.nama_anggota 
          FROM iuran i
          JOIN anggota a ON i.id_anggota = a.id_anggota
          WHERE a.nama_anggota LIKE '%$search_query%' OR i.id_iuran LIKE '%$search_query%' 
          ORDER BY date_created DESC";

$result = mysqli_query($conn, $query);

if (!$result) {
    die("Query Error: " . mysqli_error($conn));
}

// Menangani penghapusan iuran
if (isset($_GET['hapus_id'])) {
    $id_iuran = $_GET['hapus_id'];

    if (is_numeric($id_iuran)) {
        $query_delete = "DELETE FROM iuran WHERE id_iuran = '$id_iuran'";
        if (mysqli_query($conn, $query_delete)) {
            echo "<script>alert('Iuran berhasil dihapus!'); window.location='?page=iuran';</script>";
        } else {
            echo "<script>alert('Gagal menghapus iuran.'); window.location='?page=iuran';</script>";
        }
    } else {
        echo "<script>alert('ID iuran tidak valid.'); window.location='?page=iuran';</script>";
    }
}

// Menambahkan Iuran
if (isset($_POST['submit'])) {
    $id_anggota = $_POST['id_anggota'];
    $tanggal_iuran = $_POST['tanggal_iuran'];
    $nominal = $_POST['nominal'];

    if (empty($id_anggota) || empty($tanggal_iuran) || empty($nominal)) {
        echo "<script>alert('Semua kolom harus diisi.');</script>";
    } else {
        $query_insert = "INSERT INTO iuran (id_anggota, tanggal_iuran, nominal, date_created) 
                         VALUES ('$id_anggota', '$tanggal_iuran', '$nominal', NOW())";

        if (mysqli_query($conn, $query_insert)) {
            echo "<script>alert('Iuran berhasil ditambahkan!'); window.location='?page=iuran';</script>";
        } else {
            echo "<script>alert('Gagal menambahkan iuran. Error: " . mysqli_error($conn) . "');</script>";
        }
    }
}
?>

<main class="main-content position-relative border-radius-lg ">
    <div class="container-fluid py-4">
        <div class="header-section mb-4">
            <h5 class="page-title text-white mb-3">Data Iuran Semua</h5>
        </div>
        <div class="card shadow-sm rounded-4">
            <div class="card-body">
                <div class="table-responsive">
                    <table id="DataTable" class="table table-hover table-bordered">
                        <thead>
                            <tr>
                                <th>No</th>
                                <th>Anggota</th>
                                <th>Tanggal Iuran</th>
                                <th>Tanggal Bayar</th>
                                <th>Nominal</th>
                                <th class="aksi">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $no = 1;
                            $jml = 0;
                            while ($row = mysqli_fetch_assoc($result)):
                                $jml += $row['nominal'];
                            ?>
                                <tr>
                                    <td><?php echo $no++; ?></td>
                                    <td><?php echo htmlspecialchars($row['nama_anggota']); ?></td>
                                    <td><?php echo date('d-m-Y', strtotime($row['tanggal_iuran'])); ?></td>
                                    <td><?php echo date('d-m-Y', strtotime($row['date_created'])); ?></td>
                                    <td>Rp <?php echo number_format($row['nominal'], 0, ',', '.'); ?></td>
                                    <td>
                                        <a href="?page=iuran&hapus_id=<?php echo htmlspecialchars($row['id_iuran']); ?>"
                                            class="btn btn-danger btn-sm"
                                            onclick="return confirm('Apakah Anda yakin ingin menghapus iuran ini?')">Hapus</a>
                                    </td>
                                </tr>
                            <?php endwhile; ?>
                        </tbody>
                        <tfoot>
                            <tr>
                                <td colspan="4">
                                    <center><b>Jumlah</b></center>
                                </td>
                                <td colspan="2"><b>Rp <?php echo number_format($jml, 0, ',', '.'); ?>,-</b></td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
    </div>
</main>