<!-- EDIT USER MODAL -->
<div class="modal-overlay" id="editUserModal">
  <div class="modal">
    <div class="modal-header"><span class="modal-title">Edit User</span><button class="modal-close">✕</button></div>
    <div class="modal-body">
      <form id="editUserForm">
        <input type="hidden" name="user_id" id="edit_user_id">
        <div class="form-grid">
          <div class="form-group"><label class="form-label">Name *</label><input type="text" name="name" id="edit_user_name" class="form-control" required></div>
          <div class="form-group"><label class="form-label">Phone</label><input type="text" name="phone" id="edit_user_phone" class="form-control"></div>
          <div class="form-group span-2"><label class="form-label">Email *</label><input type="email" name="email" id="edit_user_email" class="form-control" required></div>
          <div class="form-group"><label class="form-label">New Password (leave blank to keep current)</label><input type="password" name="password" class="form-control" minlength="6"></div>
          <div class="form-group"><label class="form-label">Role</label>
            <select name="role_id" id="edit_user_role" class="form-control">
              <option value="">No Role</option>
              <?php $roles->data_seek(0); while ($ro = $roles->fetch_assoc()): ?>
              <option value="<?= $ro['id'] ?>"><?= htmlspecialchars($ro['name']) ?></option>
              <?php endwhile; ?>
            </select>
          </div>
        </div>
      </form>
    </div>
    <div class="modal-footer">
      <button class="btn btn-secondary" onclick="closeModal('editUserModal')">Cancel</button>
      <button class="btn btn-primary" onclick="updateUser()">Update User</button>
    </div>
  </div>
</div>

<!-- ADD LOST REASON MODAL -->
<div class="modal-overlay" id="addLostModal">
  <div class="modal">
    <div class="modal-header"><span class="modal-title">Add Lost Reason</span><button class="modal-close">✕</button></div>
    <div class="modal-body">
      <div class="form-group"><label class="form-label">Reason *</label><input type="text" id="lostReasonInput" class="form-control" placeholder="e.g. Price too high"></div>
    </div>
    <div class="modal-footer">
      <button class="btn btn-secondary" onclick="closeModal('addLostModal')">Cancel</button>
      <button class="btn btn-primary" onclick="addLostReason()">Add</button>
    </div>
  </div>
</div>


<!-- EDIT LOST REASON MODAL -->
<div class="modal-overlay" id="editLostModal">
  <div class="modal">
    <div class="modal-header"><span class="modal-title">Edit Lost Reason</span><button class="modal-close">✕</button></div>
    <div class="modal-body">
      <input type="hidden" id="editLostReasonId">
      <div class="form-group"><label class="form-label">Reason *</label><input type="text" id="editLostReasonName" class="form-control"></div>
      <div class="form-group"><label class="form-label">Status</label><select id="editLostReasonStatus" class="form-control"><option value="active">Active</option><option value="inactive">Inactive</option></select></div>
    </div>
    <div class="modal-footer"><button class="btn btn-secondary" onclick="closeModal('editLostModal')">Cancel</button><button class="btn btn-primary" onclick="saveLostReasonModal()">Save</button></div>
  </div>
</div>

<!-- EDIT SOURCE MODAL -->
<div class="modal-overlay" id="editSourceModal">
  <div class="modal">
    <div class="modal-header"><span class="modal-title">Edit Source</span><button class="modal-close">✕</button></div>
    <div class="modal-body">
      <input type="hidden" id="editSourceId">
      <div class="form-group"><label class="form-label">Source Name *</label><input type="text" id="editSourceName" class="form-control"></div>
      <div class="form-group"><label class="form-label">Status</label><select id="editSourceStatus" class="form-control"><option value="1">Active</option><option value="0">Inactive</option></select></div>
    </div>
    <div class="modal-footer"><button class="btn btn-secondary" onclick="closeModal('editSourceModal')">Cancel</button><button class="btn btn-primary" onclick="saveSourceModal()">Save</button></div>
  </div>
</div>

<!-- ADD STAGE MODAL -->
<div class="modal-overlay" id="addStageModal">
  <div class="modal">
    <div class="modal-header">
      <span class="modal-title">Add Stage</span>
      <button class="modal-close">✕</button>
    </div>
    <div class="modal-body">
      <div class="form-group">
        <label class="form-label">Stage Name *</label>
        <input type="text" id="stageName" class="form-control">
      </div>
      <div class="form-group">
        <label class="form-label">Position</label>
        <input type="number" id="stagePosition" class="form-control">
      </div>
      <div class="form-group">
        <label class="form-label">Color</label>
        <input type="color" id="stageColor" class="form-control" value="#6B7280">
      </div>
      <div class="form-group">
        <label class="form-label">Stage Type</label>
<?php
$hasStartStage = false; $hasWonStage = false; $hasLostStage = false;
$stageTypeCheck = $conn->query("SELECT is_start,is_end,result_type,name FROM stage WHERE account_id=$account_id");
while($stageTypeCheck && $stc=$stageTypeCheck->fetch_assoc()){
  if((int)$stc['is_start']===1) $hasStartStage = true;
  if((int)$stc['is_end']===1 && (($stc['result_type'] ?? '') === 'won' || strtolower($stc['name'] ?? '') === 'won')) $hasWonStage = true;
  if((int)$stc['is_end']===1 && (($stc['result_type'] ?? '') === 'lost' || strtolower($stc['name'] ?? '') === 'lost')) $hasLostStage = true;
}
?>
        <select id="stageType" class="form-control" onchange="updateStageResultOptions()">
          <option value="normal">Normal</option>
          <?php if(!$hasStartStage): ?><option value="start">Start</option><?php endif; ?>
          <?php if(!$hasWonStage || !$hasLostStage): ?><option value="end">End</option><?php endif; ?>
        </select>
      </div>
      <div class="form-group" id="stageResultWrap" style="display:none">
        <label class="form-label">End Result</label>
        <select id="stageResultType" class="form-control">
          <?php if(!$hasWonStage): ?><option value="won">Won</option><?php endif; ?>
          <?php if(!$hasLostStage): ?><option value="lost">Lost</option><?php endif; ?>
        </select>
      </div>
      <div class="alert alert-info" style="margin-top:10px">Only one Start, one End-Won and one End-Lost is allowed per account. Names can be changed later, but type stays fixed.</div>
    </div>
    <div class="modal-footer">
      <button class="btn btn-secondary" onclick="closeModal('addStageModal')">Cancel</button>
      <button class="btn btn-primary" onclick="addStage()">Add</button>
    </div>
  </div>
</div>
