<?php
/**
 * Pengurus (Committee/Board) Management
 * Koperasi Management System
 */

$pageTitle = 'Pengurus';
$pageSubtitle = 'Kelola data pengurus koperasi';

require_once __DIR__ . '/config/database.php';
$db = getDB();

// Handle Actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';

    if ($action === 'add') {
        $stmt = $db->prepare("INSERT INTO pengurus (nama, jabatan, nik, tempat_lahir, tanggal_lahir, jenis_kelamin, alamat, telepon, email, foto, status, tanggal_mulai) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?)");
        $stmt->execute([
            sanitize($_POST['nama']),
            sanitize($_POST['jabatan']),
            sanitize($_POST['nik'] ?? ''),
            sanitize($_POST['tempat_lahir'] ?? ''),
            $_POST['tanggal_lahir'] ?: null,
            $_POST['jenis_kelamin'] ?? 'L',
            sanitize($_POST['alamat'] ?? ''),
            sanitize($_POST['telepon'] ?? ''),
            sanitize($_POST['email'] ?? ''),
            $_POST['status'] ?? 'aktif',
            $_POST['tanggal_mulai'] ?: date('Y-m-d')
        ]);
        
        setFlash('success', "Pengurus berhasil ditambahkan.");
        header('Location: pengurus.php');
        exit;
    }
    
    if ($action === 'edit') {
        $id = $_POST['id'];

        $stmt = $db->prepare("UPDATE pengurus SET nama=?, jabatan=?, nik=?, tempat_lahir=?, tanggal_lahir=?, jenis_kelamin=?, alamat=?, telepon=?, email=?, status=?, tanggal_mulai=?, tanggal_selesai=? WHERE id=?");
        $stmt->execute([
            sanitize($_POST['nama']),
            sanitize($_POST['jabatan']),
            sanitize($_POST['nik'] ?? ''),
            sanitize($_POST['tempat_lahir'] ?? ''),
            $_POST['tanggal_lahir'] ?: null,
            $_POST['jenis_kelamin'] ?? 'L',
            sanitize($_POST['alamat'] ?? ''),
            sanitize($_POST['telepon'] ?? ''),
            sanitize($_POST['email'] ?? ''),
            $_POST['status'] ?? 'aktif',
            $_POST['tanggal_mulai'] ?: null,
            $_POST['tanggal_selesai'] ?: null,
            $id
        ]);
        setFlash('success', 'Data pengurus berhasil diperbarui.');
        header('Location: pengurus.php');
        exit;
    }
    
    if ($action === 'delete') {
        $id = $_POST['id'];
        $stmt = $db->prepare("DELETE FROM pengurus WHERE id = ?");
        $stmt->execute([$id]);
        setFlash('success', 'Pengurus berhasil dihapus.');
        header('Location: pengurus.php');
        exit;
    }

    if ($action === 'delete_all') {
        if (!empty($_POST['ids'])) {
            $ids = $_POST['ids'];
            $placeholders = str_repeat('?,', count($ids) - 1) . '?';
            
            $stmt = $db->prepare("DELETE FROM pengurus WHERE id IN ($placeholders)");
            $stmt->execute($ids);
            setFlash('success', count($ids) . ' pengurus berhasil dihapus.');
        } else {
            setFlash('warning', 'Pilih pengurus yang akan dihapus.');
        }
        header('Location: pengurus.php');
        exit;
    }
}

// Search & Filter
$search = $_GET['search'] ?? '';
$status = $_GET['status'] ?? '';
$page = max(1, intval($_GET['page'] ?? 1));
$perPage = 10;
$offset = ($page - 1) * $perPage;

$where = "WHERE 1=1";
$params = [];

if ($search) {
    $where .= " AND (p.nama LIKE ? OR p.jabatan LIKE ? OR p.telepon LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
    $params[] = "%$search%";
}

if ($status) {
    $where .= " AND p.status = ?";
    $params[] = $status;
}

$countStmt = $db->prepare("SELECT COUNT(*) FROM pengurus p LEFT JOIN anggota a ON p.nik = a.nik $where");
$countStmt->execute($params);
$totalRows = $countStmt->fetchColumn();
$totalPages = ceil($totalRows / $perPage);

