<!-- ===================== 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_<?= crmEid($s['id']) ?>">
          <td class="font-bold">
            <?php if ($is_admin): ?>
            <input type="text" class="form-control form-control-sm" id="stage_name_<?= crmEid($s['id']) ?>" value="<?= htmlspecialchars($s['name']) ?>" style="display:none;">
            <span id="stage_name_span_<?= crmEid($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_<?= crmEid($s['id']) ?>" value="<?= $s['position'] ?>" style="display:none; width:80px;">
            <span id="stage_position_span_<?= crmEid($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_<?= crmEid($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_<?= crmEid($s['id']) ?>"></span>
          </td>
          <td>
            <?php if ($is_admin): ?>
            <input type="hidden" id="stage_type_<?= crmEid($s['id']) ?>" value="<?= $s['is_start'] ? 'start' : (($s['is_end'] && (($s['result_type'] ?? '') === 'won' || strtolower($s['name']) === 'won')) ? 'won' : (($s['is_end'] && (($s['result_type'] ?? '') === 'lost' || strtolower($s['name']) === 'lost')) ? 'lost' : 'normal')) ?>">
            <?php endif; ?>
            <span id="stage_type_span_<?= crmEid($s['id']) ?>">
              <?php
              if ($s['is_start']) echo '<span class="badge badge-green">Start</span>';
              elseif ($s['is_end'] && (($s['result_type'] ?? '') === 'won' || strtolower($s['name']) === 'won')) echo '<span class="badge badge-green">End - Won</span>';
              elseif ($s['is_end'] && (($s['result_type'] ?? '') === 'lost' || strtolower($s['name']) === 'lost')) echo '<span class="badge badge-red">End - Lost</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_<?= crmEid($s['id']) ?>" onclick="editStage('<?= crmEid($s['id']) ?>')">Edit</button>
            <button class="btn btn-success btn-sm" id="stage_save_btn_<?= crmEid($s['id']) ?>" style="display:none;" onclick="saveStage('<?= crmEid($s['id']) ?>')">Save</button>
            <?php $fixedStage = ($s['is_start'] || ($s['is_end'] && in_array(($s['result_type'] ?? ''), ['won','lost'], true))); ?>
            <?php if (!$fixedStage): ?><button class="btn btn-danger btn-sm" onclick="deleteStage('<?= crmEid($s['id']) ?>')">Delete</button><?php else: ?><span class="badge badge-gray">Fixed</span><?php endif; ?>
            <?php endif; ?>
          </td>
        </tr>
        <?php endwhile; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>
