<?php
require_once __DIR__ . '/../../inc/auth.php';
require_once __DIR__ . '/../../inc/functions.php';
$session = auth_require('owner', 'engineer');

$pdo = getDB();

$passports = $pdo->query("
    SELECT op.*, o.name as object_name, u.full_name as author_name
    FROM object_passports op
    LEFT JOIN objects o ON o.id = op.object_id
    LEFT JOIN users u ON u.id = op.created_by
    ORDER BY op.created_at DESC
")->fetchAll();

require __DIR__ . '/../../inc/header.php';
?>

<div class="main-header">
    <div>
        <h1>Паспорта объектов</h1>
        <div class="breadcrumb"><a href="/service/dashboard.php">Главная</a> / Паспорта</div>
    </div>
    <a href="../shared/questionnaire.php?new=1" class="btn btn-primary btn-sm" data-tooltip="Начать опрос для создания паспорта">+ Новый опросник</a>
</div>

<div class="card">
    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>Объект</th>
                    <th>Автор</th>
                    <th>Индекс объекта</th>
                    <th>Оценка риска</th>
                    <th>Стоимость SLA</th>
                    <th>Дата</th>
                    <th class="actions"></th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($passports)): ?>
                <tr><td colspan="7" style="text-align:center;color:var(--text-muted);">Нет паспортов</td></tr>
                <?php endif; ?>
                <?php foreach ($passports as $p): ?>
                <tr>
                    <td><strong><?= htmlspecialchars($p['object_name'] ?: '—') ?></strong></td>
                    <td><?= htmlspecialchars($p['author_name'] ?: '—') ?></td>
                    <td><strong><?= $p['object_index'] ?></strong> (<?= object_class($p['object_index']) ?>)</td>
                    <td><span class="badge badge-<?= $p['risk_score'] <= 20 ? 'done' : ($p['risk_score'] <= 50 ? 'high' : ($p['risk_score'] <= 75 ? 'medium' : 'critical')) ?>"><?= $p['risk_score'] ?></span></td>
                    <td><?= fmt_money($p['sla_price_monthly']) ?></td>
                    <td><?= date('d.m.Y', strtotime($p['created_at'])) ?></td>
                    <td class="actions"><a href="passport_view.php?id=<?= $p['id'] ?>" class="btn btn-secondary btn-sm" data-tooltip="Просмотр">👁️</a></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php require __DIR__ . '/../../inc/footer.php'; ?>
