<?php
$targetByPriority = [];
foreach ($targets as $t) { $targetByPriority[$t['priority_id']] = $t; }
?>
<div class="card" style="max-width:920px">
  <div class="card-header">
    <h3>Policy</h3>
    <?php if ($policy) : ?><span class="badge badge-outline">Version <?= $policy['version'] ?></span><?php endif; ?>
  </div>
  <div class="card-body">
    <form method="post" action="<?= $policy ? url('/admin/sla/' . $policy['id']) : url('/admin/sla') ?>">
      <?= csrf_field() ?>
      <div class="form-row">
        <div class="form-group"><label>Name <span class="req">*</span></label><input type="text" name="name" required value="<?= e($policy['name'] ?? '') ?>"></div>
        <div class="form-group">
          <label>Contract tier</label>
          <select name="contract_tier" <?= $policy ? 'disabled' : '' ?>>
            <?php foreach (['Standard','Gold','Platinum'] as $tier) : ?><option <?= ($policy['contract_tier'] ?? '')===$tier?'selected':'' ?>><?= $tier ?></option><?php endforeach; ?>
          </select>
        </div>
      </div>
      <div class="form-row">
        <div class="form-group">
          <label>Business calendar</label>
          <select name="calendar_id"><?php foreach ($calendars as $c) : ?><option value="<?= $c['id'] ?>" <?= ($policy['calendar_id'] ?? null)==$c['id']?'selected':'' ?>><?= e($c['name']) ?></option><?php endforeach; ?></select>
        </div>
        <div class="form-group"><label>Effective from</label><input type="date" name="effective_from" value="<?= e($policy['effective_from'] ?? date('Y-m-d')) ?>" <?= $policy ? 'disabled' : '' ?>></div>
      </div>
      <div class="flex gap-3">
        <label class="checkbox-row"><input type="checkbox" name="is_24x7" value="1" <?= !empty($policy['is_24x7'])?'checked':'' ?>> 24×7 (ignores business calendar)</label>
        <?php if ($policy) : ?><label class="checkbox-row"><input type="checkbox" name="is_active" value="1" <?= !empty($policy['is_active'])?'checked':'' ?>> Active</label><?php endif; ?>
      </div>

      <h3 class="mt-3">Response &amp; resolution targets</h3>
      <div class="table-wrap">
        <table class="table">
          <thead><tr><th>Priority</th><th>First response (min)</th><th>Update every (min)</th><th>Resolution (min)</th><th>Warn at %</th></tr></thead>
          <tbody>
          <?php foreach ($priorities as $p) : $t = $targetByPriority[$p['id']] ?? null; ?>
            <tr>
              <td><?= priority_badge($p) ?> <?= e($p['name']) ?></td>
              <td><input type="number" name="first_response_<?= $p['id'] ?>" value="<?= $t['first_response_minutes'] ?? '' ?>" style="width:90px"></td>
              <td><input type="number" name="update_freq_<?= $p['id'] ?>" value="<?= $t['update_frequency_minutes'] ?? '' ?>" style="width:90px"></td>
              <td><input type="number" name="resolution_<?= $p['id'] ?>" value="<?= $t['resolution_minutes'] ?? '' ?>" style="width:90px"></td>
              <td>
                <input type="number" name="warn1_<?= $p['id'] ?>" value="<?= $t['warn_pct_1'] ?? 50 ?>" style="width:55px"> /
                <input type="number" name="warn2_<?= $p['id'] ?>" value="<?= $t['warn_pct_2'] ?? 80 ?>" style="width:55px">
              </td>
            </tr>
          <?php endforeach; ?>
          </tbody>
        </table>
      </div>
      <p class="help-text">Minutes are wall-clock business minutes per the calendar above (e.g. 480 = 8 working hours). Leave a row blank to skip that priority.</p>
      <button type="submit" class="btn btn-primary mt-2"><?= $policy ? 'Save policy & targets' : 'Create policy' ?></button>
    </form>
  </div>
</div>

<?php if ($policy) : ?>
<div class="ticket-grid mt-3" style="grid-template-columns:1fr 340px">
  <div class="card">
    <div class="card-header"><h3>Escalation matrix</h3></div>
    <div class="table-wrap">
      <table class="table">
        <thead><tr><th>Level</th><th>Trigger</th><th>Notify</th><th>Action</th></tr></thead>
        <tbody>
        <?php foreach ($levels as $l) : ?>
          <tr>
            <td>L<?= $l['level'] ?></td>
            <td class="text-sm"><?= $l['trigger_on_breach'] ? 'On breach' : ($l['trigger_pct'] . '% of resolution target elapsed') ?></td>
            <td><span class="badge badge-outline"><?= e($l['notify_role']) ?></span></td>
            <td class="text-sm"><?= e($l['action_note']) ?></td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($levels)) : ?><tr><td colspan="4" class="text-faint">No escalation levels yet — tickets will still flag breaches but nothing auto-escalates.</td></tr><?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
  <div class="card">
    <div class="card-header"><h3>Add escalation level</h3></div>
    <div class="card-body">
      <form method="post" action="<?= url('/admin/sla/' . $policy['id'] . '/escalation') ?>">
        <?= csrf_field() ?>
        <div class="form-group"><label>Level <span class="req">*</span></label><input type="number" name="level" min="1" required value="<?= count($levels) + 1 ?>"></div>
        <div class="form-group"><label>Trigger at % elapsed (leave blank if "on breach")</label><input type="number" name="trigger_pct" placeholder="e.g. 80"></div>
        <label class="checkbox-row"><input type="checkbox" name="trigger_on_breach" value="1"> Trigger on breach instead</label>
        <div class="form-group mt-2">
          <label>Notify</label>
          <select name="notify_role"><option>LEAD</option><option>HEAD</option><option>ACCOUNT_MANAGER</option></select>
        </div>
        <div class="form-group"><label>Action note</label><input type="text" name="action_note" placeholder="What should happen"></div>
        <button class="btn btn-primary btn-block">Add level</button>
      </form>
    </div>
  </div>
</div>
<?php endif; ?>
