<?php
if (!currentUserIsSuperAdmin($conn)) { echo '<div class="card" style="padding:22px"><h3>Permission denied</h3><p class="text-muted">Only the system owner can manage accounts.</p></div>'; return; }
$accountsRows = $conn->query("SELECT a.*, (SELECT COUNT(*) FROM users u WHERE u.account_id=a.id) users_count FROM accounts a ORDER BY a.datetime_created DESC");
?>
<div class="fade-in">
  <div class="toolbar"><span class="card-title">System Accounts</span><button class="btn btn-primary btn-sm" onclick="openModal('createAccountModal')" style="margin-left:auto">+ Create Account</button></div>
  <div class="card"><div class="table-wrapper"><table class="crm-table">
    <thead><tr><th>Business</th><th>Owner</th><th>Email</th><th>Status</th><th>Users</th><th>Created</th></tr></thead><tbody>
      <?php while($a=$accountsRows->fetch_assoc()): ?>
      <tr><td><strong><?= htmlspecialchars($a['business_name']) ?></strong></td><td><?= htmlspecialchars($a['client_name']) ?></td><td><?= htmlspecialchars($a['email'] ?: '—') ?></td><td><span class="badge <?= $a['status']==='active'?'badge-green':'badge-gray' ?>"><?= htmlspecialchars($a['status']) ?></span></td><td><?= (int)$a['users_count'] ?></td><td class="text-xs text-muted"><?= formatDate($a['datetime_created']) ?></td></tr>
      <?php endwhile; ?>
    </tbody></table></div></div>
</div>
<div class="modal-overlay" id="createAccountModal"><div class="modal"><div class="modal-header"><span class="modal-title">Create Account</span><button class="modal-close">✕</button></div><div class="modal-body"><form id="createAccountForm"><div class="form-grid"><div class="form-group"><label class="form-label">Client Name *</label><input type="text" name="client_name" class="form-control" required></div><div class="form-group"><label class="form-label">Business Name *</label><input type="text" name="business_name" class="form-control" required></div><div class="form-group"><label class="form-label">Phone</label><input type="text" name="phone" class="form-control"></div><div class="form-group"><label class="form-label">Owner Email *</label><input type="email" name="email" class="form-control" required></div><div class="form-group span-2"><label class="form-label">Owner Password *</label><input type="password" name="password" class="form-control" minlength="6" required></div></div></form><div class="alert alert-info" style="margin-top:10px">New / Won / Lost stages will be created automatically for the new account.</div></div><div class="modal-footer"><button class="btn btn-secondary" onclick="closeModal('createAccountModal')">Cancel</button><button class="btn btn-primary" onclick="createAccount()">Create</button></div></div></div>
<script>
async function createAccount(){const fd=new FormData(document.getElementById('createAccountForm')); fd.append('action','create_account'); const res=await apiCall('handlers/accounts.php',fd); if(res.success){showToast('Account created','success'); setTimeout(()=>location.reload(),600);} else showToast(res.message||'Error','error');}
</script>
