<?php
// organization/includes/header.php
// Auth guard + full HTML shell for org admin portal.
// Set $page_title and $active_nav before including.

if (!isset($_SESSION)) session_start();

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'org_admin') {
    header("Location: login.php"); exit;
}

require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/functions.php';

$uid    = (int)$_SESSION['user_id'];
$org_id = (int)($_SESSION['organization_id'] ?? 0);

// Verify session integrity on every request
$u = $conn->prepare(
    "SELECT u.name, u.email, o.name AS org_name, o.status AS org_status
     FROM users u
     INNER JOIN organizations o ON u.organization_id = o.id
     WHERE u.id=? AND u.role='org_admin' AND u.status=1 AND u.organization_id=?"
);
$u->bind_param("ii", $uid, $org_id);
$u->execute();
$__user = $u->get_result()->fetch_assoc();

if (!$__user) {
    session_destroy();
    header("Location: login.php?reason=session_expired"); exit;
}

if ($__user['org_status'] !== 'active') {
    session_destroy();
    header("Location: login.php?reason=org_suspended"); exit;
}

$__name     = $__user['name'];
$__email    = $__user['email'];
$__org_name = $__user['org_name'];
$__initials = initials($__name);

$__active   = $active_nav  ?? 'dashboard';
$__title    = $page_title  ?? 'Dashboard';

// Quick stats for topbar
$r = $conn->query("SELECT COUNT(*) FROM users WHERE organization_id={$org_id} AND role='org_staff' AND status=1");
$__active_staff = $r ? (int)$r->fetch_row()[0] : 0;

$r2 = $conn->query(
    "SELECT COALESCE(SUM(w.balance),0)
     FROM wallets w INNER JOIN users u ON w.user_id=u.id
     WHERE u.organization_id={$org_id} AND w.wallet_type='meal_ticket'"
);
$__total_mt_balance = $r2 ? (float)$r2->fetch_row()[0] : 0.00;

