<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>طلبات تغيير الجدول<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/scheduling" class="btn btn-outline">لوحة الجدولة</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<!-- Filter Tabs -->
<div class="card" style="margin-bottom:20px;padding:0;">
    <div style="display:flex;gap:0;border-bottom:2px solid #E5E7EB;">
        <?php foreach (['pending' => 'معلقة', 'approved' => 'مقبولة', 'rejected' => 'مرفوضة', 'all' => 'الكل'] as $key => $label): ?>
        <a href="/scheduling/requests?status=<?= $key ?>" style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $status === $key ? '#0D7377' : 'transparent' ?>;color:<?= $status === $key ? '#0D7377' : '#6B7280' ?>;">
            <?= $label ?>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<div class="card" style="padding:20px;">
    <?php if (empty($requests)): ?>
    <p style="color:#6B7280;text-align:center;padding:30px;">لا توجد طلبات</p>
    <?php else: ?>
    <?php foreach ($requests as $req): ?>
    <div style="padding:15px;border:1px solid #E5E7EB;border-radius:8px;margin-bottom:12px;">
        <div style="display:flex;justify-content:space-between;align-items:start;">
            <div>
                <span style="font-weight:700;"><?= e($req['requester_name'] ?? '') ?></span>
                <span style="font-size:11px;color:#6B7280;margin-right:8px;">(<?= $req['requester_type'] === 'coach' ? 'مدرب' : ($req['requester_type'] === 'player' ? 'لاعب' : 'مسؤول') ?>)</span>
                <span style="font-size:11px;background:<?= $req['request_type'] === 'reschedule' ? '#DBEAFE' : '#FEE2E2' ?>;color:<?= $req['request_type'] === 'reschedule' ? '#1D4ED8' : '#DC2626' ?>;padding:2px 8px;border-radius:4px;margin-right:6px;">
                    <?= $req['request_type'] === 'reschedule' ? 'إعادة جدولة' : ($req['request_type'] === 'cancel' ? 'إلغاء' : $req['request_type']) ?>
                </span>
            </div>
            <div style="text-align:left;">
                <span style="font-size:11px;color:#6B7280;"><?= e(substr($req['created_at'] ?? '', 0, 16)) ?></span>
                <?php if ($req['status'] === 'approved'): ?>
                <div style="color:#059669;font-size:12px;font-weight:600;">● مقبول</div>
                <?php elseif ($req['status'] === 'rejected'): ?>
                <div style="color:#DC2626;font-size:12px;font-weight:600;">● مرفوض</div>
                <?php else: ?>
                <div style="color:#F59E0B;font-size:12px;font-weight:600;">● معلق</div>
                <?php endif; ?>
            </div>
        </div>

        <div style="margin-top:8px;font-size:13px;color:#4B5563;">
            <strong>السبب:</strong> <?= e($req['reason_ar'] ?? '') ?>
        </div>

        <?php if ($req['requested_date'] || $req['requested_start_time']): ?>
        <div style="margin-top:6px;font-size:12px;color:#6B7280;">
            <strong>المطلوب:</strong>
            <?php if ($req['requested_date']): ?>التاريخ: <?= e($req['requested_date']) ?><?php endif; ?>
            <?php if ($req['requested_start_time']): ?> | الوقت: <?= e(substr($req['requested_start_time'], 0, 5)) ?>-<?= e(substr($req['requested_end_time'] ?? '', 0, 5)) ?><?php endif; ?>
            <?php if ($req['requested_day_of_week'] !== null): ?> | اليوم: <?= ['الأحد','الإثنين','الثلاثاء','الأربعاء','الخميس','الجمعة','السبت'][(int) $req['requested_day_of_week']] ?? '' ?><?php endif; ?>
        </div>
        <?php endif; ?>

        <?php if (!empty($req['auto_suggestions_json'])): ?>
        <?php $suggestions = json_decode($req['auto_suggestions_json'], true); ?>
        <?php if (!empty($suggestions)): ?>
        <div style="margin-top:8px;padding:8px;background:#F0F9FF;border-radius:6px;font-size:12px;">
            <strong style="color:#0D7377;">بدائل مقترحة تلقائياً:</strong>
            <?php foreach (array_slice($suggestions, 0, 3) as $s): ?>
            <span style="display:inline-block;background:#fff;border:1px solid #BAE6FD;border-radius:4px;padding:2px 6px;margin:2px;"><?= e($s['day_name'] ?? '') ?> <?= e($s['date'] ?? '') ?> (<?= e(substr($s['start_time'] ?? '', 0, 5)) ?>)</span>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
        <?php endif; ?>

        <?php if ($req['status'] === 'pending'): ?>
        <div style="margin-top:10px;display:flex;gap:8px;align-items:center;">
            <form method="POST" action="/scheduling/requests/<?= (int) $req['id'] ?>/approve" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn btn-sm btn-primary">قبول وتطبيق</button></form>
            <form method="POST" action="/scheduling/requests/<?= (int) $req['id'] ?>/reject" style="display:inline-flex;gap:5px;align-items:center;">
                <?= csrf_field() ?>
                <input type="text" name="notes" placeholder="ملاحظة الرفض..." style="font-size:12px;padding:4px 8px;border:1px solid #D1D5DB;border-radius:4px;width:180px;">
                <button type="submit" class="btn btn-sm btn-outline" style="color:#DC2626;">رفض</button>
            </form>
        </div>
        <?php endif; ?>

        <?php if (!empty($req['review_notes'])): ?>
        <div style="margin-top:6px;font-size:12px;color:#6B7280;font-style:italic;">ملاحظة المراجع: <?= e($req['review_notes']) ?></div>
        <?php endif; ?>
    </div>
    <?php endforeach; ?>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>
