<?php

use ChurchCRM\dto\SystemURLs;

$sPageTitle = 'Prayer - ' . $churchName;
require SystemURLs::getDocumentRoot() . '/Include/HeaderNotLoggedIn.php';
$hidePageHeader = true;
require 'header.php';
?>

<div class="page-header">
  <div class="container animate-up">
    <h1><i class="fa-solid fa-hands-praying me-2"></i>Prayer</h1>
    <p class="mb-0 opacity-75">Share your prayer requests with our community</p>
  </div>
</div>

<div class="container py-5">
  <div class="row">
    <div class="col-lg-6 mb-5 mb-lg-0 animate-on-scroll">
      <div class="card shadow-sm border-0">
        <div class="card-header bg-white py-3">
          <h4 class="mb-0 fw-bold"><i class="fa-solid fa-pray me-2" style="color:#6f42c1;"></i>Submit a Prayer Request</h4>
        </div>
        <div class="card-body">
          <?php if ($success): ?>
            <div class="alert alert-success d-flex align-items-center">
              <i class="fa-solid fa-check-circle me-2 fa-lg"></i>
              <div>Thank you! Your prayer request has been submitted. Our team will pray for you.</div>
            </div>
          <?php endif; ?>
          <?php if (!empty($error)): ?>
            <div class="alert alert-danger d-flex align-items-center">
              <i class="fa-solid fa-circle-exclamation me-2 fa-lg"></i>
              <div><?= $error ?></div>
            </div>
          <?php endif; ?>

          <form method="POST" action="<?= $rootPath ?>/external/prayer">
            <div class="mb-3">
              <label class="form-label fw-medium">Your Name <span class="text-secondary fw-normal">(optional)</span></label>
              <input type="text" name="name" class="form-control" placeholder="Enter your name">
            </div>
            <div class="mb-3">
              <label class="form-label fw-medium">Email <span class="text-secondary fw-normal">(optional)</span></label>
              <input type="email" name="email" class="form-control" placeholder="Enter your email">
            </div>
            <div class="mb-3">
              <label class="form-label fw-medium">Prayer Request <span class="text-danger">*</span></label>
              <textarea name="request" class="form-control" rows="5" placeholder="Share your prayer request..." required></textarea>
            </div>
            <button type="submit" class="btn btn-primary w-100 py-2">
              <i class="fa-solid fa-paper-plane me-2"></i>Submit Prayer Request
            </button>
          </form>
        </div>
      </div>
    </div>

    <div class="col-lg-6 animate-on-scroll">
      <h4 class="fw-bold mb-4"><i class="fa-solid fa-check-circle me-2 text-success"></i>Answered Prayers</h4>
      <?php if (empty($prayers)): ?>
        <div class="text-center py-5">
          <i class="fa-solid fa-hands-praying fa-4x text-secondary mb-3" style="opacity:.4;"></i>
          <p class="text-secondary">No answered prayers posted yet.</p>
        </div>
      <?php else: ?>
        <?php foreach ($prayers as $p): ?>
          <div class="card prayer-card shadow-sm">
            <div class="card-body">
              <p class="card-text"><em>"<?= nl2br(substr($p['pray_request'], 0, 300)) ?>"</em></p>
              <?php if ($p['pray_answer_notes']): ?>
                <div class="mt-2 p-3 bg-success bg-opacity-10 rounded">
                  <strong class="text-success"><i class="fa-solid fa-check-circle me-1"></i>Answered:</strong> <?= nl2br($p['pray_answer_notes']) ?>
                </div>
              <?php endif; ?>
              <small class="text-secondary d-block mt-2"><i class="fa-regular fa-clock me-1"></i>Submitted <?= date('M j, Y', strtotime($p['pray_created_at'])) ?></small>
            </div>
          </div>
        <?php endforeach; ?>
      <?php endif; ?>
    </div>
  </div>
</div>

<?php require 'footer.php'; ?>
<?php require SystemURLs::getDocumentRoot() . '/Include/FooterNotLoggedIn.php'; ?>
