<?php require ROOT_PATH . '/admin/templates/header.php'; ?>

<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

<?php if(!empty($_GET['error'])&&$_GET['error']==='protected'): ?>
<div class="alert alert-warning alert-dismissible fade show py-2">
    <?= __('languages.protected_notice','This language cannot be deleted.') ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if(!empty($_GET['deleted'])): ?>
<div class="alert alert-success alert-dismissible fade show py-2">
    <?= __('languages.deleted','Language deleted successfully.') ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-4">
    <div>
        <h3 class="mb-1 fw-bold"><i class="bi bi-translate me-2 text-primary"></i><?= __('languages.title','Languages') ?></h3>
        <p class="text-muted mb-0 small"><?= __('languages.subtitle','Manage store languages and translations') ?></p>
    </div>
    <a href="/admin/languages/create" class="btn btn-primary">
        <i class="bi bi-plus-lg me-1"></i><?= __('languages.add','Add Language') ?>
    </a>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white border-bottom d-flex justify-content-between align-items-center py-3">
        <h6 class="mb-0 fw-semibold"><i class="bi bi-list-ul me-2 text-muted"></i><?= __('languages.list','Languages List') ?></h6>
        <span class="badge bg-secondary rounded-pill"><?= count($languages??[]) ?></span>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th class="ps-3"><?= __('languages.code','Code') ?></th>
                        <th><?= __('languages.name','Name') ?></th>
                        <th><?= __('languages.default','Default') ?></th>
                        <th class="text-end pe-3"><?= __('languages.actions','Actions') ?></th>
                    </tr>
                </thead>
                <tbody>
                <?php if(!empty($languages)): foreach($languages as $lang): ?>
                <tr>
                    <td class="ps-3">
                        <span class="badge bg-light text-dark border fw-bold px-3"><?= htmlspecialchars($lang['code']) ?></span>
                    </td>
                    <td class="fw-semibold"><?= htmlspecialchars($lang['name']) ?></td>
                    <td>
                        <?php if(!empty($lang['is_default'])): ?>
                            <span class="badge bg-success-subtle text-success border border-success-subtle">
                                <i class="bi bi-check-circle me-1"></i><?= __('languages.yes','Default') ?>
                            </span>
                        <?php else: ?>
                            <form method="post" action="/admin/languages/default/<?= (int)$lang['id'] ?>" style="display:inline">
                                <?= \Core\System\Csrf::field() ?>
                                <button type="submit" class="btn btn-sm btn-outline-secondary">
                                    <?= __('languages.set_default','Set Default') ?>
                                </button>
                            </form>
                        <?php endif; ?>
                    </td>
                    <td class="text-end pe-3">
                        <a href="/admin/languages/edit/<?= (int)$lang['id'] ?>" class="btn btn-sm btn-outline-warning" title="<?= __('languages.edit','Edit') ?>">
                            <i class="bi bi-pencil"></i>
                        </a>
                        <a href="/admin/languages/translate/<?= htmlspecialchars($lang['code']) ?>" class="btn btn-sm btn-outline-info" title="<?= __('languages.translate','Translate') ?>">
                            <i class="bi bi-globe2"></i>
                        </a>
                        <?php if(empty($lang['is_default'])&&$lang['code']!=='en'&&$lang['code']!=='ar'): ?>
                        <form method="post" action="/admin/languages/delete/<?= (int)$lang['id'] ?>" class="d-inline" onsubmit="return confirm('<?= __('languages.confirm_delete','Delete this language?') ?>')">
                            <?= \Core\System\Csrf::field() ?>
                            <button type="submit" class="btn btn-sm btn-outline-danger">
                                <i class="bi bi-trash3"></i>
                            </button>
                        </form>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; else: ?>
                <tr><td colspan="4" class="text-center py-4 text-muted"><?= __('languages.no_languages','No languages found.') ?></td></tr>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

</div></section></div>
<?php require ROOT_PATH . '/admin/templates/footer.php'; ?>