<?php
// vars: $cashboxes, $form, $errors, $success
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">حركة خزنة</h4>
  <a class="btn btn-outline-secondary" href="<?= e(url($cashboxes_url ?? '/finance/cashboxes')) ?>">الخزن</a>
</div>

<?php if ($success): ?>
  <div class="alert alert-success"><?= e($success) ?></div>
<?php endif; ?>

<?php if (!empty($errors)): ?>
  <div class="alert alert-danger">
    <ul class="mb-0">
      <?php foreach ($errors as $er): ?><li><?= e($er) ?></li><?php endforeach; ?>
    </ul>
  </div>
<?php endif; ?>

<form class="card p-3" method="post">
  <?= csrf_field() ?>

  <div class="row g-3">
    <div class="col-md-4">
      <label class="form-label">الخزنة</label>
      <select class="form-select" name="cashbox_id" required>
        <?php foreach ($cashboxes as $cb): ?>
          <option value="<?= (int)$cb['id'] ?>" <?= ((int)$form['cashbox_id']===(int)$cb['id']?'selected':'') ?>><?= e($cb['name']) ?></option>
        <?php endforeach; ?>
      </select>
    </div>

    <div class="col-md-3">
      <label class="form-label">التاريخ</label>
      <input type="date" class="form-control" name="trx_date" value="<?= e($form['trx_date']) ?>" required>
    </div>

    <div class="col-md-2">
      <label class="form-label">الوقت</label>
      <input type="time" class="form-control" name="trx_time" value="<?= e($form['trx_time']) ?>">
    </div>

    <div class="col-md-3">
      <label class="form-label">نوع الحركة</label>
      <select class="form-select" name="trx_type">
        <option value="in" <?= ($form['trx_type']==='in'?'selected':'') ?>>إيداع (داخل)</option>
        <option value="out" <?= ($form['trx_type']==='out'?'selected':'') ?>>صرف (خارج)</option>
      </select>
    </div>

    <div class="col-md-4">
      <label class="form-label">المبلغ</label>
      <input type="number" step="0.01" class="form-control" name="amount" value="<?= e($form['amount']) ?>" required>
    </div>

    <div class="col-md-8">
      <label class="form-label">ملاحظات</label>
      <input type="text" class="form-control" name="notes" value="<?= e($form['notes']) ?>" placeholder="سبب الحركة / تفاصيل...">
    </div>
  </div>

  <div class="mt-3 d-flex gap-2">
    <button class="btn btn-primary" type="submit"><i class="fa-solid fa-check"></i> تسجيل</button>
    <?php if (can('cashboxes.report.view')): ?>
      <a class="btn btn-outline-secondary" href="<?= e((url($cashbox_ledger_url ?? '/finance/cashboxes/ledger')) . '?cashbox_id='.(int)$form['cashbox_id']) ?>">كشف الحركة</a>
    <?php endif; ?>
  </div>
</form>
