73 lines
3.6 KiB
PHP
73 lines
3.6 KiB
PHP
<?= $this->extend('layout/main') ?>
|
|
|
|
<?= $this->section('content') ?>
|
|
<h1 class="h4 mb-3"><?= esc($title) ?></h1>
|
|
|
|
<div class="card shadow-sm">
|
|
<div class="card-body">
|
|
<?php $action = $op ? site_url('operations/' . $op['id']) : site_url('operations'); ?>
|
|
<?= form_open($action, ['class' => 'row g-3']) ?>
|
|
<div class="col-md-4">
|
|
<label class="form-label">Data</label>
|
|
<input type="date" name="date" class="form-control" required
|
|
value="<?= esc(old('date', $op['date'] ?? date('Y-m-d'))) ?>">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">Kwota (zł)</label>
|
|
<input type="number" step="0.01" min="0.01" name="amount" class="form-control" required
|
|
value="<?= esc(old('amount', $op['amount'] ?? '')) ?>">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">Typ</label>
|
|
<?php $t = old('type', $op['type'] ?? 'expense'); ?>
|
|
<select name="type" id="type" class="form-select" required>
|
|
<option value="income" <?= $t === 'income' ? 'selected' : '' ?>>Przychód</option>
|
|
<option value="expense" <?= $t === 'expense' ? 'selected' : '' ?>>Wydatek</option>
|
|
</select>
|
|
<div class="form-text">Wybór kategorii ustawia typ automatycznie.</div>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">Kategoria</label>
|
|
<?php $selCat = old('category_id', $op['category_id'] ?? ''); ?>
|
|
<select name="category_id" id="category_id" class="form-select">
|
|
<option value="" data-type="">— bez kategorii —</option>
|
|
<?php foreach ($categories as $c): ?>
|
|
<option value="<?= $c['id'] ?>" data-type="<?= $c['type'] ?>"
|
|
<?= (string) $selCat === (string) $c['id'] ? 'selected' : '' ?>>
|
|
<?= esc($c['indent_label']) ?> (<?= $c['type'] === 'income' ? 'przychód' : 'wydatek' ?>)
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">Opis (opcjonalnie)</label>
|
|
<input type="text" name="description" class="form-control" maxlength="255"
|
|
value="<?= esc(old('description', $op['description'] ?? '')) ?>">
|
|
</div>
|
|
<div class="col-12">
|
|
<button type="submit" class="btn btn-primary">Zapisz</button>
|
|
<a href="<?= site_url('operations') ?>" class="btn btn-link">Anuluj</a>
|
|
</div>
|
|
<?= form_close() ?>
|
|
</div>
|
|
</div>
|
|
<?= $this->endSection() ?>
|
|
|
|
<?= $this->section('scripts') ?>
|
|
<script>
|
|
// Kategoria narzuca typ operacji (spojnosc ze zrodlem prawdy po stronie serwera).
|
|
const cat = document.getElementById('category_id');
|
|
const type = document.getElementById('type');
|
|
function syncType() {
|
|
const opt = cat.options[cat.selectedIndex];
|
|
const t = opt.getAttribute('data-type');
|
|
if (t) { type.value = t; type.setAttribute('disabled', 'disabled'); }
|
|
else { type.removeAttribute('disabled'); }
|
|
}
|
|
cat.addEventListener('change', syncType);
|
|
syncType();
|
|
// disabled select nie wysyla wartosci -> odblokuj przy submit (serwer i tak wymusi typ z kategorii)
|
|
type.form.addEventListener('submit', () => type.removeAttribute('disabled'));
|
|
</script>
|
|
<?= $this->endSection() ?>
|