$r3 = $conn->query(
    "SELECT COALESCE(balance,0) FROM wallets WHERE organization_id={$org_id} AND wallet_type='organization' LIMIT 1"
);
$__org_wallet_balance = $r3 ? (float)$r3->fetch_row()[0] : 0.00;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo h($__title); ?> — Dabil Org Portal</title>
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
<style>
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
:root{
  --primary:#225AE3;--primary-dark:#1a47b8;--primary-light:#4f79ea;
  --primary-bg:#EEF3FD;--bg:#F5F7F8;--white:#FFFFFF;
  --text:#0F172A;--text-muted:#64748B;--text-light:#94A3B8;
  --border:#E2E8F0;--border-dark:#CBD5E1;
  --success:#10B981;--success-bg:#F0FDF4;
  --error:#EF4444;--error-bg:#FEF2F2;
  --warning:#F59E0B;--warning-bg:#FFFBEB;
  --info:#06B6D4;--info-bg:#ECFEFF;
  --org-accent:#225AE3;--org-accent-bg:#EEF3FD;
  --sidebar-w:262px;--topbar-h:64px;
  --radius:12px;--radius-lg:16px;
  --shadow-sm:0 1px 3px rgba(0,0,0,.07);
  --shadow-md:0 4px 16px rgba(0,0,0,.08);
  --shadow-lg:0 8px 30px rgba(34,90,227,.14);
  --font:'Plus Jakarta Sans',sans-serif;
}
body{font-family:var(--font);background:var(--bg);color:var(--text);min-height:100vh}
a{text-decoration:none;color:inherit}
button{font-family:var(--font)}
.sidebar{position:fixed;top:0;left:0;width:var(--sidebar-w);height:100vh;background:var(--white);border-right:1px solid var(--border);display:flex;flex-direction:column;z-index:200;transition:transform .3s cubic-bezier(.4,0,.2,1)}
.sidebar-brand{padding:0 20px;height:var(--topbar-h);display:flex;align-items:center;gap:10px;border-bottom:1px solid var(--border);flex-shrink:0}
.brand-icon{width:36px;height:36px;background:var(--org-accent);border-radius:10px;display:flex;align-items:center;justify-content:center;flex-shrink:0}
.brand-icon svg{width:18px;height:18px;stroke:#fff;fill:none}
.brand-name{font-size:1.1rem;font-weight:800;letter-spacing:-0.02em;color:var(--primary)}
.brand-tag{font-size:.6rem;font-weight:700;text-transform:uppercase;letter-spacing:.1em;color:var(--white);background:var(--org-accent);padding:2px 7px;border-radius:20px;margin-left:auto}
.sidebar-scroll{flex:1;overflow-y:auto;padding:16px 12px}
.sidebar-scroll::-webkit-scrollbar{width:4px}
.sidebar-scroll::-webkit-scrollbar-thumb{background:var(--border);border-radius:4px}
.nav-section{margin-bottom:24px}
.nav-label{font-size:.65rem;font-weight:700;text-transform:uppercase;letter-spacing:.12em;color:var(--text-light);padding:0 12px;margin-bottom:6px}
.nav-item{display:flex;align-items:center;gap:10px;padding:10px 12px;border-radius:10px;cursor:pointer;transition:all .18s;color:var(--text-muted);font-size:.85rem;font-weight:500;margin-bottom:2px;position:relative}
.nav-item:hover{background:var(--bg);color:var(--text)}
.nav-item.active{background:var(--org-accent-bg);color:var(--org-accent);font-weight:700}
.nav-item.active::before{content:'';position:absolute;left:0;top:20%;bottom:20%;width:3px;background:var(--org-accent);border-radius:0 3px 3px 0;margin-left:-12px}
.nav-item svg{width:18px;height:18px;flex-shrink:0}
.nav-badge{margin-left:auto;background:var(--org-accent);color:#fff;font-size:.65rem;padding:2px 7px;border-radius:20px;font-weight:700}
.org-name-pill{padding:8px 12px;margin:0 0 12px;border-radius:10px;background:var(--org-accent-bg);font-size:.78rem;font-weight:700;color:var(--org-accent);display:flex;align-items:center;gap:8px}
.org-name-pill svg{width:14px;height:14px;stroke:currentColor;fill:none;flex-shrink:0}
.sidebar-footer{border-top:1px solid var(--border);padding:16px 12px;flex-shrink:0}
.user-pill{display:flex;align-items:center;gap:10px;padding:10px 12px;border-radius:10px;background:var(--bg)}
.avatar{width:34px;height:34px;border-radius:50%;background:var(--org-accent);display:flex;align-items:center;justify-content:center;font-size:.75rem;font-weight:800;color:#fff;flex-shrink:0}
.user-details{flex:1;overflow:hidden}
.user-details .uname{font-size:.82rem;font-weight:700;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.user-details .uemail{font-size:.7rem;color:var(--text-muted);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.topbar{position:fixed;top:0;left:var(--sidebar-w);right:0;height:var(--topbar-h);background:var(--white);border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;padding:0 28px;z-index:100;transition:left .3s}
.topbar-left{display:flex;align-items:center;gap:12px}
.hamburger{display:none;background:none;border:none;cursor:pointer;padding:6px;border-radius:8px;color:var(--text-muted)}
.hamburger svg{width:20px;height:20px;stroke:currentColor;fill:none}
.page-label{font-size:1rem;font-weight:700;letter-spacing:-0.01em}
.topbar-right{display:flex;align-items:center;gap:12px}
.topbar-stat{display:flex;align-items:center;gap:7px;background:var(--bg);border:1.5px solid var(--border);border-radius:24px;padding:6px 14px 6px 10px}
.topbar-stat svg{width:15px;height:15px;stroke:var(--org-accent);fill:none;flex-shrink:0}
.topbar-stat span{font-size:.78rem;font-weight:700;color:var(--text)}
.topbar-avatar{width:36px;height:36px;border-radius:50%;background:var(--org-accent);display:flex;align-items:center;justify-content:center;font-size:.75rem;font-weight:800;color:#fff;cursor:pointer}
.layout{display:flex;min-height:100vh}
.main-content{margin-left:var(--sidebar-w);margin-top:var(--topbar-h);flex:1;padding:28px;min-height:calc(100vh - var(--topbar-h))}
.card{background:var(--white);border-radius:var(--radius-lg);border:1px solid var(--border);box-shadow:var(--shadow-sm)}
.card-header{padding:18px 22px;border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;gap:12px}
.card-header h3{font-size:.95rem;font-weight:700}
.card-body{padding:20px 22px}
.btn{display:inline-flex;align-items:center;justify-content:center;gap:8px;border-radius:10px;border:none;cursor:pointer;font-family:var(--font);font-weight:600;transition:all .2s}
.btn-primary{background:var(--primary);color:#fff;box-shadow:var(--shadow-lg)}
.btn-primary:hover{background:var(--primary-dark);transform:translateY(-1px)}
.btn-org{background:var(--org-accent);color:#fff;box-shadow:0 4px 14px rgba(34,90,227,.25)}
.btn-org:hover{background:#1a47b8;transform:translateY(-1px)}
.btn-secondary{background:var(--bg);color:var(--text);border:1.5px solid var(--border)}
.btn-secondary:hover{background:var(--border)}
.btn-danger{background:var(--error);color:#fff}
.btn-danger:hover{background:#dc2626;transform:translateY(-1px)}
.btn-success{background:var(--success);color:#fff}
.btn-success:hover{background:#059669;transform:translateY(-1px)}
.btn-sm{padding:7px 12px;font-size:.78rem}
.btn-md{padding:10px 20px;font-size:.875rem}
.btn-lg{padding:13px 24px;font-size:.9rem}
.btn-block{width:100%}
.btn svg{width:15px;height:15px;stroke:currentColor;fill:none}
.badge{padding:3px 10px;border-radius:20px;font-size:.72rem;font-weight:700;display:inline-flex;align-items:center;gap:4px}
.badge-success{background:var(--success-bg);color:var(--success)}
.badge-warning{background:var(--warning-bg);color:var(--warning)}
.badge-error{background:var(--error-bg);color:var(--error)}
.badge-primary{background:var(--primary-bg);color:var(--primary)}
.badge-org{background:var(--org-accent-bg);color:var(--org-accent)}
.badge-muted{background:var(--bg);color:var(--text-muted)}
.data-table{width:100%;border-collapse:collapse}
.data-table th{text-align:left;padding:10px 16px;font-size:.7rem;font-weight:700;text-transform:uppercase;letter-spacing:.08em;color:var(--text-muted);background:var(--bg);border-bottom:1px solid var(--border)}
.data-table td{padding:13px 16px;font-size:.84rem;border-bottom:1px solid var(--border)}
.data-table tr:last-child td{border-bottom:none}
.data-table tbody tr:hover td{background:var(--bg)}
.alert{padding:12px 16px;border-radius:10px;font-size:.82rem;font-weight:500;display:flex;align-items:center;gap:10px;margin-bottom:18px}
.alert svg{width:16px;height:16px;stroke:currentColor;fill:none;flex-shrink:0}
.alert-success{background:var(--success-bg);color:var(--success);border:1px solid #BBF7D0}
.alert-error{background:var(--error-bg);color:var(--error);border:1px solid #FECACA}
.alert-warning{background:var(--warning-bg);color:var(--warning);border:1px solid #FDE68A}
.alert-info{background:var(--primary-bg);color:var(--primary);border:1px solid rgba(34,90,227,.2)}
.form-group{margin-bottom:18px}
.form-group label{display:block;font-size:.7rem;font-weight:700;text-transform:uppercase;letter-spacing:.1em;color:var(--text-muted);margin-bottom:7px}
.form-control{width:100%;height:46px;border:1.5px solid var(--border);border-radius:10px;background:var(--bg);padding:0 14px;font-size:.875rem;color:var(--text);font-family:var(--font);outline:none;transition:all .2s}
.form-control:focus{border-color:var(--org-accent);background:var(--white);box-shadow:0 0 0 3px rgba(34,90,227,.1)}
.form-control::placeholder{color:var(--text-light)}
textarea.form-control{height:auto;padding:12px 14px;resize:vertical}
select.form-control{cursor:pointer}
.modal-backdrop{display:none;position:fixed;inset:0;background:rgba(0,0,0,.5);z-index:500;backdrop-filter:blur(3px);align-items:center;justify-content:center;padding:20px}
.modal-backdrop.open{display:flex}
.modal{background:var(--white);border-radius:20px;width:100%;max-width:520px;box-shadow:0 20px 60px rgba(0,0,0,.2);animation:modalIn .25s ease;max-height:90vh;overflow-y:auto}
.modal-lg{max-width:680px}
.modal-header{padding:22px 24px;border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;position:sticky;top:0;background:var(--white);z-index:1}
.modal-header h3{font-size:1rem;font-weight:800}
.modal-close{background:none;border:none;cursor:pointer;width:32px;height:32px;border-radius:8px;display:flex;align-items:center;justify-content:center;color:var(--text-muted);transition:background .2s}
.modal-close:hover{background:var(--bg)}
.modal-close svg{width:18px;height:18px;stroke:currentColor;fill:none}
.modal-body{padding:24px}
.modal-footer{padding:16px 24px;border-top:1px solid var(--border);display:flex;gap:10px;justify-content:flex-end}
@keyframes modalIn{from{opacity:0;transform:translateY(12px) scale(.98)}to{opacity:1;transform:none}}
.overlay{display:none;position:fixed;inset:0;background:rgba(0,0,0,.4);z-index:199;backdrop-filter:blur(2px)}
.overlay.active{display:block}
.stat-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:16px;margin-bottom:24px}
.stat-card{background:var(--white);border:1px solid var(--border);border-radius:var(--radius-lg);padding:20px 22px}
.stat-card .stat-label{font-size:.72rem;font-weight:700;text-transform:uppercase;letter-spacing:.08em;color:var(--text-muted);margin-bottom:8px}
.stat-card .stat-value{font-size:1.7rem;font-weight:800;letter-spacing:-0.03em;line-height:1}
.stat-card .stat-sub{font-size:.75rem;color:var(--text-muted);margin-top:6px}
@media(max-width:1024px){.sidebar{transform:translateX(-100%)}.sidebar.open{transform:translateX(0)}.topbar{left:0}.main-content{margin-left:0}.hamburger{display:flex}}
@media(max-width:640px){.main-content{padding:20px 16px}.page-label{display:none}.topbar-stat{display:none}}
@keyframes fadeUp{from{opacity:0;transform:translateY(14px)}to{opacity:1;transform:translateY(0)}}
.anim-up{animation:fadeUp .4s ease both}
@keyframes spin{to{transform:rotate(360deg)}}
</style>
</head>
<body>
<div class="overlay" id="overlay" onclick="closeSidebar()"></div>

<aside class="sidebar" id="sidebar">
  <div class="sidebar-brand">
    <img src="assets/img/dlogo.png" alt="Dabil" style="height:30px;width:auto;display:block">
    <span class="brand-tag">Org Portal</span>
  </div>

  <div class="sidebar-scroll">
    <div style="padding:12px 0 4px">
      <div class="org-name-pill">
        <svg viewBox="0 0 24 24" stroke-width="2"><path d="M3 9l9-7 9 7v11a2 2 0 01-2 2H5a2 2 0 01-2-2z"/></svg>
        <?php echo h($__org_name); ?>
      </div>
    </div>

    <div class="nav-section">
      <div class="nav-label">Overview</div>
      <a href="dashboard.php" class="nav-item <?php echo $__active==='dashboard'?'active':''; ?>">
        <svg viewBox="0 0 24 24" stroke-width="2" fill="none" stroke="currentColor" stroke-linecap="round"><rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/></svg>
        Dashboard
      </a>
    </div>

    <div class="nav-section">
      <div class="nav-label">Staff</div>
      <a href="staff.php" class="nav-item <?php echo in_array($__active,['staff','staff_view'])?'active':''; ?>">
        <svg viewBox="0 0 24 24" stroke-width="2" fill="none" stroke="currentColor" stroke-linecap="round"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 00-3-3.87M16 3.13a4 4 0 010 7.75"/></svg>
        Staff List
        <span class="nav-badge"><?php echo $__active_staff; ?></span>
      </a>
    </div>

    <div class="nav-section">
      <div class="nav-label">Finance</div>
      <a href="wallet.php" class="nav-item <?php echo $__active==='wallet'?'active':''; ?>">
        <svg viewBox="0 0 24 24" stroke-width="2" fill="none" stroke="currentColor" stroke-linecap="round"><path d="M21 12V7H5a2 2 0 010-4h15a1 1 0 011 1v4"/><path d="M3 5v14a2 2 0 002 2h16v-5"/><path d="M18 12a2 2 0 000 4h4v-4z"/></svg>
        Org Wallet
        <span class="nav-badge" style="background:<?php echo $__org_wallet_balance < 1000 ? 'var(--error)' : 'var(--org-accent)'; ?>"><?php echo '₦' . number_format($__org_wallet_balance/1000, 1) . 'k'; ?></span>
      </a>
      <a href="fund_staff.php" class="nav-item <?php echo $__active==='fund_staff'?'active':''; ?>">
        <svg viewBox="0 0 24 24" stroke-width="2" fill="none" stroke="currentColor" stroke-linecap="round"><path d="M12 2v20M17 5H9.5a3.5 3.5 0 100 7h5a3.5 3.5 0 010 7H6"/></svg>
        Fund Staff
      </a>
      <a href="fund_bulk.php" class="nav-item <?php echo $__active==='fund_bulk'?'active':''; ?>">
        <svg viewBox="0 0 24 24" stroke-width="2" fill="none" stroke="currentColor" stroke-linecap="round"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/><line x1="19" y1="8" x2="19" y2="14"/><line x1="22" y1="11" x2="16" y2="11"/></svg>
        Bulk Fund
      </a>
      <a href="schedules.php" class="nav-item <?php echo $__active==='schedules'?'active':''; ?>">
        <svg viewBox="0 0 24 24" stroke-width="2" fill="none" stroke="currentColor" stroke-linecap="round"><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>
        Schedules
      </a>
      <a href="reports.php" class="nav-item <?php echo $__active==='reports'?'active':''; ?>">
        <svg viewBox="0 0 24 24" stroke-width="2" fill="none" stroke="currentColor" stroke-linecap="round"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg>
        Reports
      </a>
      <a href="transactions.php" class="nav-item <?php echo $__active==='transactions'?'active':''; ?>">
        <svg viewBox="0 0 24 24" stroke-width="2" fill="none" stroke="currentColor" stroke-linecap="round"><polyline points="23 6 13.5 15.5 8.5 10.5 1 18"/><polyline points="17 6 23 6 23 12"/></svg>
        Transactions
      </a>
    </div>

    <div class="nav-section">
      <div class="nav-label">Account</div>
      <a href="profile.php" class="nav-item <?php echo $__active==='profile'?'active':''; ?>">
        <svg viewBox="0 0 24 24" stroke-width="2" fill="none" stroke="currentColor" stroke-linecap="round"><path d="M20 21v-2a4 4 0 00-4-4H8a4 4 0 00-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
        Profile
      </a>
      <a href="logout.php" class="nav-item">
        <svg viewBox="0 0 24 24" stroke-width="2" fill="none" stroke="currentColor" stroke-linecap="round"><path d="M9 21H5a2 2 0 01-2-2V5a2 2 0 012-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/></svg>
        Sign Out
      </a>
    </div>
  </div>

  <div class="sidebar-footer">
    <div class="user-pill">
      <div class="avatar"><?php echo h($__initials); ?></div>
      <div class="user-details">
        <div class="uname"><?php echo h($__name); ?></div>
        <div class="uemail"><?php echo h($__email); ?></div>
      </div>
    </div>
  </div>
</aside>

<div class="topbar">
  <div class="topbar-left">
    <button class="hamburger" onclick="toggleSidebar()">
      <svg viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round"><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="18" x2="21" y2="18"/></svg>
    </button>
    <span class="page-label"><?php echo h($__title); ?></span>
  </div>
  <div class="topbar-right">
    <div class="topbar-stat">
      <svg viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/></svg>
      <span><?php echo $__active_staff; ?> staff</span>
    </div>
    <div class="topbar-stat">
      <svg viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round"><path d="M12 2v20M17 5H9.5a3.5 3.5 0 100 7h5a3.5 3.5 0 010 7H6"/></svg>
      <span><?php echo fmt_money($__total_mt_balance); ?> MT</span>
    </div>
    <a href="wallet.php" class="topbar-stat" style="text-decoration:none;border-color:<?php echo $__org_wallet_balance < 1000 ? 'rgba(239,68,68,.4)' : 'var(--border)'; ?>" title="Organisation Wallet">
      <svg viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" style="stroke:<?php echo $__org_wallet_balance < 1000 ? 'var(--error)' : 'var(--org-accent)'; ?>"><path d="M21 12V7H5a2 2 0 010-4h15a1 1 0 011 1v4"/><path d="M3 5v14a2 2 0 002 2h16v-5"/><path d="M18 12a2 2 0 000 4h4v-4z"/></svg>
      <span style="color:<?php echo $__org_wallet_balance < 1000 ? 'var(--error)' : 'var(--text)'; ?>"><?php echo fmt_money($__org_wallet_balance); ?></span>
    </a>
    <div class="topbar-avatar"><?php echo h($__initials); ?></div>
  </div>
</div>

<div class="layout">
<main class="main-content">
