<?php $this->start('body'); ?>
<?php $e = $this->data['e']; ?>
<?php $bg = ['posted'=>'success','voided'=>'dark','je_failed'=>'danger','pending'=>'warning'][$e['status']] ?? 'secondary'; ?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <div>
    <h5 class="mb-0 fw-semibold"><i class="fas fa-gas-pump me-2 text-muted"></i><?= htmlspecialchars($e['entry_no']); ?>
      <span class="badge bg-<?= $bg; ?> ms-2" style="font-size:.65em;vertical-align:middle;text-transform:capitalize;"><?= htmlspecialchars(str_replace('_',' ',$e['status'])); ?></span>
    </h5>
    <div class="text-muted small">
      <?= humanDate($e['entry_date']); ?><?php if ($e['entry_time']): ?> · <?= substr($e['entry_time'],0,5); ?><?php endif; ?>
      · <a href="<?= PROOT; ?>Trucks/view/<?= (int)$e['truck_id']; ?>"><?= htmlspecialchars($e['truck_reg']); ?></a>
      <?php if ($e['driver_name']): ?> · <a href="<?= PROOT; ?>Drivers/view/<?= (int)$e['driver_id']; ?>"><?= htmlspecialchars($e['driver_name']); ?></a><?php endif; ?>
      <?php if ($e['trip_no']):     ?> · <a href="<?= PROOT; ?>Trips/view/<?= (int)$e['trip_id']; ?>"><?= htmlspecialchars($e['trip_no']); ?></a><?php endif; ?>
    </div>
  </div>
  <a href="<?= PROOT; ?>FuelEntries" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<?= flashHtml(); ?>

<?php if ($e['status'] === 'voided'): ?>
  <div class="alert alert-warning py-2 small">
    <i class="fas fa-ban me-1"></i>
    <strong>Voided</strong> on <?= humanDateTime($e['voided_at']); ?>
    <?php if ($e['void_reason']): ?> · <?= htmlspecialchars($e['void_reason']); ?><?php endif; ?>
    <?php if ($e['void_je_no']): ?> · reversing JE <a href="<?= PROOT; ?>Journals/view/<?= (int)$e['void_je_id']; ?>" class="font-monospace"><?= htmlspecialchars($e['void_je_no']); ?></a><?php endif; ?>
  </div>
<?php endif; ?>

<?php if ($e['status'] === 'je_failed'): ?>
  <div class="alert alert-danger py-2 small">
    <i class="fas fa-exclamation-triangle me-1"></i>
    <strong>JE post failed</strong> — the entry is saved but not in the ledger. Check the notes below and fix the underlying issue (usually a missing GL account).
  </div>
<?php endif; ?>

