<?php

require_once __DIR__ . '/Include/Config.php';
require_once __DIR__ . '/Include/PageInit.php';

use ChurchCRM\Authentication\AuthenticationManager;
use ChurchCRM\model\ChurchCRM\PledgeQuery;
use ChurchCRM\Utils\InputUtils;
use ChurchCRM\Utils\RedirectUtils;
use ChurchCRM\view\PageHeader;

$sPageTitle = gettext('Electronic Transaction Details');
$sPageSubtitle = gettext('View transaction details for a pledge payment');

// Get the PledgeID out of the querystring
$iPledgeID = InputUtils::legacyFilterInput($_GET['PledgeID'], 'int');
$linkBack = RedirectUtils::getLinkBackFromRequest('v2/dashboard');

// Security: User must have Finance permission to use this form.
// Clean error handling: (such as somebody typing an incorrect URL ?PersonID= manually)
AuthenticationManager::redirectHomeIfFalse(AuthenticationManager::getCurrentUser()->isFinanceEnabled(), 'Finance');

// Is this the second pass?
if (isset($_POST['Back'])) {
    RedirectUtils::redirect($linkBack);
}

$pledge = PledgeQuery::create()->findPk((int) $iPledgeID);
if ($pledge === null) {
    RedirectUtils::redirect($linkBack);
}

$rsResultRec = RunPreparedQuery('SELECT * FROM result_res WHERE res_ID = ?', 'i', [(int) $pledge->getAutResultId()]);

$aBreadcrumbs = PageHeader::breadcrumbs([
    [gettext('Finance'), '/finance/'],
    [gettext('Transaction Details')],
]);
require_once __DIR__ . '/Include/Header.php';

$resArr = mysqli_fetch_array($rsResultRec);
if ($resArr) {
    extract($resArr);
    echo $res_echotype2;
}

?>

<div class="card">
  <div class="card-body">
    <form method="post" action="PledgeDetails.php?<?= 'PledgeID=' . $iPledgeID . '&linkBack=' . InputUtils::escapeAttribute($linkBack) ?>" name="PledgeDelete">
      <input type="submit" class="btn btn-secondary" value="<?= gettext('Back') ?>" name="Back">
    </form>
  </div>
</div>
<?php
require_once __DIR__ . '/Include/Footer.php';
