<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<?php
$prio   = $lookups->taskPriorities[$complaint->priority] ?? ['label' => ucfirst($complaint->priority), 'badge' => 'secondary'];
$status = $lookups->complaintStatuses[$complaint->status] ?? ['label' => ucfirst($complaint->status), 'badge' => 'secondary'];
$cat    = $lookups->complaintCategories[$complaint->category]['label'] ?? $complaint->category;
?>

<div class="d-flex justify-content-between align-items-start mb-3">
    <div>
        <h1 class="h3 mb-0">CR-<?= sprintf('%04d', $complaint->id) ?> — <?= esc($complaint->guest_name) ?></h1>
        <p class="text-muted mb-0">
            <?= esc($cat) ?> · <span class="badge bg-<?= $status['badge'] ?>"><?= esc($status['label']) ?></span>
            <span class="badge bg-<?= $prio['badge'] ?>"><?= esc($prio['label']) ?></span>
        </p>
    </div>
    <a href="<?= site_url('guests/' . $complaint->guest_id) ?>" class="btn btn-outline-secondary btn-sm">View Guest</a>
</div>

<div class="row">
    <div class="col-lg-8 mb-3">
        <div class="card shadow-sm mb-3">
            <div class="card-body">
                <h2 class="h6">What happened</h2>
                <p class="mb-0"><?= nl2br(esc($complaint->description)) ?></p>
                <hr>
                <div class="row row-cols-2 g-3 small">
                    <div class="col"><span class="text-muted">Reported By</span><br><span class="fw-semibold"><?= esc($complaint->reporter_name ?: '—') ?></span></div>
                    <div class="col"><span class="text-muted">Assigned To</span><br><span class="fw-semibold"><?= esc($complaint->assignee_name ?: 'Unassigned') ?></span></div>
                    <div class="col"><span class="text-muted">Logged</span><br><span class="fw-semibold"><?= esc(date('d M Y, g:i a', strtotime($complaint->created_at))) ?></span></div>
                    <div class="col"><span class="text-muted">Phone</span><br><span class="fw-semibold"><?= esc($complaint->guest_phone) ?></span></div>
                </div>
            </div>
        </div>

        <?php if ($complaint->resolution_notes): ?>
        <div class="card shadow-sm mb-3">
            <div class="card-header d-flex align-items-center gap-2"><i class="bi bi-check2-circle text-success"></i> Resolution</div>
            <div class="card-body">
                <p class="mb-1"><?= nl2br(esc($complaint->resolution_notes)) ?></p>
                <span class="text-muted small">Resolved <?= esc(date('d M Y, g:i a', strtotime($complaint->resolved_at))) ?></span>
            </div>
        </div>
        <?php endif; ?>

        <?php if ($complaint->follow_up_notes): ?>
        <div class="card shadow-sm">
            <div class="card-header d-flex align-items-center gap-2"><i class="bi bi-telephone-outbound"></i> Follow-Up</div>
            <div class="card-body">
                <p class="mb-1"><?= nl2br(esc($complaint->follow_up_notes)) ?></p>
                <span class="text-muted small">Followed up <?= esc(date('d M Y, g:i a', strtotime($complaint->follow_up_at))) ?></span>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <div class="col-lg-4">
        <div class="card shadow-sm">
            <div class="card-header">Workflow</div>
            <div class="card-body d-flex flex-column gap-3">
                <?php if (in_array($complaint->status, ['open', 'assigned'], true)): ?>
                    <form action="<?= site_url('complaints/' . $complaint->id . '/assign') ?>" method="post">
                        <?= csrf_field() ?>
                        <label class="form-label small fw-semibold">Assign To</label>
                        <select class="form-select form-select-sm mb-2" name="assigned_to" required>
                            <option value="">— Choose staff —</option>
                            <?php foreach ($staff as $s): ?>
                                <option value="<?= $s->id ?>" <?= $complaint->assigned_to == $s->id ? 'selected' : '' ?>><?= esc($s->full_name) ?></option>
                            <?php endforeach; ?>
                        </select>
                        <button type="submit" class="btn btn-outline-secondary btn-sm w-100">Assign</button>
                    </form>

                    <form action="<?= site_url('complaints/' . $complaint->id . '/resolve') ?>" method="post">
                        <?= csrf_field() ?>
                        <label class="form-label small fw-semibold">Resolve</label>
                        <textarea class="form-control form-control-sm mb-2" name="resolution_notes" rows="3" required placeholder="What was done to resolve this?"></textarea>
                        <button type="submit" class="btn btn-success btn-sm w-100"><i class="bi bi-check2 me-1"></i>Mark Resolved</button>
                    </form>
                <?php elseif ($complaint->status === 'resolved'): ?>
                    <form action="<?= site_url('complaints/' . $complaint->id . '/follow-up') ?>" method="post">
                        <?= csrf_field() ?>
                        <label class="form-label small fw-semibold">Log Follow-Up</label>
                        <textarea class="form-control form-control-sm mb-2" name="follow_up_notes" rows="3" required placeholder="How did the follow-up contact go?"></textarea>
                        <button type="submit" class="btn btn-outline-primary btn-sm w-100"><i class="bi bi-telephone-outbound me-1"></i>Log Follow-Up</button>
                    </form>
                <?php else: ?>
                    <p class="text-muted small mb-0">This complaint has been resolved and followed up.</p>
                <?php endif; ?>
                <a href="<?= site_url('complaints') ?>" class="btn btn-outline-secondary btn-sm">Back to Service Recovery</a>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
