114 lines
5.7 KiB
PHP

<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use MyPrayerJournal\Auth;
use MyPrayerJournal\Domain\{RecurrencePeriod, Request, RequestAction};
use MyPrayerJournal\UI\{Component, Layout};
require '../../start.php';
if ($_SERVER['REQUEST_METHOD'] <> 'GET') not_found();
Auth::requireUser();
$isNew = $_GET['id'] == 'new';
$req = match ($isNew) {
true => new Request('new'),
false => Request::byId($_GET['id'])->getOrCall(not_found(...))
};
$cancelLink = match (true) {
str_ends_with($_SERVER['HTTP_REFERER'] ?? '', 'active') => '/requests/active',
str_ends_with($_SERVER['HTTP_REFERER'] ?? '', 'snoozed') => '/requests/snoozed',
default => '/journal'
};
$action = $_GET['id'] == 'new' ? 'Add' : 'Edit';
Layout::pageHead("$action Prayer Request");?>
<article class=container>
<h2 class=pb-3><?=$action?> Prayer Request</h2>
<form <?=$isNew ? 'hx-post' : 'hx-patch'?>=/request/save hx-target=#top hx-push-url=true>
<input type=hidden name=requestId value=<?=$req->id?>>
<input type=hidden name=returnTo value=<?=$cancelLink?>>
<div class="form-floating pb-3">
<textarea id=requestText name=requestText class=form-control style="min-height: 8rem;"
placeholder="Enter the text of the request" autofocus required><?=$req->currentText?></textarea>
<label for=requestText>Prayer Request</label>
</div><br><?php
if (!$isNew) { ?>
<div class=pb-3>
<label>Also Mark As</label><br>
<div class="form-check form-check-inline">
<input type=radio class=form-check-input id=sU name=status value=<?=RequestAction::Updated->value?>
checked>
<label for=sU>Updated</label>
</div>
<div class="form-check form-check-inline">
<input type=radio class=form-check-input id=sP name=status value=<?=RequestAction::Prayed->value?>>
<label for=sP>Prayed</label>
</div>
<div class="form-check form-check-inline">
<input type=radio class=form-check-input id=sA name=status
value=<?=RequestAction::Answered->value?>>
<label for=sA>Answered</label>
</div>
</div><?php
} ?>
<div class=row">
<div class="col-12 offset-md-2 col-md-8 offset-lg-3 col-lg-6">
<p><strong>Recurrence &nbsp;</strong> <em class=text-muted>After prayer, request reappears&hellip;</em>
<div class="d-flex flex-row flex-wrap justify-content-center align-items-center">
<div class="form-check mx-2">
<input type=radio class=form-check-input id=rI name=recurType
value=<?=RecurrencePeriod::Immediate->value?>
onclick="mpj.edit.toggleRecurrence(event)"<?php
if ($req->recurrence->period == RecurrencePeriod::Immediate) echo ' checked'; ?>>
<label for=rI>Immediately</label>
</div>
<div class="form-check mx-2">
<input type=radio class=form-check-input id=rO name=recurType value=Other
onclick="mpj.edit.toggleRecurrence(event)"<?php
if ($req->recurrence->period <> RecurrencePeriod::Immediate) echo ' checked'; ?>>
<label for=rO>Every&hellip;</label>
</div>
<div class="form-floating mx-2">
<input type=number class=form-control id=recurCount name=recurCount placeholder=0 required
value=<?=$req->recurrence->interval->getOrDefault(0)?> style="width:6rem;"<?php
if ($req->recurrence->period === RecurrencePeriod::Immediate) echo ' disabled'; ?>>
<label for=recurCount>Count</label>
</div>
<div class="form-floating mx-2">
<select class=form-control id=recurInterval name=recurInterval style="width:6rem;" required<?php
if ($req->recurrence->period === RecurrencePeriod::Immediate) echo ' disabled'; ?>>
<option value=<?=RecurrencePeriod::Hours->value?><?php
if ($req->recurrence->period === RecurrencePeriod::Hours) echo ' selected'; ?>>
hours
</option>
<option value=<?=RecurrencePeriod::Days->value?><?php
if ($req->recurrence->period === RecurrencePeriod::Days) echo ' selected'; ?>>
days
</option>
<option value=<?=RecurrencePeriod::Weeks->value?><?php
if ($req->recurrence->period === RecurrencePeriod::Weeks) echo ' selected'; ?>>
weeks
</option>
</select>
<label for=recurInterval>Interval</label>
</div>
</div>
</div>
</div>
<div class="text-end pt-3">
<button class="btn btn-primary me-2" type=submit><?=Component::icon('save');?> Save</button>
<?=Component::pageLink($cancelLink, Component::icon('arrow_back') . ' Cancel',
['class' => 'btn btn-secondary ms-2'])?>
</div>
</form>
</article><?php
Layout::pageFoot();