<?php require __DIR__ . '/settings_nav.php'; ?>
<div class="mb-3">
    <h2 class="mb-1">🔏 <?= h(t('privacy.heading')) ?></h2>
    <div class="text-muted"><?= h(t('privacy.intro')) ?></div>
</div>
<?php if ($saved): ?><div class="alert alert-success">✅ <?= h(t('settings.saved')) ?></div>
<?php endif; ?><?php if ($error): ?><div class="alert alert-danger"><?= h($error) ?></div><?php endif; ?>
<div class="card mb-3">
    <div class="card-header">
        <h3 class="card-title"><?= h(t('privacy.retention')) ?></h3>
    </div>
    <form method="post" action="/settings/privacy/save"><input type="hidden" name="csrf" value="<?= h(Auth::csrf()) ?>">
        <div class="card-body">
            <div class="row g-3">
                <?php foreach (['privacy_retention_years' => t('privacy.other_contacts'),'privacy_retention_adopter_years' => t('privacy.adopters'),'privacy_retention_foster_years' => t('privacy.fosters'),'privacy_retention_volunteer_years' => t('privacy.volunteers')] as $key => $label): ?>
                <div class="col-sm-6 col-xl-3"><label class="form-label"><?= h($label) ?></label>
                    <div class="input-group"><input class="form-control" type="number" min="1" max="30"
                            name="<?= h($key) ?>" value="<?= (int)$settings[$key] ?>"><span
                            class="input-group-text"><?= h(t('privacy.years')) ?></span></div>
                </div><?php endforeach; ?><div class="col-sm-6 col-xl-3"><label
                        class="form-label"><?= h(t('privacy.access_retention')) ?></label>
                    <div class="input-group"><input class="form-control" type="number" min="1" max="120"
                            name="privacy_access_log_months"
                            value="<?= (int)$settings['privacy_access_log_months'] ?>"><span
                            class="input-group-text"><?= h(t('privacy.months')) ?></span></div>
                </div>
                <div class="col-12">
                    <div class="alert alert-info mb-0 py-2"><?= h(t('privacy.no_auto_delete')) ?></div>
                </div>
            </div>
        </div>
        <div class="card-footer text-end"><button class="btn btn-primary"><?= h(t('common.save')) ?></button></div>
    </form>
</div>
<div class="card mb-3">
    <div class="card-header">
        <h3 class="card-title"><?= h(t('privacy.review')) ?></h3><span
            class="badge bg-yellow-lt ms-2"><?= count($review) ?></span>
    </div>
    <div class="table-responsive">
        <table class="table table-vcenter card-table">
            <thead>
                <tr>
                    <th><?= h(t('directory.identity')) ?></th>
                    <th><?= h(t('directory.roles')) ?></th>
                    <th><?= h(t('privacy.last_activity')) ?></th>
                    <th><?= h(t('privacy.applied_policy')) ?></th>
                    <th></th>
                </tr>
            </thead>
            <tbody><?php foreach ($review as $row): ?><tr>
                    <td><a href="/directory/view?id=<?= (int)$row['id'] ?>"><?= h($row['name']) ?></a>
                        <div class="small text-muted"><?= h($row['email'] ?: $row['phone']) ?></div>
                    </td>
                    <td><?= h($row['roles'] ?: '—') ?></td>
                    <td><?= h($row['last_activity'] ? date('d/m/Y', strtotime($row['last_activity'])) : '—') ?></td>
                    <td><?= (int)$row['policy_years'] ?> <?= h(t('privacy.years')) ?><?php if ($row['blocked']): ?><div
                            class="badge bg-red-lt"><?= h(t('privacy.active_link')) ?></div><?php endif; ?></td>
                    <td class="text-end"><a class="btn btn-sm btn-outline-primary"
                            href="/settings/privacy/export?id=<?= (int)$row['id'] ?>">⬇
                            <?= h(t('privacy.export')) ?></a><?php if (!$row['blocked']): ?><form
                            class="d-inline-flex flex-wrap gap-1 justify-content-end" method="post"
                            action="/settings/privacy/anonymize"
                            onsubmit="return confirm(<?= h(json_encode(t('privacy.anonymize_confirm'), JSON_HEX_APOS | JSON_HEX_QUOT)) ?>)">
                            <input type="hidden" name="csrf" value="<?= h(Auth::csrf()) ?>"><input type="hidden"
                                name="id" value="<?= (int)$row['id'] ?>"><input class="form-control form-control-sm"
                                type="text" name="reason" required maxlength="255"
                                placeholder="<?= h(t('privacy.reason')) ?>"><label class="form-check small"><input
                                    class="form-check-input" type="checkbox" name="confirm_reviewed" required><span
                                    class="form-check-label"><?= h(t('privacy.reviewed')) ?></span></label><button
                                class="btn btn-sm btn-outline-danger"><?= h(t('privacy.anonymize')) ?></button></form>
                        <?php endif; ?></td>
                </tr><?php endforeach; ?><?php if (!$review): ?><tr>
                    <td colspan="5" class="text-center text-muted py-4"><?= h(t('privacy.none_review')) ?></td>
                </tr><?php endif; ?></tbody>
        </table>
    </div>
</div>
<div class="card">
    <div class="card-header">
        <h3 class="card-title"><?= h(t('privacy.access_register')) ?></h3>
    </div>
    <div class="table-responsive">
        <table class="table table-vcenter card-table">
            <thead>
                <tr>
                    <th><?= h(t('common.date')) ?></th>
                    <th><?= h(t('audit.user')) ?></th>
                    <th><?= h(t('privacy.resource')) ?></th>
                    <th><?= h(t('animal.animal')) ?></th>
                    <th><?= h(t('privacy.action')) ?></th>
                </tr>
            </thead>
            <tbody><?php foreach ($accesses as $row): ?><tr>
                    <td><?= h(date('d/m/Y H:i', strtotime($row['accessed_at']))) ?></td>
                    <td><?= h($row['actor'] ?: '—') ?></td>
                    <td><?= h($row['resource_type'] . ' · ' . ($row['original_name'] ?: '#' . $row['resource_id'])) ?>
                    </td>
                    <td><?= h($row['animal_name'] ?: '—') ?></td>
                    <td><?= h($row['action']) ?></td>
                </tr><?php endforeach; ?><?php if (!$accesses): ?><tr>
                    <td colspan="5" class="text-center text-muted py-4"><?= h(t('privacy.no_access')) ?></td>
                </tr><?php endif; ?></tbody>
        </table>
    </div>
</div>
