<?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/ministries/create" class="btn btn-primary">
            <i class="fa-solid fa-plus me-1"></i> <?= gettext('Add Ministry') ?>
        </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('Icon') ?></th>
                            <th><?= gettext('Title') ?></th>
                            <th><?= gettext('Description') ?></th>
                            <th><?= gettext('Sort Order') ?></th>
                            <th><?= gettext('Actions') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($ministries)): ?>
                        <tr>
                            <td colspan="5" class="text-center text-secondary py-4">
                                <?= gettext('No ministries found.') ?>
                            </td>
                        </tr>
                        <?php else: ?>
                        <?php foreach ($ministries as $ministry): ?>
                        <tr>
                            <td>
                                <?php if ($ministry['min_image_url']): ?>
                                <img src="<?= $ministry['min_image_url'] ?>" alt="<?= $ministry['min_title'] ?>" style="width:48px;height:48px;object-fit:cover;border-radius:8px;">
                                <?php elseif ($ministry['min_icon']): ?>
                                <div style="width:48px;height:48px;border-radius:8px;background:<?= $ministry['min_bg_color'] ?>;display:flex;align-items:center;justify-content:center;color:#555;font-size:1.2rem;">
                                    <i class="<?= $ministry['min_icon'] ?>"></i>
                                </div>
                                <?php else: ?>
                                <div style="width:48px;height:48px;border-radius:8px;background:#e3f2fd;display:flex;align-items:center;justify-content:center;color:#555;">
                                    <i class="fa-solid fa-hand-holding-heart"></i>
                                </div>
                                <?php endif; ?>
                            </td>
                            <td><strong><?= $ministry['min_title'] ?></strong></td>
                            <td><small class="text-secondary"><?= substr(strip_tags($ministry['min_description']), 0, 100) ?: '-' ?></small></td>
                            <td><?= $ministry['min_sort_order'] ?: 0 ?></td>
                            <td class="text-nowrap">
                                <a href="<?= SystemURLs::getRootPath() ?>/admin/website/ministries/edit/<?= $ministry['min_id'] ?>" class="btn btn-sm btn-outline-primary">
                                    <i class="fa-solid fa-pen"></i>
                                </a>
                                <a href="<?= SystemURLs::getRootPath() ?>/admin/website/ministries/delete/<?= $ministry['min_id'] ?>"
                                   class="btn btn-sm btn-outline-danger"
                                   onclick="return confirm('<?= gettext('Delete this ministry?') ?>')">
                                    <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'; ?>
