<?php
$categoryLabels = [
    'membership'  => ['en' => 'Membership', 'ar' => 'العضوية', 'icon' => 'fas fa-users', 'color' => 'primary'],
    'financial'   => ['en' => 'Financial',  'ar' => 'المالية',  'icon' => 'fas fa-money-bill-wave', 'color' => 'success'],
    'operational' => ['en' => 'Operational','ar' => 'التشغيل',  'icon' => 'fas fa-cogs', 'color' => 'info'],
    'governance'  => ['en' => 'Governance', 'ar' => 'الحوكمة',  'icon' => 'fas fa-landmark', 'color' => 'warning'],
    'custom'      => ['en' => 'Custom',     'ar' => 'مخصص',     'icon' => 'fas fa-wrench', 'color' => 'secondary'],
    'other'       => ['en' => 'Other',      'ar' => 'أخرى',     'icon' => 'fas fa-folder', 'color' => 'dark'],
];
$lang = Lang::current();
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h4 class="mb-0"><i class="fas fa-chart-bar me-2"></i><?= lang('reports.browser_title') ?></h4>
    <div class="d-flex gap-2">
        <input type="text" id="report-search" class="form-control form-control-sm" placeholder="<?= lang('common.search') ?>..." style="width:250px">
    </div>
</div>

<?php if (!empty($favorites)): ?>
<div class="card mb-4">
    <div class="card-header bg-warning bg-opacity-10">
        <h6 class="mb-0"><i class="fas fa-star text-warning me-2"></i><?= lang('reports.favorites') ?></h6>
    </div>
    <div class="card-body">
        <div class="row g-3">
            <?php foreach ($favorites as $fav): ?>
            <div class="col-md-3">
                <a href="/reports/<?= (int)$fav['report_id'] ?>" class="text-decoration-none">
                    <div class="card border-warning h-100 report-card-item">
                        <div class="card-body py-3">
                            <h6 class="mb-1"><?= e($fav['favorite_name']) ?></h6>
                            <small class="text-muted"><?= e($fav['report_name_' . $lang] ?: $fav['report_name_en']) ?></small>
                        </div>
                    </div>
                </a>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
<?php endif; ?>

<?php foreach ($categories as $catKey => $catReports): ?>
<?php $catInfo = $categoryLabels[$catKey] ?? $categoryLabels['other']; ?>
<div class="card mb-4 report-category-card" data-category="<?= e($catKey) ?>">
    <div class="card-header">
        <h5 class="mb-0">
            <i class="<?= e($catInfo['icon']) ?> text-<?= e($catInfo['color']) ?> me-2"></i>
            <?= e($catInfo[$lang] ?? $catInfo['en']) ?>
            <span class="badge bg-<?= e($catInfo['color']) ?> ms-2"><?= count($catReports) ?></span>
        </h5>
    </div>
    <div class="card-body">
        <div class="row g-3">
            <?php foreach ($catReports as $report): ?>
            <div class="col-md-4 col-lg-3 report-card-wrapper" data-name="<?= e(strtolower($report['report_name_en'] . ' ' . ($report['report_name_ar'] ?? ''))) ?>">
                <a href="/reports/<?= (int)$report['id'] ?>" class="text-decoration-none">
                    <div class="card h-100 report-card-item border-start border-3 border-<?= e($catInfo['color']) ?>">
                        <div class="card-body">
                            <div class="d-flex justify-content-between align-items-start">
                                <h6 class="card-title mb-1"><?= e($report['report_name_' . $lang] ?: $report['report_name_en']) ?></h6>
                                <?php if ($report['is_favorite']): ?>
                                <i class="fas fa-star text-warning"></i>
                                <?php endif; ?>
                            </div>
                            <p class="card-text text-muted small mb-2">
                                <?= e(str_limit($report['description_' . $lang] ?: $report['description_en'] ?: '', 80)) ?>
                            </p>
                            <div class="d-flex gap-1">
                                <?php if ($report['chart_type']): ?>
                                <span class="badge bg-light text-dark"><i class="fas fa-chart-pie me-1"></i>Chart</span>
                                <?php endif; ?>
                                <?php if ($report['is_schedulable']): ?>
                                <span class="badge bg-light text-dark"><i class="fas fa-clock me-1"></i>Schedulable</span>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </a>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
<?php endforeach; ?>

<?php if (empty($categories)): ?>
<div class="card">
    <div class="card-body text-center py-5">
        <i class="fas fa-chart-bar fa-3x text-muted mb-3"></i>
        <h5 class="text-muted"><?= lang('reports.no_reports') ?></h5>
        <p class="text-muted">Reports will be available once initialized.</p>
        <button class="btn btn-primary" id="btn-init-reports"><i class="fas fa-magic me-1"></i>Initialize Reports</button>
    </div>
</div>
<?php endif; ?>

<?php $__view->push('scripts') ?>
<script>
$(function() {
    $('#report-search').on('input', function() {
        var q = $(this).val().toLowerCase();
        $('.report-card-wrapper').each(function() {
            var name = $(this).data('name') || '';
            $(this).toggle(name.indexOf(q) !== -1);
        });
        $('.report-category-card').each(function() {
            var visible = $(this).find('.report-card-wrapper:visible').length;
            $(this).toggle(visible > 0);
        });
    });

    $('#btn-init-reports').on('click', function() {
        var $btn = $(this);
        $btn.prop('disabled', true).html('<span class="spinner-border spinner-border-sm me-1"></span>Initializing...');
        $.ajax({
            url: '/api/reports/init',
            type: 'POST',
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
            success: function() { location.reload(); },
            error: function() { $btn.prop('disabled', false).text('Initialize Reports'); }
        });
    });
});
</script>
<?php $__view->endPush() ?>