<?php

global $account;

Beneficiary::LoadAll();
Context::LoadAll();
?><!DOCTYPE html5><html><body><form method="post" action="#">
<div class="container"><div class="row">
    <div class="form-group"><label><span>label</span><input type="text" name="label" value="<?php echo $template ? $template->getLabel() : ""; ?>"/></label></div>
    <div class="form-group"><label><span>context</span><select name="context[]" multiple value="<?php echo $template ? Context::Serialize($template->getContext()) : ""; ?>"><?php
    $contexts = Context::ListAll();
usort($contexts, function($a, $b) { return strcasecmp($a->getLabel(), $b->getLabel());});
foreach ($contexts as $ctx) {
    $selected = "";
    if ($template) {
        foreach ($template->getContext() as $templateCtx) {
            if ($templateCtx->getId() === $ctx->getId()) {
                $selected = "selected";
                break;
            }
        }
    }
    echo '<option value="' .$ctx->getId() .'" ' .$selected .'>' .$ctx->getLabel() ."</option>";
}
?></select></label></div>
    <div class="form-group"><label><span>beneficiary</span><select name="beneficiary" value="<?php echo $template ? $template->getBeneficiary()->getId() : ""; ?>"><?php
$beneficiaries = Beneficiary::ListAll();
usort($beneficiaries, function($a, $b) {return strcasecmp($a->getLabel(), $b->getLabel());});
foreach ($beneficiaries as $beneficiary) { ?>
    <option value="<?php echo $beneficiary->getId(); ?>"<?php echo (($template && $template->getBeneficiary()->getId() == $beneficiary->getId()) ? " selected" : ""); ?>><?php echo $beneficiary->getLabel(); ?></option><?php } ?></select></label></div>
    <div class="form-group"><label><span>date</span><input type="date" name="paymentDate" value="<?php echo $template ? $template->getPaymentDate()->format("Y-m-d") : ""; ?>"/></label></div>
    <div class="form-group"><label><span>amount</span><input type="number" name="amount" step="0.01" min="0" value="<?php echo $template ? $template->getAmount() : ""; ?>"/></label></div>
    <div class="form-group"><input type="submit" value="Add"/></div>
</div></div>
</form>
</html>
