<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تذاكر الدعم الفني<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/support/create" class="btn btn-primary">+ تذكرة جديدة</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<?php
use App\Modules\Support\Models\Ticket;
use App\Modules\Support\Services\AttachmentService;
?>

<!-- Status Summary -->
<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:12px;margin-bottom:20px;">
    <div style="background:#fff;border:2px solid <?= ($filters['status'] ?? '') === 'open' ? '#DC2626' : '#E5E7EB' ?>;border-radius:10px;padding:15px;text-align:center;">
        <a href="/support?status=open" style="text-decoration:none;color:inherit;">
            <div style="font-size:28px;font-weight:700;color:#DC2626;"><?= (int) $counts['open'] ?></div>
            <div style="font-size:12px;color:#6B7280;">مفتوحة</div>
        </a>
    </div>
    <div style="background:#fff;border:2px solid <?= ($filters['status'] ?? '') === 'in_progress' ? '#3B82F6' : '#E5E7EB' ?>;border-radius:10px;padding:15px;text-align:center;">
        <a href="/support?status=in_progress" style="text-decoration:none;color:inherit;">
            <div style="font-size:28px;font-weight:700;color:#3B82F6;"><?= (int) $counts['in_progress'] ?></div>
            <div style="font-size:12px;color:#6B7280;">قيد المعالجة</div>
        </a>
    </div>
    <div style="background:#fff;border:2px solid <?= ($filters['status'] ?? '') === 'waiting' ? '#F59E0B' : '#E5E7EB' ?>;border-radius:10px;padding:15px;text-align:center;">
        <a href="/support?status=waiting" style="text-decoration:none;color:inherit;">
            <div style="font-size:28px;font-weight:700;color:#F59E0B;"><?= (int) $counts['waiting'] ?></div>
            <div style="font-size:12px;color:#6B7280;">في الانتظار</div>
        </a>
    </div>
    <div style="background:#fff;border:2px solid <?= ($filters['status'] ?? '') === 'closed' ? '#059669' : '#E5E7EB' ?>;border-radius:10px;padding:15px;text-align:center;">
        <a href="/support?status=closed" style="text-decoration:none;color:inherit;">
            <div style="font-size:28px;font-weight:700;color:#059669;"><?= (int) $counts['closed'] ?></div>
            <div style="font-size:12px;color:#6B7280;">مغلقة</div>
        </a>
    </div>
</div>

<!-- Filters -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:12px 20px;">
        <form method="GET" action="/support" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
            <div>
                <label class="form-label" style="font-size:11px;">بحث</label>
                <input type="text" name="search" class="form-input" value="<?= e($filters['search'] ?? '') ?>" placeholder="رقم التذكرة، الموضوع...">
            </div>
            <div>
                <label class="form-label" style="font-size:11px;">الحالة</label>
                <select name="status" class="form-select">
                    <option value="">الكل</option>
                    <option value="open" <?= ($filters['status'] ?? '') === 'open' ? 'selected' : '' ?>>مفتوحة</option>
                    <option value="in_progress" <?= ($filters['status'] ?? '') === 'in_progress' ? 'selected' : '' ?>>قيد المعالجة</option>
                    <option value="waiting" <?= ($filters['status'] ?? '') === 'waiting' ? 'selected' : '' ?>>في الانتظار</option>
                    <option value="closed" <?= ($filters['status'] ?? '') === 'closed' ? 'selected' : '' ?>>مغلقة</option>
                </select>
            </div>
            <div>
                <label class="form-label" style="font-size:11px;">الأولوية</label>
                <select name="priority" class="form-select">
                    <option value="">الكل</option>
                    <option value="urgent" <?= ($filters['priority'] ?? '') === 'urgent' ? 'selected' : '' ?>>عاجل</option>
                    <option value="high" <?= ($filters['priority'] ?? '') === 'high' ? 'selected' : '' ?>>مرتفع</option>
                    <option value="normal" <?= ($filters['priority'] ?? '') === 'normal' ? 'selected' : '' ?>>عادي</option>
                    <option value="low" <?= ($filters['priority'] ?? '') === 'low' ? 'selected' : '' ?>>منخفض</option>
                </select>
            </div>
            <div>
                <label class="form-label" style="font-size:11px;">التصنيف</label>
                <select name="category" class="form-select">
                    <option value="">الكل</option>
                    <?php foreach (Ticket::getCategories() as $k => $v): ?>
                        <option value="<?= $k ?>" <?= ($filters['category'] ?? '') === $k ? 'selected' : '' ?>><?= e($v) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <button type="submit" class="btn btn-outline">بحث</button>
            <?php if (array_filter($filters)): ?>
                <a href="/support" class="btn btn-outline" style="color:#DC2626;border-color:#DC2626;">مسح</a>
            <?php endif; ?>
        </form>
    </div>