$stmt = $db->prepare("SELECT p.*, a.foto AS anggota_foto FROM pengurus p LEFT JOIN anggota a ON p.nik = a.nik $where ORDER BY p.status ASC, p.nama ASC LIMIT $perPage OFFSET $offset");
$stmt->execute($params);
$pengurusList = $stmt->fetchAll();

// For modals
$editData = null;
if (isset($_GET['edit'])) {
    $editStmt = $db->prepare("SELECT p.*, a.foto AS anggota_foto FROM pengurus p LEFT JOIN anggota a ON p.nik = a.nik WHERE p.id = ?");
    $editStmt->execute([$_GET['edit']]);
    $editData = $editStmt->fetch();
}

$viewData = null;
if (isset($_GET['view'])) {
    $viewStmt = $db->prepare("SELECT p.*, a.foto AS anggota_foto FROM pengurus p LEFT JOIN anggota a ON p.nik = a.nik WHERE p.id = ?");
    $viewStmt->execute([$_GET['view']]);
    $viewData = $viewStmt->fetch();
}

include __DIR__ . '/includes/header.php';
?>

<div class="card">
    <div class="card-header">
        <div class="toolbar" style="width: 100%;">
            <div class="search-box">
                <i class="fas fa-search"></i>
                <input type="text" id="searchInput" placeholder="Cari nama, jabatan..." 
                       value="<?= htmlspecialchars($search) ?>"
                       onkeydown="if(event.key==='Enter') { window.location='pengurus.php?search='+this.value; }">
            </div>
            <div class="filter-group">
                <select onchange="window.location='pengurus.php?status='+this.value+'&search=<?= urlencode($search) ?>'">
                    <option value="">Semua Status</option>
                    <option value="aktif" <?= $status === 'aktif' ? 'selected' : '' ?>>Aktif</option>
                    <option value="mantan" <?= $status === 'mantan' ? 'selected' : '' ?>>Mantan</option>
                </select>
            </div>
            <div style="margin-left: auto; display: flex; gap: 10px;">
                <button type="button" class="btn btn-danger" id="btnDeleteBatch" style="display: none;" onclick="document.getElementById('formBatchDelete').submit();">
                    <i class="fas fa-trash"></i> Hapus Terpilih
                </button>
                <a href="export_pengurus.php" class="btn btn-success">
                    <i class="fas fa-file-export"></i> Export
                </a>
                <button class="btn btn-primary" onclick="openModal('addModal')">
                    <i class="fas fa-plus"></i> Tambah Pengurus
                </button>
            </div>
        </div>
    </div>

    <form method="POST" id="formBatchDelete">
        <input type="hidden" name="action" value="delete_all">
        <div class="table-responsive">
            <table class="data-table" id="pengurusTable">
                <thead>
                    <tr>
                        <th width="40"><input type="checkbox" id="checkAll"></th>
                        <th>Pengurus</th>
                        <th>Jabatan</th>
                        <th>Telepon</th>
                        <th>Masa Jabatan</th>
                        <th>Status</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($pengurusList)): ?>
                        <tr><td colspan="7" class="text-center text-muted" style="padding: 40px;">
                            <i class="fas fa-user-tie" style="font-size: 2rem; display: block; margin-bottom: 10px; color: var(--gray-300);"></i>
                            Tidak ada data pengurus
                        </td></tr>
                    <?php else: ?>
                        <?php foreach ($pengurusList as $p): ?>
                            <tr>
                                <td><input type="checkbox" name="ids[]" value="<?= $p['id'] ?>" class="check-item"></td>
                                <td>
                                    <div class="member-cell">
                                        <div class="member-avatar" style="overflow: hidden; background: var(--gray-100);">
                                            <?php if ($p['anggota_foto'] && file_exists('uploads/anggota/' . $p['anggota_foto'])): ?>
                                                <img src="uploads/anggota/<?= htmlspecialchars($p['anggota_foto']) ?>" style="width: 100%; height: 100%; object-fit: cover;">
                                            <?php else: ?>
                                                <i class="fas fa-user" style="color: var(--gray-400);"></i>
                                            <?php endif; ?>
                                        </div>
                                        <div class="member-details">
                                            <div class="member-name"><?= htmlspecialchars($p['nama']) ?></div>
                                            <div class="member-id"><?= htmlspecialchars($p['email'] ?: '-') ?></div>
                                        </div>
                                    </div>
                                </td>
                                <td><?= htmlspecialchars($p['jabatan']) ?></td>
                                <td><?= htmlspecialchars($p['telepon'] ?? '-') ?></td>
                                <td>
                                    <div style="font-size: 0.85rem;">
                                        <div><i class="fas fa-arrow-right" style="font-size: 0.7rem; color: var(--success-500);"></i> Mulai: <?= $p['tanggal_mulai'] ? formatTanggal($p['tanggal_mulai']) : '-' ?></div>
                                        <?php if($p['tanggal_selesai']): ?>
                                            <div><i class="fas fa-arrow-left" style="font-size: 0.7rem; color: var(--danger-500);"></i> Selesai: <?= formatTanggal($p['tanggal_selesai']) ?></div>
                                        <?php endif; ?>
                                    </div>
                                </td>
                                <td>
                                    <span class="badge <?= $p['status'] === 'aktif' ? 'badge-success' : 'badge-danger' ?>">
                                        <?= ucfirst($p['status']) ?>
                                    </span>
                                </td>
                                <td>
                                    <div class="action-buttons">
                                        <a href="pengurus.php?view=<?= $p['id'] ?>" class="btn btn-icon btn-outline" title="View Detail">
                                            <i class="fas fa-eye" style="color: var(--primary-500);"></i>
                                        </a>
                                        <a href="pengurus.php?edit=<?= $p['id'] ?>" class="btn btn-icon btn-outline" title="Edit">
                                            <i class="fas fa-pen" style="color: var(--info-500);"></i>
                                        </a>
                                        <form method="POST" style="display:inline;" onsubmit="return confirm('Hapus pengurus <?= htmlspecialchars($p['nama']) ?>?');">
                                            <input type="hidden" name="action" value="delete">
                                            <input type="hidden" name="id" value="<?= $p['id'] ?>">
                                            <button type="submit" class="btn btn-icon btn-outline" title="Hapus">
                                                <i class="fas fa-trash" style="color: var(--danger-500);"></i>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </form>

    <?php if ($totalPages > 1): ?>
        <div class="card-footer">
            <div class="pagination">
                <?php if ($page > 1): ?>
                    <a href="pengurus.php?page=<?= $page-1 ?>&search=<?= urlencode($search) ?>&status=<?= urlencode($status) ?>"><i class="fas fa-chevron-left"></i></a>
                <?php endif; ?>
                <?php for ($p = 1; $p <= $totalPages; $p++): ?>
                    <a href="pengurus.php?page=<?= $p ?>&search=<?= urlencode($search) ?>&status=<?= urlencode($status) ?>" 
                       class="<?= $p === $page ? 'active' : '' ?>"><?= $p ?></a>
                <?php endfor; ?>
                <?php if ($page < $totalPages): ?>
                    <a href="pengurus.php?page=<?= $page+1 ?>&search=<?= urlencode($search) ?>&status=<?= urlencode($status) ?>"><i class="fas fa-chevron-right"></i></a>
                <?php endif; ?>
            </div>
        </div>
    <?php endif; ?>
