<?php

use ChurchCRM\dto\SystemURLs;

include SystemURLs::getDocumentRoot() . '/Include/Header.php';
?>

<div class="container-fluid">
    <div class="d-flex justify-content-end mb-3">
        <a href="<?= SystemURLs::getRootPath() ?>/admin/website/sermons/create" class="btn btn-primary">
            <i class="fa-solid fa-plus me-1"></i> <?= gettext('Add Sermon') ?>
        </a>
    </div>

    <div class="card shadow-sm">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-vcenter card-table">
                    <thead>
                        <tr>
                            <th><?= gettext('Title') ?></th>
                            <th><?= gettext('Speaker') ?></th>
                            <th><?= gettext('Date') ?></th>
                            <th><?= gettext('Scripture') ?></th>
                            <th><?= gettext('Featured') ?></th>
                            <th><?= gettext('Actions') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($sermons)): ?>
                        <tr>
                            <td colspan="6" class="text-center text-secondary py-4">
                                <?= gettext('No sermons found.') ?>
                            </td>
                        </tr>
                        <?php else: ?>
                        <?php foreach ($sermons as $sermon): ?>
                        <tr>
                            <td>
                                <strong><?= $sermon['serm_title'] ?></strong>
                                <?php if ($sermon['serm_description']): ?>
                                <br><small class="text-secondary"><?= substr($sermon['serm_description'], 0, 80) ?: '' ?></small>
                                <?php endif; ?>
                            </td>
                            <td><?= $sermon['serm_speaker'] ?: '-' ?></td>
                            <td><?= $sermon['serm_date'] ? date('M j, Y', strtotime($sermon['serm_date'])) : '-' ?></td>
                            <td><?= $sermon['serm_scripture'] ?: '-' ?></td>
                            <td>
                                <?php if ($sermon['serm_featured']): ?>
                                    <span class="badge bg-success"><?= gettext('Yes') ?></span>
                                <?php else: ?>
                                    <span class="badge bg-secondary"><?= gettext('No') ?></span>
                                <?php endif; ?>
                            </td>
                            <td class="text-nowrap">
                                <a href="<?= SystemURLs::getRootPath() ?>/admin/website/sermons/edit/<?= $sermon['serm_id'] ?>" class="btn btn-sm btn-outline-primary">
                                    <i class="fa-solid fa-pen"></i>
                                </a>
                                <a href="<?= SystemURLs::getRootPath() ?>/admin/website/sermons/delete/<?= $sermon['serm_id'] ?>"
                                   class="btn btn-sm btn-outline-danger"
                                   onclick="return confirm('<?= gettext('Delete this sermon?') ?>')">
                                    <i class="fa-solid fa-trash"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php include SystemURLs::getDocumentRoot() . '/Include/Footer.php'; ?>
