<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/iscms_layout.php';

$conn = iscms_init();
$pageTitle = 'Complaints';
$activeNav = 'complaints';

$sql = "SELECT c.id, c.complaint_number, c.complaint_type, c.channel, c.status, c.subject,
               c.received_at, c.resolved_at, c.satisfaction_rating,
               o.org_name,
               p.product_name,
               CONCAT(TRIM(uc.first_name),' ',TRIM(uc.last_name)) AS complainant_name,
               CONCAT(TRIM(ua.first_name),' ',TRIM(ua.last_name)) AS assigned_to_name
        FROM complaints c
        LEFT JOIN organizations o ON c.org_id = o.id
        LEFT JOIN products p ON c.product_id = p.id
        LEFT JOIN users uc ON c.complainant_id = uc.id
        LEFT JOIN users ua ON c.assigned_to = ua.id
        ORDER BY c.updated_at DESC
        LIMIT 400";
$result = $conn->query($sql);
$rows = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$dbError = $result ? null : $conn->error;

iscms_render_head($pageTitle);
iscms_render_sidebar($activeNav);
iscms_render_topbar($pageTitle);
?>
    <div class="page-header">
        <h1>Complaints</h1>
        <p>Public and stakeholder complaints with assignment and resolution status.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Related</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="non_conformances.php"><i class="ri-error-warning-line"></i> Non-conformances</a>
            <a class="qa-btn" href="organizations.php"><i class="ri-building-2-line"></i> Organisations</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-feedback-line"></i> Register</h2>
            <span class="text-muted" style="font-size:.75rem"><?= count($rows) ?> shown</span>
        </div>
        <div class="table-responsive">
            <?php if ($dbError): ?>
                <div class="alert alert-danger m-3" style="font-size:.85rem">Unable to load data: <?= iscms_h($dbError) ?></div>
            <?php else: ?>
            <table class="ssbs-table">
                <thead>
                    <tr>
                        <th>Number</th>
                        <th>Subject</th>
                        <th>Type</th>
                        <th>Channel</th>
                        <th>Status</th>
                        <th>Complainant</th>
                        <th>Organisation</th>
                        <th>Product</th>
                        <th>Assigned</th>
                        <th>Received</th>
                        <th>Resolved</th>
                        <th>Rating</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="12"><i class="ri-inbox-line me-2"></i>No complaints</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['complaint_number']) ?></td>
                            <td><small><?= iscms_h((string) ($r['subject'] ?? '')) ?></small></td>
                            <td><small><?= iscms_h(str_replace('_', ' ', (string) $r['complaint_type'])) ?></small></td>
                            <td><small><?= iscms_h(str_replace('_', ' ', (string) $r['channel'])) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                            <td><small><?= iscms_val(trim((string) ($r['complainant_name'] ?? '')) ?: null) ?></small></td>
                            <td><small><?= iscms_val($r['org_name'] ?? null) ?></small></td>
                            <td><small><?= iscms_val($r['product_name'] ?? null) ?></small></td>
                            <td><small><?= iscms_val(trim((string) ($r['assigned_to_name'] ?? '')) ?: null) ?></small></td>
                            <td><small class="text-muted"><?= !empty($r['received_at']) ? iscms_h(substr((string) $r['received_at'], 0, 16)) : '-' ?></small></td>
                            <td><small class="text-muted"><?= !empty($r['resolved_at']) ? iscms_h(substr((string) $r['resolved_at'], 0, 16)) : '-' ?></small></td>
                            <td><small><?= isset($r['satisfaction_rating']) && $r['satisfaction_rating'] !== null && $r['satisfaction_rating'] !== '' ? iscms_h((string) $r['satisfaction_rating']) : '-' ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