</div>

<!-- Add Modal -->
<div class="modal" id="addModal">
    <div class="modal-header">
        <h3><i class="fas fa-user-plus" style="color: var(--primary-500); margin-right: 8px;"></i>Tambah Pengurus Baru</h3>
        <button class="modal-close" onclick="closeModal('addModal')"><i class="fas fa-times"></i></button>
    </div>
    <form method="POST" enctype="multipart/form-data">
        <input type="hidden" name="action" value="add">
        <div class="modal-body">
            <div class="form-row">
                <div class="form-group" style="flex: 2;">
                    <label>Nama Lengkap <span class="required">*</span></label>
                    <input type="text" name="nama" class="form-control" required placeholder="Masukkan nama lengkap">
                </div>
                <div class="form-group">
                    <label>Jabatan <span class="required">*</span></label>
                    <input type="text" name="jabatan" class="form-control" required placeholder="Contoh: Ketua, Sekretaris">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label>NIK</label>
                    <input type="text" name="nik" class="form-control" maxlength="16" placeholder="16 digit NIK">
                </div>
                <div class="form-group">
                    <label>Jenis Kelamin</label>
                    <select name="jenis_kelamin" class="form-control">
                        <option value="L">Laki-laki</option>
                        <option value="P">Perempuan</option>
                    </select>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label>Tempat Lahir</label>
                    <input type="text" name="tempat_lahir" class="form-control" placeholder="Kota kelahiran">
                </div>
                <div class="form-group">
                    <label>Tanggal Lahir</label>
                    <input type="date" name="tanggal_lahir" class="form-control">
                </div>
            </div>
            <div class="form-group">
                <label>Alamat</label>
                <textarea name="alamat" class="form-control" rows="2" placeholder="Alamat lengkap"></textarea>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label>Telepon</label>
                    <input type="text" name="telepon" class="form-control" placeholder="08xxxxxxxxxx">
                </div>
                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" placeholder="email@contoh.com">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label>Tanggal Mulai Menjabat</label>
                    <input type="date" name="tanggal_mulai" class="form-control" value="<?= date('Y-m-d') ?>">
                </div>
                <div class="form-group">
                    <label>Status</label>
                    <select name="status" class="form-control">
                        <option value="aktif">Aktif</option>
                        <option value="mantan">Mantan</option>
                    </select>
                </div>
            </div>
            <div class="alert alert-info" style="margin-top: 15px; display: flex; gap: 10px; align-items: flex-start;">
                <i class="fas fa-info-circle" style="font-size: 1.2rem; color: var(--primary-500); margin-top: 2px;"></i>
                <div style="font-size: 0.88rem; line-height: 1.4;">
                    <strong>Sinkronisasi Foto Profil Otomatis:</strong><br>
                    Foto profil pengurus akan diambil secara otomatis dari menu <strong>Anggota</strong> berdasarkan kecocokan <strong>NIK</strong>. Pastikan NIK yang diinput di atas sesuai dengan NIK pada data anggota.
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-outline" onclick="closeModal('addModal')">Batal</button>
            <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Simpan</button>
        </div>
    </form>
