<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تذكرة #<?= e($ticket['ticket_number']) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/support" class="btn btn-outline">← العودة للتذاكر</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<?php
use App\Modules\Support\Models\Ticket;
use App\Modules\Support\Services\AttachmentService;

$statusColor = match($ticket['status']) { 'open' => '#DC2626', 'in_progress' => '#3B82F6', 'waiting' => '#F59E0B', 'closed' => '#059669', default => '#6B7280' };
$priorityColor = match($ticket['priority']) { 'urgent' => '#DC2626', 'high' => '#F59E0B', 'normal' => '#6B7280', 'low' => '#9CA3AF', default => '#6B7280' };
?>

<div style="display:grid;grid-template-columns:1fr 300px;gap:20px;">
    <!-- Main Content -->
    <div>
        <!-- Ticket Header -->
        <div class="card" style="margin-bottom:20px;">
            <div style="padding:20px;">
                <div style="display:flex;justify-content:space-between;align-items:start;margin-bottom:15px;">
                    <h2 style="margin:0;font-size:20px;color:#1A1A2E;line-height:1.4;"><?= e($ticket['subject']) ?></h2>
                    <span style="background:<?= $statusColor ?>22;color:<?= $statusColor ?>;padding:4px 14px;border-radius:14px;font-size:13px;font-weight:700;white-space:nowrap;"><?= e(Ticket::getStatusLabel($ticket['status'])) ?></span>
                </div>
                <div style="display:flex;gap:15px;font-size:12px;color:#6B7280;flex-wrap:wrap;">
                    <span>&#x1f3ab; <?= e($ticket['ticket_number']) ?></span>
                    <span>&#x1f4c5; <?= e(substr($ticket['created_at'], 0, 16)) ?></span>
                    <span>&#x1f464; <?= e($ticket['created_by_name'] ?? '—') ?></span>
                    <?php if ($ticket['category']): ?><span>&#x1f4c2; <?= e(Ticket::getCategoryLabel($ticket['category'])) ?></span><?php endif; ?>
                    <span style="color:<?= $priorityColor ?>;font-weight:600;">&#x26a1; <?= e(Ticket::getPriorityLabel($ticket['priority'])) ?></span>
                </div>
            </div>
        </div>

        <!-- Ticket Body -->
        <div class="card" style="margin-bottom:20px;">
            <div style="padding:20px;">
                <div style="font-size:14px;line-height:1.8;white-space:pre-wrap;word-wrap:break-word;color:#1F2937;font-family:inherit;max-height:none;overflow:visible;"><?= e($ticket['body']) ?></div>

                <?php if (!empty($ticketAttachments)): ?>
                <div style="margin-top:20px;padding-top:15px;border-top:1px solid #E5E7EB;">
                    <div style="font-size:12px;font-weight:700;color:#6B7280;margin-bottom:10px;">&#x1f4ce; المرفقات (<?= count($ticketAttachments) ?>)</div>
                    <div style="display:flex;flex-wrap:wrap;gap:10px;">
                        <?php foreach ($ticketAttachments as $a): ?>
                        <a href="/support/attachments/<?= (int) $a['id'] ?>" target="_blank" style="display:flex;align-items:center;gap:8px;padding:8px 14px;background:#F3F4F6;border-radius:8px;text-decoration:none;color:#374151;font-size:13px;border:1px solid #E5E7EB;">
                            <?php if (AttachmentService::isImage($a['mime_type'])): ?>
                                <img src="/support/attachments/<?= (int) $a['id'] ?>" style="width:40px;height:40px;object-fit:cover;border-radius:4px;">
                            <?php else: ?>
                                <span style="font-size:20px;">&#x1f4c4;</span>
                            <?php endif; ?>
                            <div>
                                <div style="font-weight:600;max-width:150px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"><?= e($a['original_filename']) ?></div>
                                <div style="font-size:11px;color:#9CA3AF;"><?= AttachmentService::formatSize((int) $a['file_size']) ?></div>
                            </div>
                        </a>
                        <?php endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Replies -->
        <?php if (!empty($replies)): ?>
        <div style="margin-bottom:20px;">
            <h3 style="color:#374151;font-size:15px;margin:0 0 15px;padding-bottom:10px;border-bottom:1px solid #E5E7EB;">&#x1f4ac; الردود (<?= count($replies) ?>)</h3>

            <?php foreach ($replies as $reply): ?>
            <div class="card" style="margin-bottom:12px;border-right:4px solid #0D7377;">
                <div style="padding:15px 20px;">
                    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:10px;">
                        <span style="font-weight:700;color:#0D7377;font-size:14px;"><?= e($reply['employee_name'] ?? 'غير معروف') ?></span>
                        <span style="font-size:11px;color:#9CA3AF;"><?= e(substr($reply['created_at'], 0, 16)) ?></span>
                    </div>
                    <div style="font-size:14px;line-height:1.8;white-space:pre-wrap;word-wrap:break-word;color:#1F2937;"><?= e($reply['body']) ?></div>

                    <?php
                    $rAttachments = $replyAttachments[(int) $reply['id']] ?? [];
                    if (!empty($rAttachments)): ?>
                    <div style="margin-top:12px;padding-top:10px;border-top:1px solid #F3F4F6;">
                        <div style="display:flex;flex-wrap:wrap;gap:8px;">
                            <?php foreach ($rAttachments as $ra): ?>
                            <a href="/support/attachments/<?= (int) $ra['id'] ?>" target="_blank" style="display:flex;align-items:center;gap:6px;padding:6px 12px;background:#F9FAFB;border-radius:6px;text-decoration:none;color:#374151;font-size:12px;border:1px solid #E5E7EB;">
                                <?php if (AttachmentService::isImage($ra['mime_type'])): ?>
                                    <img src="/support/attachments/<?= (int) $ra['id'] ?>" style="width:30px;height:30px;object-fit:cover;border-radius:3px;">
                                <?php else: ?>
                                    <span>&#x1f4c4;</span>
                                <?php endif; ?>
                                <span style="max-width:120px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"><?= e($ra['original_filename']) ?></span>
                            </a>
                            <?php endforeach; ?>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <!-- Reply Form -->
        <?php if ($ticket['status'] !== 'closed'): ?>
        <div class="card">
            <div style="padding:20px;">
                <h3 style="margin:0 0 15px;font-size:15px;color:#374151;">&#x270f;&#xfe0f; إضافة رد</h3>
                <form method="POST" action="/support/<?= (int) $ticket['id'] ?>/reply" enctype="multipart/form-data">
                    <?= csrf_field() ?>
                    <div class="form-group" style="margin-bottom:15px;">
                        <textarea name="body" class="form-input" rows="8" required placeholder="اكتب ردك هنا...&#10;&#10;يمكنك لصق نصوص طويلة، أكواد خطأ، بيانات..." style="font-size:14px;line-height:1.7;min-height:150px;resize:vertical;"></textarea>
                    </div>
                    <div class="form-group" style="margin-bottom:15px;">
                        <label class="form-label" style="font-size:12px;">مرفقات إضافية</label>
                        <input type="file" name="attachments[]" multiple class="form-input" accept="image/*,.pdf,.doc,.docx,.xls,.xlsx,.txt,.csv,.zip,.rar,.mp4,.mov">
                    </div>
                    <button type="submit" class="btn btn-primary">إرسال الرد</button>
                </form>
            </div>
        </div>
        <?php else: ?>
        <div class="card" style="background:#F0FDF4;border:1px solid #A7F3D0;">
            <div style="padding:20px;text-align:center;">
                <span style="font-size:24px;">&#x2705;</span>
                <p style="margin:8px 0 0;color:#065F46;font-weight:600;">تم إغلاق هذه التذكرة</p>
                <?php if ($ticket['closed_at']): ?>
                    <p style="margin:5px 0 0;font-size:12px;color:#6B7280;">بتاريخ <?= e(substr($ticket['closed_at'], 0, 16)) ?></p>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <!-- Sidebar -->
    <div>
        <!-- Ticket Info Card -->
        <div class="card" style="margin-bottom:15px;">
            <div style="padding:15px;">
                <h4 style="margin:0 0 15px;font-size:14px;color:#374151;border-bottom:1px solid #E5E7EB;padding-bottom:10px;">معلومات التذكرة</h4>
                <table style="width:100%;font-size:13px;">
                    <tr>
                        <td style="padding:6px 0;color:#6B7280;">الحالة</td>
                        <td style="padding:6px 0;"><span style="color:<?= $statusColor ?>;font-weight:700;"><?= e(Ticket::getStatusLabel($ticket['status'])) ?></span></td>
                    </tr>
                    <tr>
                        <td style="padding:6px 0;color:#6B7280;">الأولوية</td>
                        <td style="padding:6px 0;"><span style="color:<?= $priorityColor ?>;font-weight:600;"><?= e(Ticket::getPriorityLabel($ticket['priority'])) ?></span></td>
                    </tr>
                    <tr>
                        <td style="padding:6px 0;color:#6B7280;">التصنيف</td>
                        <td style="padding:6px 0;"><?= e(Ticket::getCategoryLabel($ticket['category'] ?? '')) ?></td>
                    </tr>
                    <tr>
                        <td style="padding:6px 0;color:#6B7280;">المعين إليه</td>
                        <td style="padding:6px 0;font-weight:600;"><?= e($ticket['assigned_to_name'] ?? '— غير معين —') ?></td>
                    </tr>
                    <tr>
                        <td style="padding:6px 0;color:#6B7280;">تاريخ الإنشاء</td>
                        <td style="padding:6px 0;font-size:11px;"><?= e($ticket['created_at']) ?></td>
                    </tr>
                    <tr>
                        <td style="padding:6px 0;color:#6B7280;">آخر تحديث</td>
                        <td style="padding:6px 0;font-size:11px;"><?= e($ticket['updated_at']) ?></td>
                    </tr>
                </table>
            </div>
        </div>

        <!-- Actions -->
        <div class="card" style="margin-bottom:15px;">
            <div style="padding:15px;">
                <h4 style="margin:0 0 12px;font-size:14px;color:#374151;">إجراءات</h4>

                <!-- Assign -->
                <?php if (can('support.assign')): ?>
                <form method="POST" action="/support/<?= (int) $ticket['id'] ?>/assign" style="margin-bottom:12px;">
                    <?= csrf_field() ?>
                    <label class="form-label" style="font-size:11px;">تعيين إلى</label>
                    <div style="display:flex;gap:6px;">
                        <select name="assigned_to" class="form-select" style="font-size:12px;flex:1;">
                            <option value="">— بلا —</option>
                            <?php foreach ($employees as $emp): ?>
                                <option value="<?= (int) $emp['id'] ?>" <?= ((int) ($ticket['assigned_to_employee_id'] ?? 0)) === (int) $emp['id'] ? 'selected' : '' ?>><?= e($emp['full_name_ar']) ?></option>
                            <?php endforeach; ?>
                        </select>
                        <button type="submit" class="btn btn-sm btn-outline">تعيين</button>
                    </div>
                </form>
                <?php endif; ?>

                <!-- Close / Reopen -->
                <?php if (can('support.close')): ?>
                <?php if ($ticket['status'] !== 'closed'): ?>
                <form method="POST" action="/support/<?= (int) $ticket['id'] ?>/close">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-sm" style="width:100%;background:#059669;color:#fff;border:none;" onclick="return confirm('إغلاق التذكرة؟')">&#x2705; إغلاق التذكرة</button>
                </form>
                <?php else: ?>
                <form method="POST" action="/support/<?= (int) $ticket['id'] ?>/reopen">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-sm" style="width:100%;background:#D97706;color:#fff;border:none;" onclick="return confirm('إعادة فتح التذكرة؟')">&#x1f504; إعادة الفتح</button>
                </form>
                <?php endif; ?>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php $__template->endSection(); ?>
