<?php $this->start('body'); ?>
<?php $f = $this->data['form']; $tpl = $this->data['tpl']; $isNew = $this->data['is_new']; ?>
<?php $action = $isNew ? PROOT . 'PmTemplates/save' : PROOT . 'PmTemplates/update/' . (int)$tpl['c0d']; ?>

<div class="d-flex align-items-center justify-content-between mb-3">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-<?= $isNew?'plus':'pen'; ?> me-2 text-muted"></i><?= $isNew ? 'New PM Template' : 'Edit · ' . htmlspecialchars($tpl['name']); ?></h5>
  <a href="<?= PROOT; ?>PmTemplates" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<?= $this->raw('fb_msg'); ?>

<form method="POST" action="<?= $action; ?>" class="card border-0 shadow-sm" autocomplete="off">
  <?= csrf_field(); ?>
  <div class="card-body">
    <div class="row g-3">
      <div class="col-md-6"><label class="form-label">Name <span class="text-danger">*</span></label>
        <input type="text" name="name" class="form-control" required value="<?= htmlspecialchars($f['name'] ?? ''); ?>" placeholder="e.g. Engine oil &amp; filter change"></div>
      <div class="col-md-3"><label class="form-label">Kind</label>
        <select name="kind" class="form-select">
          <?php foreach (['oil_change'=>'Oil change','oil_filter'=>'Oil filter','fuel_filter'=>'Fuel filter','air_filter'=>'Air filter','transmission_oil'=>'Transmission oil','diff_oil'=>'Differential oil','brake_inspection'=>'Brake inspection','brake_fluid'=>'Brake fluid','tyre_rotation'=>'Tyre rotation','tyre_inspection'=>'Tyre inspection','coolant'=>'Coolant / radiator','battery'=>'Battery','alignment'=>'Wheel alignment','general_service'=>'General service','greasing'=>'Greasing','other'=>'Other'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= ($f['kind'] ?? '')===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-3"><label class="form-label">Priority</label>
        <select name="priority" class="form-select">
          <?php foreach (['low','normal','high','critical'] as $p): ?>
            <option value="<?= $p; ?>" <?= ($f['priority'] ?? 'normal')===$p?'selected':''; ?>><?= ucfirst($p); ?></option>
          <?php endforeach; ?>
        </select></div>

      <div class="col-md-3"><label class="form-label">Interval (km)</label>
        <input type="number" min="0" name="interval_km" class="form-control" value="<?= htmlspecialchars((string)($f['interval_km'] ?? '')); ?>" placeholder="e.g. 10000"></div>
      <div class="col-md-3"><label class="form-label">Interval (days)</label>
        <input type="number" min="0" name="interval_days" class="form-control" value="<?= htmlspecialchars((string)($f['interval_days'] ?? '')); ?>" placeholder="e.g. 90"></div>
      <div class="col-md-3"><label class="form-label">Warn ahead (km)</label>
        <input type="number" min="0" name="threshold_km" class="form-control" value="<?= htmlspecialchars((string)($f['threshold_km'] ?? 500)); ?>"></div>
      <div class="col-md-3"><label class="form-label">Warn ahead (days)</label>
        <input type="number" min="0" name="threshold_days" class="form-control" value="<?= htmlspecialchars((string)($f['threshold_days'] ?? 7)); ?>"></div>

      <div class="col-md-3"><label class="form-label">Est. labour (hours)</label>
        <input type="number" step="0.1" min="0" name="default_labour_hours" class="form-control" value="<?= htmlspecialchars((string)($f['default_labour_hours'] ?? '')); ?>"></div>
      <div class="col-md-3"><label class="form-label">Est. parts cost (TZS)</label>
        <input type="number" step="0.01" min="0" name="default_parts_cost" class="form-control" value="<?= htmlspecialchars((string)($f['default_parts_cost'] ?? '')); ?>"></div>
      <div class="col-md-3 d-flex align-items-end">
        <div class="form-check">
          <input type="checkbox" name="active" value="1" class="form-check-input" id="chkAc" <?= (!isset($f['active']) || !empty($f['active']))?'checked':''; ?>>
          <label class="form-check-label" for="chkAc">Active</label>
        </div></div>

      <div class="col-12"><label class="form-label">Notes</label>
        <textarea name="notes" rows="2" class="form-control" placeholder="Mechanic hints, TZ-specific notes, part numbers..."><?= htmlspecialchars($f['notes'] ?? ''); ?></textarea></div>
    </div>
  </div>
  <div class="card-footer bg-white d-flex justify-content-end gap-2 py-3">
    <a href="<?= PROOT; ?>PmTemplates" class="btn btn-outline-secondary">Cancel</a>
    <button class="btn btn-primary"><i class="fas fa-save me-1"></i><?= $isNew?'Create template':'Save changes'; ?></button>
  </div>
</form>

<?php $this->end(); ?>
