<?php
session_start();
require_once '../../config/database.php';
require_once '../../config/function.php';

// Cek login
checkLogin();

$current_user = getCurrentUser();
$tahun_ajaran_aktif = getTahunAjaranAktif();

// Pagination
$limit = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;
$search = isset($_GET['search']) ? $_GET['search'] : '';
$filter_status = isset($_GET['status']) ? $_GET['status'] : '';

// Query untuk mengambil data konseling
$sql = "SELECT k.*, s.nama as nama_siswa, s.nis, 
        g.nama_guru as nama_guru_bk, u.role as role_pelapor
        FROM bkcare_konseling k
        JOIN bkcare_siswa s ON k.id_siswa = s.id_siswa
        LEFT JOIN bkcare_guru g ON k.id_guru_bk = g.id_guru
        LEFT JOIN bkcare_users u ON u.id_user = (SELECT id_user FROM bkcare_guru WHERE id_guru = k.id_guru_bk)
        WHERE 1=1";

$count_sql = "SELECT COUNT(*) as total FROM bkcare_konseling k WHERE 1=1";

if ($search) {
    $sql .= " AND (s.nama LIKE '%$search%' OR s.nis LIKE '%$search%')";
    $count_sql .= " AND (s.nama LIKE '%$search%' OR s.nis LIKE '%$search%')";
}

if ($filter_status) {
    $sql .= " AND k.status = '$filter_status'";
    $count_sql .= " AND k.status = '$filter_status'";
}

$sql .= " ORDER BY k.tanggal DESC LIMIT $limit OFFSET $offset";

$result = mysqli_query($conn, $sql);
$total_result = mysqli_query($conn, $count_sql);
$total_rows = mysqli_fetch_assoc($total_result)['total'];
$total_pages = ceil($total_rows / $limit);
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Data Konseling - BKCare</title>
    <link rel="stylesheet" href="../../assets/css/style.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
    <?php include_once '../../includes/navbar.php'; ?>
    
    <div class="container-fluid">
        <div class="row">
            <?php include_once '../../includes/sidebar.php'; ?>
            
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">
                        <i class="fas fa-chalkboard-user me-2"></i> Data Konseling
                    </h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <a href="tambah.php" class="btn btn-primary">
                            <i class="fas fa-plus me-1"></i> Tambah Konseling
                        </a>
                    </div>
                </div>

                <!-- Filter dan Search -->
                <div class="card mb-4">
                    <div class="card-body">
                        <form method="GET" class="row g-3">
                            <div class="col-md-4">
                                <label class="form-label">
                                    <i class="fas fa-search me-1"></i> Cari
                                </label>
                                <input type="text" name="search" class="form-control" 
                                       value="<?= htmlspecialchars($search) ?>" 
                                       placeholder="Nama atau NIS siswa...">
                            </div>
                            <div class="col-md-3">
                                <label class="form-label">
                                    <i class="fas fa-filter me-1"></i> Status
                                </label>
                                <select name="status" class="form-select">
                                    <option value="">Semua Status</option>
                                    <option value="proses" <?= $filter_status == 'proses' ? 'selected' : '' ?>>Proses</option>
                                    <option value="selesai" <?= $filter_status == 'selesai' ? 'selected' : '' ?>>Selesai</option>
                                </select>
                            </div>
                            <div class="col-md-2 d-flex align-items-end">
                                <button type="submit" class="btn btn-primary w-100">
                                    <i class="fas fa-search me-1"></i> Filter
                                </button>
                            </div>
                            <div class="col-md-2 d-flex align-items-end">
                                <a href="index.php" class="btn btn-secondary w-100">
                                    <i class="fas fa-undo me-1"></i> Reset
                                </a>
                            </div>
                        </form>
                    </div>
                </div>

                <!-- Tabel Data Konseling -->
                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th><i class="fas fa-calendar me-1"></i> Tanggal</th>
                                        <th><i class="fas fa-user-graduate me-1"></i> Siswa</th>
                                        <th><i class="fas fa-chalkboard-user me-1"></i> Guru BK</th>
                                        <th><i class="fas fa-tag me-1"></i> Jenis Konseling</th>
                                        <th><i class="fas fa-map-marker-alt me-1"></i> Lokasi</th>
                                        <th><i class="fas fa-info-circle me-1"></i> Status</th>
                                        <th><i class="fas fa-cog me-1"></i> Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php if (mysqli_num_rows($result) > 0): ?>
                                        <?php $no = $offset + 1; ?>
                                        <?php while ($row = mysqli_fetch_assoc($result)): ?>
                                            <tr>
                                                <td><?= $no++ ?></td>
                                                <td><?= date('d/m/Y', strtotime($row['tanggal'])) ?></td>
                                                <td>
                                                    <strong><?= htmlspecialchars($row['nama_siswa']) ?></strong><br>
                                                    <small class="text-muted">NIS: <?= $row['nis'] ?></small>
                                                </td>
                                                <td><?= $row['nama_guru_bk'] ?? '-' ?></td>
                                                <td><?= htmlspecialchars($row['jenis_konseling']) ?></td>
                                                <td><?= htmlspecialchars($row['lokasi']) ?></td>
                                                <td>
                                                    <?php if ($row['status'] == 'proses'): ?>
                                                        <span class="badge bg-warning">Proses</span>
                                                    <?php else: ?>
                                                        <span class="badge bg-success">Selesai</span>
                                                    <?php endif; ?>
                                                </td>
                                                <td>
                                                    <a href="detail.php?id=<?= $row['id_konseling'] ?>" 
                                                       class="btn btn-sm btn-info" title="Detail">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                    <a href="sesi.php?id=<?= $row['id_konseling'] ?>" 
                                                       class="btn btn-sm btn-primary" title="Sesi Konseling">
                                                        <i class="fas fa-comments"></i>
                                                    </a>
                                                </td>
                                            </tr>
                                        <?php endwhile; ?>
                                    <?php else: ?>
                                        <tr>
                                            <td colspan="8" class="text-center">Tidak ada data konseling</td>
                                        </tr>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>

                        <!-- Pagination -->
                        <?php if ($total_pages > 1): ?>
                            <nav aria-label="Page navigation">
                                <ul class="pagination justify-content-center">
                                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                                        <li class="page-item <?= $i == $page ? 'active' : '' ?>">
                                            <a class="page-link" 
                                               href="?page=<?= $i ?>&search=<?= urlencode($search) ?>&status=<?= $filter_status ?>">
                                                <?= $i ?>
                                            </a>
                                        </li>
                                    <?php endfor; ?>
                                </ul>
                            </nav>
                        <?php endif; ?>
                    </div>
                </div>
            </main>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="../../assets/js/script.js"></script>
</body>
</html>