<?php

use ChurchCRM\dto\SystemURLs;

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

<div class="container-fluid">
    <div class="card shadow-sm">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-vcenter card-table">
                    <thead>
                        <tr>
                            <th><?= gettext('Name') ?></th>
                            <th><?= gettext('Email') ?></th>
                            <th><?= gettext('Request') ?></th>
                            <th><?= gettext('Submitted') ?></th>
                            <th><?= gettext('Status') ?></th>
                            <th><?= gettext('Actions') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($prayers)): ?>
                        <tr>
                            <td colspan="6" class="text-center text-secondary py-4">
                                <?= gettext('No prayer requests found.') ?>
                            </td>
                        </tr>
                        <?php else: ?>
                        <?php foreach ($prayers as $p): ?>
                        <tr>
                            <td><?= $p['pray_name'] ?: gettext('Anonymous') ?></td>
                            <td><?= $p['pray_email'] ?: '-' ?></td>
                            <td>
                                <?= substr($p['pray_request'], 0, 100) ?: '' ?>
                                <?php if (strlen($p['pray_request']) > 100): ?>
                                    <a href="#" data-bs-toggle="modal" data-bs-target="#prayerModal<?= $p['pray_id'] ?>"><?= gettext('more') ?></a>
                                <?php endif; ?>
                            </td>
                            <td><?= date('M j, Y g:i A', strtotime($p['pray_created_at'])) ?></td>
                            <td>
                                <?php
                                $statusBadge = match ($p['pray_status']) {
                                    'pending' => 'bg-warning',
                                    'approved' => 'bg-info',
                                    'answered' => 'bg-success',
                                    default => 'bg-secondary'
                                };
                                ?>
                                <span class="badge <?= $statusBadge ?>"><?= ucfirst($p['pray_status']) ?></span>
                            </td>
                            <td class="text-nowrap">
                                <button class="btn btn-sm btn-outline-info" data-bs-toggle="modal" data-bs-target="#prayerModal<?= $p['pray_id'] ?>">
                                    <i class="fa-solid fa-eye"></i>
                                </button>
                                <a href="<?= SystemURLs::getRootPath() ?>/admin/website/prayers/delete/<?= $p['pray_id'] ?>"
                                   class="btn btn-sm btn-outline-danger"
                                   onclick="return confirm('<?= gettext('Delete this prayer request?') ?>')">
                                    <i class="fa-solid fa-trash"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php foreach ($prayers as $p): ?>
<div class="modal fade" id="prayerModal<?= $p['pray_id'] ?>" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><?= gettext('Prayer Request') ?></h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?= SystemURLs::getRootPath() ?>/admin/website/prayers/status/<?= $p['pray_id'] ?>">
                <div class="modal-body">
                    <p><strong><?= gettext('From:') ?></strong> <?= $p['pray_name'] ?: gettext('Anonymous') ?>
                       <?= $p['pray_email'] ? "($p[pray_email])" : '' ?></p>
                    <p><strong><?= gettext('Request:') ?></strong></p>
                    <p class="p-3 bg-light rounded"><?= nl2br($p['pray_request']) ?></p>
                    <p><strong><?= gettext('Submitted:') ?></strong> <?= date('F j, Y g:i A', strtotime($p['pray_created_at'])) ?></p>

                    <div class="mb-3">
                        <label class="form-label"><?= gettext('Status') ?></label>
                            <select name="status" class="form-control">
                                <option value="pending" <?= $p['pray_status'] === 'pending' ? 'selected' : '' ?>><?= gettext('Pending') ?></option>
                                <option value="approved" <?= $p['pray_status'] === 'approved' ? 'selected' : '' ?>><?= gettext('Approved') ?></option>
                                <option value="answered" <?= $p['pray_status'] === 'answered' ? 'selected' : '' ?>><?= gettext('Testimony') ?></option>
                            </select>
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><?= gettext('Testimony Notes (shown publicly)') ?></label>
                        <textarea name="answer_notes" class="form-control" rows="3"><?= $p['pray_answer_notes'] ?? '' ?></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary"><?= gettext('Update Status') ?></button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?= gettext('Close') ?></button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php endforeach; ?>

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