</div>

<!-- Tickets List -->
<div class="card">
    <div class="table-responsive">
        <table class="data-table" style="width:100%;">
            <thead>
                <tr>
                    <th>رقم التذكرة</th>
                    <th>الموضوع</th>
                    <th>التصنيف</th>
                    <th>الأولوية</th>
                    <th>الحالة</th>
                    <th>مقدم الطلب</th>
                    <th>المعين إليه</th>
                    <th>التاريخ</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($tickets as $t):
                    $priorityColor = match($t['priority']) { 'urgent' => '#DC2626', 'high' => '#F59E0B', 'normal' => '#6B7280', 'low' => '#9CA3AF', default => '#6B7280' };
                    $statusColor = match($t['status']) { 'open' => '#DC2626', 'in_progress' => '#3B82F6', 'waiting' => '#F59E0B', 'closed' => '#059669', default => '#6B7280' };
                ?>
                <tr<?= $t['status'] === 'closed' ? ' style="opacity:0.6;"' : '' ?>>
                    <td style="direction:ltr;text-align:right;font-weight:600;font-size:12px;white-space:nowrap;"><?= e($t['ticket_number']) ?></td>
                    <td style="font-weight:600;max-width:250px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">
                        <a href="/support/<?= (int)$t['id'] ?>" style="color:#1A1A2E;"><?= e($t['subject']) ?></a>
                    </td>
                    <td style="font-size:12px;"><?= e(Ticket::getCategoryLabel($t['category'] ?? '')) ?></td>
                    <td><span style="color:<?= $priorityColor ?>;font-weight:700;font-size:12px;"><?= e(Ticket::getPriorityLabel($t['priority'])) ?></span></td>
                    <td><span style="background:<?= $statusColor ?>22;color:<?= $statusColor ?>;padding:3px 10px;border-radius:12px;font-size:12px;font-weight:600;"><?= e(Ticket::getStatusLabel($t['status'])) ?></span></td>
                    <td style="font-size:12px;"><?= e($t['created_by_name'] ?? '—') ?></td>
                    <td style="font-size:12px;"><?= e($t['assigned_to_name'] ?? '—') ?></td>
                    <td style="font-size:11px;color:#6B7280;white-space:nowrap;"><?= e(substr($t['created_at'], 0, 16)) ?></td>
                    <td><a href="/support/<?= (int)$t['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($tickets)): ?>
                <tr><td colspan="9" style="text-align:center;padding:40px;color:#9CA3AF;">
                    <div style="font-size:48px;margin-bottom:10px;">&#x1f3ab;</div>
                    لا توجد تذاكر
                </td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if (!empty($pagination) && $pagination['total_pages'] > 1): ?>
    <div style="padding:15px 20px;border-top:1px solid #E5E7EB;display:flex;justify-content:center;gap:5px;">
        <?php for ($p = 1; $p <= $pagination['total_pages']; $p++): ?>
            <?php $params = array_merge($filters, ['page' => $p]); ?>
            <a href="/support?<?= http_build_query(array_filter($params)) ?>" class="btn btn-sm <?= $p === $pagination['current_page'] ? 'btn-primary' : 'btn-outline' ?>"><?= $p ?></a>
        <?php endfor; ?>
    </div>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>
