<?php $this->start('body'); ?>
<?php $a = $this->data['a']; $active = empty($a['assigned_to']); ?>

<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-exchange-alt me-2 text-muted"></i>Assignment #<?= (int)$a['c0d']; ?>
      <?php if ($active): ?><span class="badge bg-success ms-2" style="font-size:.65em;vertical-align:middle;">Active</span>
      <?php else: ?><span class="badge bg-secondary ms-2" style="font-size:.65em;vertical-align:middle;">Ended</span><?php endif; ?>
    </h5>
    <div class="text-muted small">
      <a href="<?= PROOT; ?>Drivers/view/<?= (int)$a['driver_id']; ?>"><?= htmlspecialchars($a['driver_name'] ?? '?'); ?></a>
      &nbsp;<i class="fas fa-arrow-right"></i>&nbsp;
      <a href="<?= PROOT; ?>Trucks/view/<?= (int)$a['truck_id']; ?>"><?= htmlspecialchars($a['truck_reg'] ?? '?'); ?></a>
    </div>
  </div>
  <a href="<?= PROOT; ?>DriverAssignments" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<?= flashHtml(); ?>

<div class="row g-3">
  <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">Details</h6>
        <dl class="row mb-0">
          <dt class="col-4 text-muted fw-normal">Driver</dt><dd class="col-8"><a href="<?= PROOT; ?>Drivers/view/<?= (int)$a['driver_id']; ?>"><?= htmlspecialchars($a['driver_name'] ?? '?'); ?></a><?php if ($a['employee_no']): ?> <span class="text-muted small">· <?= htmlspecialchars($a['employee_no']); ?></span><?php endif; ?></dd>
          <dt class="col-4 text-muted fw-normal">Phone</dt> <dd class="col-8"><?= htmlspecialchars($a['phone_1'] ?? '—'); ?></dd>
          <dt class="col-4 text-muted fw-normal">Truck</dt> <dd class="col-8"><a href="<?= PROOT; ?>Trucks/view/<?= (int)$a['truck_id']; ?>"><?= htmlspecialchars($a['truck_reg'] ?? '?'); ?></a> <span class="text-muted small"><?= htmlspecialchars(trim(($a['make'] ?? '') . ' ' . ($a['model'] ?? ''))); ?></span></dd>
          <dt class="col-4 text-muted fw-normal">Role</dt>  <dd class="col-8" style="text-transform:capitalize;"><?= htmlspecialchars($a['role_type']); ?></dd>
          <dt class="col-4 text-muted fw-normal">Assigned from</dt><dd class="col-8"><?= humanDate($a['assigned_from']); ?></dd>
          <dt class="col-4 text-muted fw-normal">Assigned to</dt>  <dd class="col-8"><?= $a['assigned_to'] ? humanDate($a['assigned_to']) : '<span class="text-success">Still active</span>'; ?></dd>
          <?php if ($a['reason']):     ?><dt class="col-4 text-muted fw-normal">Reason</dt>    <dd class="col-8"><?= htmlspecialchars($a['reason']); ?></dd><?php endif; ?>
          <?php if ($a['end_reason']): ?><dt class="col-4 text-muted fw-normal">End reason</dt><dd class="col-8"><?= htmlspecialchars($a['end_reason']); ?></dd><?php endif; ?>
          <?php if ($a['notes']):      ?><dt class="col-4 text-muted fw-normal">Notes</dt>     <dd class="col-8"><?= nl2br(htmlspecialchars($a['notes'])); ?></dd><?php endif; ?>
          <dt class="col-4 text-muted fw-normal">Created</dt><dd class="col-8 small text-muted"><?= humanDateTime($a['created_on']); ?></dd>
          <?php if ($a['edited_on']): ?><dt class="col-4 text-muted fw-normal">Last edit</dt><dd class="col-8 small text-muted"><?= humanDateTime($a['edited_on']); ?></dd><?php endif; ?>
        </dl>
      </div>
    </div>
  </div>

  <?php if ($active): ?>
  <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">End this assignment</h6>
        <p class="small text-muted">Ending an assignment records when the driver stopped operating this truck. Assignment history is preserved.</p>
        <form method="POST" action="<?= PROOT; ?>DriverAssignments/end/<?= (int)$a['c0d']; ?>" id="endForm">
          <?= csrf_field(); ?>
          <div class="mb-2">
            <label class="form-label small mb-1">End date</label>
            <input type="date" name="assigned_to" class="form-control form-control-sm" value="<?= date('Y-m-d'); ?>" required>
          </div>
          <div class="mb-2">
            <label class="form-label small mb-1">Reason</label>
            <select name="end_reason" class="form-select form-select-sm">
              <option value="Reassignment">Reassignment</option>
              <option value="Leave">Leave</option>
              <option value="Resigned">Resigned</option>
              <option value="Suspended">Suspended</option>
              <option value="Terminated">Terminated</option>
              <option value="Transferred">Transferred</option>
              <option value="Other">Other</option>
            </select>
          </div>
          <button type="button" class="btn btn-sm btn-warning w-100" id="btnEnd"><i class="fas fa-stop-circle me-1"></i>End assignment</button>
        </form>
      </div>
    </div>
  </div>
  <?php endif; ?>
</div>

<form id="deleteForm" method="POST" action="<?= PROOT; ?>DriverAssignments/delete/<?= (int)$a['c0d']; ?>" class="d-none">
  <?= csrf_field(); ?>
</form>

<div class="text-end mt-3">
  <button type="button" class="btn btn-sm btn-outline-danger" id="btnDelete"><i class="fas fa-trash me-1"></i>Delete record</button>
</div>

<?php $this->end(); ?>
<?php $this->start('section_js'); ?>
<script>
document.getElementById('btnDelete')?.addEventListener('click', function () {
  $.confirm({
    title: 'Delete assignment record?',
    content: 'This soft-deletes the assignment history entry entirely. Prefer ending the assignment instead — end preserves history.',
    type: 'red',
    buttons: {
      remove: { text: 'Delete', btnClass: 'btn-danger', action: () => document.getElementById('deleteForm').submit() },
      cancel: function () {}
    }
  });
});
document.getElementById('btnEnd')?.addEventListener('click', function () {
  $.confirm({
    title: 'End this assignment?',
    content: 'The driver will be marked as no longer operating this truck from the end date.',
    type: 'orange',
    buttons: {
      end: { text: 'End assignment', btnClass: 'btn-warning', action: () => document.getElementById('endForm').submit() },
      cancel: function () {}
    }
  });
});
</script>
<?php $this->end(); ?>
