<?= $this->extend('install/_layout') ?>
<?php $currentStep = 2; ?>

<?= $this->section('content') ?>
<h5 class="mb-3">Step 2 — Folder Permission Check</h5>

<ul class="list-group mb-4">
    <?php foreach ($results as $row): ?>
        <li class="list-group-item req-row <?= $row['passed'] ? 'pass' : 'fail' ?>">
            <div class="d-flex justify-content-between">
                <span><?= esc($row['label']) ?> <small class="text-muted"><?= esc($row['path']) ?></small></span>
                <span><?= $row['passed'] ? 'Writable ✅' : 'Not writable ❌' ?></span>
            </div>
            <?php if (! $row['passed']): ?>
                <pre class="bg-light p-2 mt-2 mb-0"><code><?= esc($row['fix_command']) ?></code></pre>
            <?php endif; ?>
        </li>
    <?php endforeach; ?>
</ul>

<?php if (! $allPassed): ?>
    <div class="alert alert-danger">Run the fix commands above on your server, then refresh this page.</div>
<?php endif; ?>

<div class="d-flex justify-content-between">
    <a href="<?= base_url('install/step/1') ?>" class="btn btn-outline-secondary">← Back</a>
    <button id="nextBtn" class="btn btn-primary" <?= $allPassed ? '' : 'disabled' ?>>Next: Database →</button>
</div>
<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
document.getElementById('nextBtn').addEventListener('click', async () => {
    const res = await fetch('<?= base_url('install/step/2') ?>', { method: 'POST', headers: {'X-Requested-With':'XMLHttpRequest'} });
    const json = await res.json();
    if (json.success) { window.location.href = json.data.next; }
    else { alert(json.errors[0].message); }
});
</script>
<?= $this->endSection() ?>