<div class="row g-3">
  <!-- ── Refuel details + payment ── -->
  <div class="col-lg-7">
    <div class="card border-0 shadow-sm">
      <div class="card-body">
        <h6 class="text-uppercase text-muted small mb-3">Refuel Details</h6>
        <dl class="row mb-0">
          <dt class="col-5 text-muted fw-normal">Station</dt>       <dd class="col-7"><?= htmlspecialchars($e['station_name'] ?: '—'); ?><?php if ($e['station_location']): ?> <span class="text-muted small">· <?= htmlspecialchars($e['station_location']); ?></span><?php endif; ?></dd>
          <dt class="col-5 text-muted fw-normal">Fuel type</dt>     <dd class="col-7" style="text-transform:capitalize;"><?= htmlspecialchars($e['fuel_type']); ?></dd>
          <dt class="col-5 text-muted fw-normal">Litres</dt>        <dd class="col-7"><strong><?= number_format((float)$e['liters'], 2); ?></strong> L</dd>
          <dt class="col-5 text-muted fw-normal">Price per litre</dt><dd class="col-7"><?= htmlspecialchars($e['currency']); ?> <?= number_format((float)$e['price_per_liter'], 2); ?></dd>
          <dt class="col-5 text-muted fw-normal">Total amount</dt>  <dd class="col-7 fw-semibold"><?= htmlspecialchars($e['currency']); ?> <?= number_format((float)$e['gross_amount'], 2, '.', ','); ?></dd>
          <?php if ($e['odometer_reading']): ?>
            <dt class="col-5 text-muted fw-normal">Odometer</dt><dd class="col-7"><?= number_format((int)$e['odometer_reading']); ?> km</dd>
          <?php endif; ?>
          <?php if ($e['fuel_tank_liters'] && (float)$e['liters'] > 0): ?>
            <?php $pct = round((float)$e['liters'] / (int)$e['fuel_tank_liters'] * 100, 0); ?>
            <dt class="col-5 text-muted fw-normal">Tank fill</dt><dd class="col-7"><?= $pct; ?>% of <?= number_format((int)$e['fuel_tank_liters']); ?> L capacity</dd>
          <?php endif; ?>

          <hr class="my-2">
          <dt class="col-5 text-muted fw-normal">Payment method</dt> <dd class="col-7"><?= htmlspecialchars(ucwords(str_replace('_',' ',$e['payment_method']))); ?></dd>
          <?php if ($e['payment_reference']): ?><dt class="col-5 text-muted fw-normal">Payment ref</dt><dd class="col-7 font-monospace small"><?= htmlspecialchars($e['payment_reference']); ?></dd><?php endif; ?>
          <?php if ($e['receipt_no']):        ?><dt class="col-5 text-muted fw-normal">Receipt #</dt>  <dd class="col-7 font-monospace small"><?= htmlspecialchars($e['receipt_no']); ?></dd><?php endif; ?>
          <?php if ($e['receipt_url']):       ?><dt class="col-5 text-muted fw-normal">Receipt</dt>    <dd class="col-7"><a href="<?= htmlspecialchars($e['receipt_url']); ?>" target="_blank"><i class="fas fa-external-link-alt me-1"></i>View</a></dd><?php endif; ?>

          <?php if ($e['je_no']): ?>
            <hr class="my-2">
            <dt class="col-5 text-muted fw-normal">Fuel JE</dt><dd class="col-7"><a href="<?= PROOT; ?>Journals/view/<?= (int)$e['je_id']; ?>" class="font-monospace"><?= htmlspecialchars($e['je_no']); ?></a></dd>
          <?php endif; ?>
          <?php if ($e['notes']): ?>
            <hr class="my-2">
            <dt class="col-5 text-muted fw-normal">Notes</dt><dd class="col-7"><?= nl2br(htmlspecialchars($e['notes'])); ?></dd>
          <?php endif; ?>
        </dl>
      </div>
    </div>
  </div>

  <!-- ── Sensor reconciliation ── -->
  <div class="col-lg-5">
    <div class="card border-0 shadow-sm">
      <div class="card-body">
        <h6 class="text-uppercase text-muted small mb-3"><i class="fas fa-satellite-dish me-1"></i>Sensor Reconciliation</h6>
        <?php if ($e['sensor_event_id']): ?>
          <dl class="row mb-0 small">
            <dt class="col-6 text-muted fw-normal">Sensor fill</dt><dd class="col-6"><?= number_format((float)$e['sensor_fill_liters'], 2); ?> L</dd>
            <dt class="col-6 text-muted fw-normal">Recorded fill</dt><dd class="col-6"><?= number_format((float)$e['liters'], 2); ?> L</dd>
            <dt class="col-6 text-muted fw-normal">Variance</dt>
            <dd class="col-6 <?= abs((float)$e['sensor_variance_l']) > 5 ? 'text-danger fw-semibold' : 'text-success'; ?>">
              <?= number_format((float)$e['sensor_variance_l'], 2); ?> L
            </dd>
            <dt class="col-6 text-muted fw-normal">Status</dt><dd class="col-6" style="text-transform:capitalize;"><?= htmlspecialchars($e['variance_status']); ?></dd>
          </dl>
        <?php else: ?>
          <p class="text-muted small mb-2 fst-italic">
            <i class="fas fa-hourglass-half me-1"></i>
            No sensor event matched yet. Fuel sensor ingestion goes live in Iter 3 — once GPS &amp; fuel-probe data flows in, the Fuel Officer console will auto-match sensor fill events to this entry and flag any variance.
          </p>
          <span class="badge bg-light text-muted border">Status: <?= htmlspecialchars($e['variance_status']); ?></span>
        <?php endif; ?>
      </div>
    </div>

    <?php if ($e['status'] === 'posted'): ?>
      <div class="card border-0 shadow-sm mt-3">
        <div class="card-body">
          <h6 class="text-uppercase text-muted small mb-3"><i class="fas fa-ban me-1"></i>Void this entry</h6>
          <p class="small text-muted mb-2">Posts a reversing JE. The original JE stays in the ledger for audit.</p>
          <button type="button" class="btn btn-sm btn-outline-danger w-100" onclick="doVoid()"><i class="fas fa-times me-1"></i>Void &amp; reverse JE</button>
        </div>
      </div>
    <?php endif; ?>
  </div>
</div>

<form id="voidForm" method="POST" action="<?= PROOT; ?>FuelEntries/void/<?= (int)$e['c0d']; ?>" class="d-none">
  <?= csrf_field(); ?>
  <input type="hidden" name="void_reason" id="vReason">
</form>

<?php $this->end(); ?>
<?php $this->start('section_js'); ?>
<script>
function doVoid() {
    $.confirm({
        title: 'Void this fuel entry?',
        content: '<label class="form-label small">Reason <span class="text-danger">*</span></label><textarea id="voidReason" class="form-control form-control-sm" rows="3" placeholder="e.g. Wrong amount, duplicate entry, etc."></textarea>',
        type: 'red',
        buttons: {
            void: { text: 'Void &amp; reverse JE', btnClass: 'btn-danger', action: function () {
                const r = document.getElementById('voidReason').value.trim();
                if (!r) { $.alert('Please enter a reason.'); return false; }
                document.getElementById('vReason').value = r;
                document.getElementById('voidForm').submit();
            }},
            back: function () {}
        }
    });
}
</script>
<?php $this->end(); ?>