</div>

<!-- Edit Modal -->
<?php if ($editData): ?>
<script>document.addEventListener('DOMContentLoaded', () => openModal('editModal'));</script>
<div class="modal" id="editModal">
    <div class="modal-header">
        <h3><i class="fas fa-user-edit" style="color: var(--info-500); margin-right: 8px;"></i>Edit Pengurus</h3>
        <button class="modal-close" onclick="window.location='pengurus.php'"><i class="fas fa-times"></i></button>
    </div>
    <form method="POST" enctype="multipart/form-data">
        <input type="hidden" name="action" value="edit">
        <input type="hidden" name="id" value="<?= $editData['id'] ?>">
        <input type="hidden" name="old_foto" value="<?= $editData['foto'] ?>">
        <div class="modal-body">
            <div class="form-row">
                <div class="form-group" style="flex: 2;">
                    <label>Nama Lengkap <span class="required">*</span></label>
                    <input type="text" name="nama" class="form-control" required value="<?= htmlspecialchars($editData['nama']) ?>">
                </div>
                <div class="form-group">
                    <label>Jabatan <span class="required">*</span></label>
                    <input type="text" name="jabatan" class="form-control" required value="<?= htmlspecialchars($editData['jabatan']) ?>">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label>NIK</label>
                    <input type="text" name="nik" class="form-control" maxlength="16" value="<?= htmlspecialchars($editData['nik'] ?? '') ?>">
                </div>
                <div class="form-group">
                    <label>Jenis Kelamin</label>
                    <select name="jenis_kelamin" class="form-control">
                        <option value="L" <?= $editData['jenis_kelamin'] === 'L' ? 'selected' : '' ?>>Laki-laki</option>
                        <option value="P" <?= $editData['jenis_kelamin'] === 'P' ? 'selected' : '' ?>>Perempuan</option>
                    </select>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label>Tempat Lahir</label>
                    <input type="text" name="tempat_lahir" class="form-control" value="<?= htmlspecialchars($editData['tempat_lahir'] ?? '') ?>">
                </div>
                <div class="form-group">
                    <label>Tanggal Lahir</label>
                    <input type="date" name="tanggal_lahir" class="form-control" value="<?= $editData['tanggal_lahir'] ?? '' ?>">
                </div>
            </div>
            <div class="form-group">
                <label>Alamat</label>
                <textarea name="alamat" class="form-control" rows="2"><?= htmlspecialchars($editData['alamat'] ?? '') ?></textarea>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label>Telepon</label>
                    <input type="text" name="telepon" class="form-control" value="<?= htmlspecialchars($editData['telepon'] ?? '') ?>">
                </div>
                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" value="<?= htmlspecialchars($editData['email'] ?? '') ?>">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label>Tanggal Mulai</label>
                    <input type="date" name="tanggal_mulai" class="form-control" value="<?= $editData['tanggal_mulai'] ?? '' ?>">
                </div>
                <div class="form-group">
                    <label>Tanggal Selesai</label>
                    <input type="date" name="tanggal_selesai" class="form-control" value="<?= $editData['tanggal_selesai'] ?? '' ?>">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group" style="flex: 1;">
                    <label>Status</label>
                    <select name="status" class="form-control">
                        <option value="aktif" <?= $editData['status'] === 'aktif' ? 'selected' : '' ?>>Aktif</option>
                        <option value="mantan" <?= $editData['status'] === 'mantan' ? 'selected' : '' ?>>Mantan</option>
                    </select>
                </div>
            </div>
            <?php if ($editData['anggota_foto'] && file_exists('uploads/anggota/' . $editData['anggota_foto'])): ?>
                <div style="margin-top: 10px; margin-bottom: 15px;">
                    <label>Foto Profil (Otomatis dari Anggota):</label><br>
                    <img src="uploads/anggota/<?= $editData['anggota_foto'] ?>" style="width: 80px; height: 80px; object-fit: cover; border-radius: 8px; border: 1px solid var(--gray-200);">
                </div>
            <?php else: ?>
                <div class="alert alert-info" style="margin-top: 15px; display: flex; gap: 10px; align-items: flex-start;">
                    <i class="fas fa-info-circle" style="font-size: 1.2rem; color: var(--primary-500); margin-top: 2px;"></i>
                    <div style="font-size: 0.88rem; line-height: 1.4;">
                        <strong>Foto Profil Belum Sinkron:</strong><br>
                        Tidak ada foto profil anggota yang cocok dengan NIK pengurus ini di menu <strong>Anggota</strong>. Silakan unggah foto di menu Anggota terlebih dahulu.
                    </div>
                </div>
            <?php endif; ?>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-outline" onclick="window.location='pengurus.php'">Batal</button>
            <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Perbarui</button>
        </div>
    </form>
