<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>لوحة التحكم المالية - حمام السباحة<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4>لوحة التحكم المالية - حمام السباحة</h4>
        <a href="/facility-grids/<?= $grid_id ?>/financial-dashboard/export?filter=<?= e($filter) ?>&from=<?= e($data['period']['from'] ?? '') ?>&to=<?= e($data['period']['to'] ?? '') ?>"
           class="btn btn-outline-success" target="_blank">تصدير PDF</a>
    </div>

    <!-- Filter -->
    <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="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-3">
            <div class="card bg-success text-white">
                <div class="card-body text-center">
                    <h6>إجمالي الإيرادات</h6>
                    <h3><?= number_format($data['total_revenue'] ?? 0, 2) ?> ج.م</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-primary text-white">
                <div class="card-body text-center">
                    <h6>عدد الحجوزات</h6>
                    <h3><?= $data['total_bookings'] ?? 0 ?></h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-info text-white">
                <div class="card-body text-center">
                    <h6>المستخدمين الفريدين</h6>
                    <h3><?= $data['unique_users'] ?? 0 ?></h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-warning text-dark">
                <div class="card-body text-center">
                    <h6>نسبة الحضور</h6>
                    <h3><?= $data['attendance']['avg_attendance_rate'] ?? 0 ?>%</h3>
                </div>
            </div>
        </div>
    </div>

    <!-- Revenue by Type -->
    <?php if (!empty($data['revenue_by_type'])): ?>
    <div class="card mb-3">
        <div class="card-header">الإيرادات حسب نوع الحاجز</div>
        <div class="card-body">
            <table class="table table-bordered">
                <thead><tr><th>النوع</th><th>العدد</th><th>الإيرادات</th></tr></thead>
                <tbody>
                <?php foreach ($data['revenue_by_type'] as $row): ?>
                <tr>
                    <td><?= e($row['booker_type']) ?></td>
                    <td><?= $row['count'] ?></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">
            <table class="table table-bordered">
                <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; ?>
</div>

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