<?php
    $layout = auth()->user()->role === 'super_admin' ? 'layouts.admin' : 'layouts.app';
?>



<?php $__env->startSection('title', $isSuperAdmin ? 'Grievance Redress Overview - All Components' : 'Grievance Redress Mechanism - Component ' . $component); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid py-4">

    <!-- Page Header -->
    <div class="dashboard-header">
        <div class="container-fluid">
            <div class="row align-items-center dashboard-header-content">
                <div class="col-lg-6">
                    <h1 class="dashboard-title">
                        <?php if($isSuperAdmin): ?>
                            <i class="fas fa-tachometer-alt" style="color: var(--dhis2-blue-primary);"></i>
                            Grievance Redress Overview - All Components
                        <?php else: ?>
                            <i class="fas fa-tachometer-alt" style="color: var(--dhis2-blue-primary);"></i>
                            GRM - Component <?php echo e($component); ?>

                        <?php endif; ?>
                        
                    </h1>
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="javascript:history.back()">Back</a></li>
                            <li class="breadcrumb-item"><a href="<?php echo e(route('component1.dashboard')); ?>">Component 1 Dashboard</a></li>
                            <li class="breadcrumb-item active">Grievances</li>
                        </ol>
                    </nav>
                </div>
                <div class="col-lg-6">
                    <div class="d-flex justify-content-lg-end gap-2 dashboard-actions">
                        <?php if(!$isSuperAdmin): ?>
                            
                            <a href="<?php echo e(route('grievances.create', ['component' => $component])); ?>" 
                            class="btn btn-success">
                                <i class="fas fa-plus me-2"></i> Register New Grievance
                            </a>
                        <?php endif; ?>
                        <a href="<?php echo e(route('grievances.export', [
                            'component' => $isSuperAdmin ? 'all' : $component,
                            'from_date' => $fromDate,
                            'to_date'   => $toDate,
                        ])); ?>" 
                        class="btn btn-primary">
                            <i class="fas fa-download me-2"></i> Export Report
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Date Range Filter -->
    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body">
            <form method="GET" action="<?php echo e(route('grievances.index')); ?>" class="row g-3 align-items-end">
                <?php if($isSuperAdmin): ?>
                    <input type="hidden" name="component" value="all">
                <?php else: ?>
                    <input type="hidden" name="component" value="<?php echo e($component); ?>">
                <?php endif; ?>

                <div class="col-md-3">
                    <label for="from_date" class="form-label">From Date</label>
                    <input type="date" name="from_date" id="from_date" 
                        class="form-control" value="<?php echo e(old('from_date', $fromDate)); ?>">
                </div>

                <div class="col-md-3">
                    <label for="to_date" class="form-label">To Date</label>
                    <input type="date" name="to_date" id="to_date" 
                        class="form-control" value="<?php echo e(old('to_date', $toDate)); ?>">
                </div>

                <div class="col-md-3 d-flex align-items-end gap-2">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-filter"></i> Filter
                    </button>
                    <a href="<?php echo e(route('grievances.index') . ($isSuperAdmin ? '?component=all' : '?component=' . $component)); ?>" 
                    class="btn btn-outline-secondary">
                        <i class="fas fa-undo"></i> Reset
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Grievance Redress Report Section -->
    <div class="card shadow-sm border-0 mb-5">
        <div class="card-header bg-primary text-white">
            <h5 class="mb-0">Grievance Redress Report</h5>
        </div>
        <div class="card-body p-4">

            <?php $__currentLoopData = $statsByComponent; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $compId => $stats): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <h6 class="mt-4 mb-3 fw-bold">
                   Under <?php echo e($stats['name']); ?>

                </h6>

                <div class="table-responsive">
                    <table class="table table-bordered table-hover align-middle text-center mb-4">
                        <thead class="table-light">
                            <tr>
                                <th style="width:10%;">SN</th>
                                <th style="width:65%;">Indicators</th>
                                <th style="width:25%;">Number</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>1</td>
                                <td class="text-start">No of grievances reported under <?php echo e($stats['name']); ?></td>
                                <td class="fw-bold"><?php echo e($stats['reported']); ?></td>
                            </tr>
                            <tr>
                                <td>2</td>
                                <td class="text-start">No of grievances resolved</td>
                                <td class="fw-bold"><?php echo e($stats['resolved']); ?></td>
                            </tr>
                            <tr>
                                <td>3</td>
                                <td class="text-start">No of grievances in relation to GBV and SEA</td>
                                <td class="fw-bold text-danger"><?php echo e($stats['gbv_sea']); ?></td>
                            </tr>
                        </tbody>
                    </table>
                </div>

                <?php if(!$loop->last): ?>
                    <hr class="my-5">
                <?php endif; ?>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

        </div>
    </div>

    <!-- Visualizations Section -->
    <div class="row mb-5">
        <!-- Line Chart: Grievances Over Time -->
        <div class="col-md-4 mb-4">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0">Grievances Reported Over Time</h5>
                </div>
                <div class="card-body">
                    <canvas id="lineChart" height="200"></canvas>
                </div>
            </div>
        </div>

        <!-- Doughnut Chart: Status Breakdown -->
        <div class="col-md-4 mb-4">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0">Grievance Status Breakdown</h5>
                </div>
                <div class="card-body">
                    <canvas id="doughnutChart" height="200"></canvas>
                </div>
            </div>
        </div>

        <!-- Bar Chart: Grievances by Category -->
        <div class="col-md-4">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0">Grievances by Category</h5>
                </div>
                <div class="card-body">
                    <canvas id="barChart" height="200"></canvas>
                </div>
            </div>
        </div>
    </div>

    <!-- Detailed Grievances List -->
    <div class="card shadow-sm border-0">
        <div class="card-header bg-secondary text-white">
            <h5 class="mb-0">
                <?php if($isSuperAdmin): ?>
                    All Registered Grievances
                <?php else: ?>
                    Registered Grievances Under - Component <?php echo e($component); ?>

                <?php endif; ?>
            </h5>
        </div>
        <div class="card-body p-0">
            <?php if($grievances->isEmpty()): ?>
                <div class="alert alert-info m-4 text-center">
                    No grievances registered yet.
                </div>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-striped table-hover mb-0">
                        <thead class="table-dark">
                            <tr>
                                <th>SN</th>
                                <?php if($isSuperAdmin): ?>
                                    <th>Component</th>
                                <?php endif; ?>
                                <th>Complainant</th>
                                <th>Type</th>
                                <th>Category</th>
                                <th>Status</th>
                                <th>Report Date</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__currentLoopData = $grievances; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $grievance): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr>
                                    <td><?php echo e($grievance->sn); ?></td>
                                    <?php if($isSuperAdmin): ?>
                                        <td><?php echo e($grievance->component); ?></td>
                                    <?php endif; ?>
                                    <td><?php echo e($grievance->complainant_name ?? 'Anonymous'); ?><br><small>(<?php echo e($grievance->gender ?? '?'); ?>)</small></td>
                                    <td><?php echo e($grievance->grievance_type ?? '—'); ?></td>
                                    <td><?php echo e($grievance->case_category ?? '—'); ?></td>
                                    <td>
                                        <?php if($grievance->status === 'Closed'): ?> <span class="badge bg-success">Closed</span>
                                        <?php elseif($grievance->status === 'Referred'): ?> <span class="badge bg-warning">Referred</span>
                                        <?php else: ?> <span class="badge bg-danger">Open</span> <?php endif; ?>
                                    </td>
                                    <td><?php echo e($grievance->report_date ? $grievance->report_date->format('d M Y') : '—'); ?></td>
                                    <td>
                                        <a href="<?php echo e(route('grievances.show', $grievance)); ?>" class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-eye"></i> View
                                        </a>
                                        <a href="<?php echo e(route('grievances.edit', $grievance)); ?>" class="btn btn-sm btn-outline-warning">
                                            <i class="fas fa-edit"></i> Edit
                                        </a>
                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </tbody>
                    </table>
                </div>
                <div class="card-footer bg-white">
                    <?php echo e($grievances->links('pagination::bootstrap-5')); ?>

                </div>
            <?php endif; ?>
        </div>
    </div>

</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
    // Line Chart: Grievances Over Time
    const lineCtx = document.getElementById('lineChart').getContext('2d');
    const lineChart = new Chart(lineCtx, {
        type: 'line',
        data: <?php echo $chartData['line']; ?>,
        options: {
            responsive: true,
            scales: {
                y: { beginAtZero: true }
            }
        }
    });

    // Doughnut Chart: Status Breakdown
    const doughnutCtx = document.getElementById('doughnutChart').getContext('2d');
    const doughnutChart = new Chart(doughnutCtx, {
        type: 'doughnut',
        data: <?php echo $chartData['doughnut']; ?>,
        options: {
            responsive: true,
            plugins: {
                legend: { position: 'top' }
            }
        }
    });

    // Bar Chart: Grievances by Category
    const barCtx = document.getElementById('barChart').getContext('2d');
    const barChart = new Chart(barCtx, {
        type: 'bar',
        data: <?php echo $chartData['bar']; ?>,
        options: {
            responsive: true,
            scales: {
                y: { beginAtZero: true }
            }
        }
    });
</script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make($layout, array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\wamp64\www\sswseepcomponents\resources\views/grievances/index.blade.php ENDPATH**/ ?>