</div>
<?php endif; ?>

<!-- View Modal -->
<?php if ($viewData): ?>
<script>document.addEventListener('DOMContentLoaded', () => openModal('viewModal'));</script>
<div class="modal" id="viewModal">
    <div class="modal-header">
        <h3><i class="fas fa-user-tag" style="color: var(--primary-500); margin-right: 8px;"></i>Detail Pengurus</h3>
        <button class="modal-close" onclick="window.location='pengurus.php'"><i class="fas fa-times"></i></button>
    </div>
    <div class="modal-body">
        <div style="display: flex; gap: 25px; align-items: flex-start; margin-bottom: 20px;">
            <div style="width: 150px; height: 150px; border-radius: 12px; overflow: hidden; background: var(--gray-100); flex-shrink: 0; box-shadow: var(--shadow-md);">
                <?php if ($viewData['anggota_foto'] && file_exists('uploads/anggota/' . $viewData['anggota_foto'])): ?>
                    <img src="uploads/anggota/<?= htmlspecialchars($viewData['anggota_foto']) ?>" style="width: 100%; height: 100%; object-fit: cover;">
                <?php else: ?>
                    <div style="width: 100%; height: 100%; display: flex; align-items: center; justify-content: center;">
                        <i class="fas fa-user" style="font-size: 4rem; color: var(--gray-300);"></i>
                    </div>
                <?php endif; ?>
            </div>
            <div style="flex: 1;">
                <h2 style="margin: 0 0 5px; color: var(--gray-900);"><?= htmlspecialchars($viewData['nama']) ?></h2>
                <div style="font-size: 1.1rem; color: var(--primary-600); font-weight: 600; margin-bottom: 15px;"><?= htmlspecialchars($viewData['jabatan']) ?></div>
                <div class="badge <?= $viewData['status'] === 'aktif' ? 'badge-success' : 'badge-danger' ?>" style="font-size: 0.85rem; padding: 5px 15px;">
                    <?= ucfirst($viewData['status']) ?>
                </div>
            </div>
        </div>

        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; border-top: 1px solid var(--gray-100); padding-top: 20px;">
            <div class="info-group">
                <label style="display: block; font-size: 0.75rem; color: var(--gray-500); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 4px;">NIK</label>
                <div style="font-weight: 500; color: var(--gray-800);"><?= htmlspecialchars($viewData['nik'] ?: '-') ?></div>
            </div>
            <div class="info-group">
                <label style="display: block; font-size: 0.75rem; color: var(--gray-500); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 4px;">Jenis Kelamin</label>
                <div style="font-weight: 500; color: var(--gray-800);"><?= $viewData['jenis_kelamin'] === 'L' ? 'Laki-laki' : 'Perempuan' ?></div>
            </div>
            <div class="info-group">
                <label style="display: block; font-size: 0.75rem; color: var(--gray-500); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 4px;">Tempat, Tgl Lahir</label>
                <div style="font-weight: 500; color: var(--gray-800);"><?= ($viewData['tempat_lahir'] ? $viewData['tempat_lahir'] . ', ' : '') . ($viewData['tanggal_lahir'] ? formatTanggal($viewData['tanggal_lahir']) : '-') ?></div>
            </div>
            <div class="info-group">
                <label style="display: block; font-size: 0.75rem; color: var(--gray-500); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 4px;">Telepon</label>
                <div style="font-weight: 500; color: var(--gray-800);"><?= htmlspecialchars($viewData['telepon'] ?: '-') ?></div>
            </div>
            <div class="info-group">
                <label style="display: block; font-size: 0.75rem; color: var(--gray-500); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 4px;">Email</label>
                <div style="font-weight: 500; color: var(--gray-800);"><?= htmlspecialchars($viewData['email'] ?: '-') ?></div>
            </div>
            <div class="info-group">
                <label style="display: block; font-size: 0.75rem; color: var(--gray-500); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 4px;">Masa Jabatan</label>
                <div style="font-weight: 500; color: var(--gray-800);">
                    <?= $viewData['tanggal_mulai'] ? formatTanggal($viewData['tanggal_mulai']) : '-' ?> s/d <?= $viewData['tanggal_selesai'] ? formatTanggal($viewData['tanggal_selesai']) : 'Sekarang' ?>
                </div>
            </div>
        </div>
        <div class="info-group" style="margin-top: 20px;">
            <label style="display: block; font-size: 0.75rem; color: var(--gray-500); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 4px;">Alamat</label>
            <div style="font-weight: 500; color: var(--gray-800); background: var(--gray-50); padding: 10px; border-radius: 6px;"><?= nl2br(htmlspecialchars($viewData['alamat'] ?: '-')) ?></div>
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-primary" onclick="window.location='pengurus.php'">Tutup</button>
    </div>
</div>
<?php endif; ?>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const checkAll = document.getElementById('checkAll');
    const checkItems = document.querySelectorAll('.check-item');
    const btnDeleteBatch = document.getElementById('btnDeleteBatch');

    if (checkAll) {
        checkAll.addEventListener('change', function() {
            checkItems.forEach(item => {
                item.checked = checkAll.checked;
            });
            toggleDeleteButton();
        });
    }

    checkItems.forEach(item => {
        item.addEventListener('change', function() {
            if (!this.checked) checkAll.checked = false;
            if (document.querySelectorAll('.check-item:checked').length === checkItems.length) {
                checkAll.checked = true;
            }
            toggleDeleteButton();
        });
    });

    function toggleDeleteButton() {
        const checkedCount = document.querySelectorAll('.check-item:checked').length;
        if (checkedCount > 0) {
            btnDeleteBatch.style.display = 'block';
            btnDeleteBatch.innerHTML = `<i class="fas fa-trash"></i> Hapus (${checkedCount})`;
        } else {
            btnDeleteBatch.style.display = 'none';
        }
    }
});
</script>

<?php include __DIR__ . '/includes/footer.php'; ?>
