<!-- ===================== STAGES TAB ===================== -->
<div class="tab-pane <?= ($requested_tab === 'stages') ? 'active' : '' ?>" id="tab-stages">
  <div class="toolbar">
    <span class="card-title">Deal Stages</span>
    <?php if ($is_admin): ?>
    <button class="btn btn-primary btn-sm" onclick="openModal('addStageModal')" style="margin-left:auto">+ Add Stage</button>
    <?php endif; ?>
  </div>

  <div class="card">
    <div class="table-wrapper">
      <table class="crm-table">
        <thead>
          <tr>
            <th>Stage</th>
            <th>Position</th>
            <th>Color</th>
            <th>Type</th>
            <th>Actions</th>
          </tr>
        </thead>
        <tbody>
        <?php while ($s = $stages->fetch_assoc()): ?>
        <tr id="stage_<?= $s['id'] ?>">
          <td class="font-bold">
            <?php if ($is_admin): ?>
            <input type="text" class="form-control form-control-sm" id="stage_name_<?= $s['id'] ?>" value="<?= htmlspecialchars($s['name']) ?>" style="display:none;">
            <span id="stage_name_span_<?= $s['id'] ?>"><?= htmlspecialchars($s['name']) ?></span>
            <?php else: ?>
            <?= htmlspecialchars($s['name']) ?>
            <?php endif; ?>
          </td>
          <td>
            <?php if ($is_admin): ?>
            <input type="number" class="form-control form-control-sm" id="stage_position_<?= $s['id'] ?>" value="<?= $s['position'] ?>" style="display:none; width:80px;">
            <span id="stage_position_span_<?= $s['id'] ?>"><?= $s['position'] ?></span>
            <?php else: ?>
            <?= $s['position'] ?>
            <?php endif; ?>
          </td>
          <td>
            <?php if ($is_admin): ?>
            <input type="color" class="form-control form-control-sm" id="stage_color_<?= $s['id'] ?>" value="<?= $s['color'] ?>" style="display:none; width:60px; height:30px;">
            <?php endif; ?>
            <span style="background:<?= $s['color'] ?>; width:18px; height:18px; display:inline-block; border-radius:4px;" id="stage_color_span_<?= $s['id'] ?>"></span>
          </td>
          <td>
            <?php if ($is_admin): ?>
            <select class="form-control form-control-sm" id="stage_type_<?= $s['id'] ?>" style="display:none;">
              <option value="normal" <?= (!$s['is_start'] && !$s['is_end']) ? 'selected' : '' ?>>Normal</option>
              <option value="start" <?= $s['is_start'] ? 'selected' : '' ?>>Start</option>
              <option value="end" <?= $s['is_end'] ? 'selected' : '' ?>>End</option>
            </select>
            <?php endif; ?>
            <span id="stage_type_span_<?= $s['id'] ?>">
              <?php
              if ($s['is_start']) echo '<span class="badge badge-green">Start</span>';
              elseif ($s['is_end']) echo '<span class="badge badge-red">End</span>';
              else echo '<span class="badge badge-blue">Normal</span>';
              ?>
            </span>
          </td>
          <td>
            <?php if ($is_admin): ?>
            <button class="btn btn-warning btn-sm" id="stage_edit_btn_<?= $s['id'] ?>" onclick="editStage(<?= $s['id'] ?>)">Edit</button>
            <button class="btn btn-success btn-sm" id="stage_save_btn_<?= $s['id'] ?>" style="display:none;" onclick="saveStage(<?= $s['id'] ?>)">Save</button>
            <button class="btn btn-danger btn-sm" onclick="deleteStage(<?= $s['id'] ?>)">Delete</button>
            <?php endif; ?>
          </td>
        </tr>
        <?php endwhile; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>
