<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>التقرير الرياضي الشامل<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sports-dashboard/export?filter=<?= e($filter) ?>&from=<?= e($data['period']['from'] ?? '') ?>&to=<?= e($data['period']['to'] ?? '') ?>"
   class="btn btn-outline-success" target="_blank">تصدير التقرير</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card mb-3">
    <div class="card-body">
        <form method="get" class="row g-3 align-items-end">
            <div class="col-auto">
                <label class="form-label">الفترة</label>
                <select name="filter" class="form-select" onchange="toggleCustomDates(this)">
                    <option value="daily" <?= $filter === 'daily' ? 'selected' : '' ?>>يومي</option>
                    <option value="weekly" <?= $filter === 'weekly' ? 'selected' : '' ?>>أسبوعي</option>
                    <option value="monthly" <?= $filter === 'monthly' ? 'selected' : '' ?>>شهري</option>
                    <option value="yearly" <?= $filter === 'yearly' ? 'selected' : '' ?>>سنوي</option>
                    <option value="3years" <?= $filter === '3years' ? 'selected' : '' ?>>3 سنوات</option>
                    <option value="5years" <?= $filter === '5years' ? 'selected' : '' ?>>5 سنوات</option>
                    <option value="custom" <?= $filter === 'custom' ? 'selected' : '' ?>>تحديد تاريخ</option>
                </select>
            </div>
            <div class="col-auto custom-dates" style="<?= $filter !== 'custom' ? 'display:none' : '' ?>">
                <label class="form-label">من</label>
                <input type="date" name="from" class="form-control" value="<?= e($data['period']['from'] ?? '') ?>">
            </div>
            <div class="col-auto custom-dates" style="<?= $filter !== 'custom' ? 'display:none' : '' ?>">
                <label class="form-label">إلى</label>
                <input type="date" name="to" class="form-control" value="<?= e($data['period']['to'] ?? '') ?>">
            </div>
            <div class="col-auto">
                <button type="submit" class="btn btn-primary">عرض</button>
            </div>
        </form>
    </div>
</div>

<!-- Stats Cards -->
<div class="row mb-4">
    <div class="col-md-2">
        <div class="card bg-success text-white">
            <div class="card-body text-center">
                <h6>إجمالي الإيرادات</h6>
                <h4><?= number_format($data['total_revenue'] ?? 0, 2) ?> ج.م</h4>
            </div>
        </div>
    </div>
    <div class="col-md-2">
        <div class="card bg-primary text-white">
            <div class="card-body text-center">
                <h6>إيرادات الحجوزات</h6>
                <h4><?= number_format($data['booking_revenue'] ?? 0, 2) ?> ج.م</h4>
            </div>
        </div>
    </div>
    <div class="col-md-2">
        <div class="card bg-info text-white">
            <div class="card-body text-center">
                <h6>الأوقات الحرة</h6>
                <h4><?= number_format($data['free_time_revenue'] ?? 0, 2) ?> ج.م</h4>
            </div>
        </div>
    </div>
    <div class="col-md-2">
        <div class="card bg-dark text-white">
            <div class="card-body text-center">
                <h6>اللاعبين الفريدين</h6>
                <h4><?= $data['unique_players'] ?? 0 ?></h4>
            </div>
        </div>
    </div>
    <div class="col-md-2">
        <div class="card bg-secondary text-white">
            <div class="card-body text-center">
                <h6>المدربين النشطين</h6>
                <h4><?= $data['active_coaches'] ?? 0 ?></h4>
            </div>
        </div>
    </div>
    <div class="col-md-2">
        <div class="card bg-warning text-dark">
            <div class="card-body text-center">
                <h6>نسبة الحضور</h6>
                <h4><?= $data['avg_attendance_rate'] ?? 0 ?>%</h4>
            </div>
        </div>
    </div>
</div>

<!-- Revenue by Facility -->
<?php if (!empty($data['bookings_by_facility'])): ?>
<div class="card mb-3">
    <div class="card-header">الإيرادات حسب المرفق</div>
    <div class="card-body p-0">
        <table class="table table-bordered table-hover mb-0">
            <thead><tr><th>المرفق</th><th>النوع</th><th>الحجوزات</th><th>الإيرادات</th></tr></thead>
            <tbody>
            <?php foreach ($data['bookings_by_facility'] as $row): ?>
            <tr>
                <td><?= e($row['facility_name']) ?></td>
                <td><?= e($row['facility_type']) ?></td>
                <td><?= $row['bookings'] ?></td>
                <td><?= number_format((float) $row['revenue'], 2) ?> ج.م</td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Playground Stats -->
<?php if (!empty($data['playground_stats'])): ?>
<div class="card mb-3">
    <div class="card-header">إحصائيات الملاعب</div>
    <div class="card-body p-0">
        <table class="table table-bordered table-hover mb-0">
            <thead><tr><th>الملعب</th><th>النوع</th><th>الحجوزات</th><th>الإيرادات</th></tr></thead>
            <tbody>
            <?php foreach ($data['playground_stats'] as $row): ?>
            <tr>
                <td><?= e($row['playground_name_ar']) ?></td>
                <td><?= e($row['playground_type']) ?></td>
                <td><?= $row['bookings'] ?></td>
                <td><?= number_format((float) $row['revenue'], 2) ?> ج.م</td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Monthly Trend -->
<?php if (!empty($data['monthly_trend'])): ?>
<div class="card mb-3">
    <div class="card-header">الاتجاه الشهري</div>
    <div class="card-body p-0">
        <table class="table table-bordered table-hover mb-0">
            <thead><tr><th>الشهر</th><th>الحجوزات</th><th>الإيرادات</th></tr></thead>
            <tbody>
            <?php foreach ($data['monthly_trend'] as $row): ?>
            <tr>
                <td><?= $row['month'] ?></td>
                <td><?= $row['bookings'] ?></td>
                <td><?= number_format((float) $row['revenue'], 2) ?> ج.م</td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<script>
function toggleCustomDates(el) {
    document.querySelectorAll('.custom-dates').forEach(d => {
        d.style.display = el.value === 'custom' ? '' : 'none';
    });
}
</script>
<?php $__template->endSection(); ?>
