<?php
// meevakeel/lawyer/dashboard.php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/auth.php';
requireLawyerLogin();

$db     = getDB();
$lid    = $_SESSION['lawyer_id'];
$lawyer = getCurrentLawyer();

// Stats
$total     = $db->prepare("SELECT COUNT(*) FROM appointments WHERE lawyer_id=?"); $total->execute([$lid]); $total = (int)$total->fetchColumn();
$pending   = $db->prepare("SELECT COUNT(*) FROM appointments WHERE lawyer_id=? AND status='pending'"); $pending->execute([$lid]); $pending = (int)$pending->fetchColumn();
$confirmed = $db->prepare("SELECT COUNT(*) FROM appointments WHERE lawyer_id=? AND status='confirmed'"); $confirmed->execute([$lid]); $confirmed = (int)$confirmed->fetchColumn();
$completed = $db->prepare("SELECT COUNT(*) FROM appointments WHERE lawyer_id=? AND status='completed'"); $completed->execute([$lid]); $completed = (int)$completed->fetchColumn();

$earnings = $db->prepare("SELECT COALESCE(SUM(lawyer_payout),0) FROM payments WHERE lawyer_id=? AND status='completed'");
$earnings->execute([$lid]); $earnings = (float)$earnings->fetchColumn();

// Recent appointments
$recent = $db->prepare("
    SELECT a.*, u.name AS client_name, u.mobile AS client_mobile
    FROM appointments a JOIN users u ON a.user_id=u.id
    WHERE a.lawyer_id=? ORDER BY a.created_at DESC LIMIT 8");
$recent->execute([$lid]);
$appointments = $recent->fetchAll();

$pageTitle = 'Lawyer Dashboard — MeeVakeel';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title><?= $pageTitle ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700;800&family=DM+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="/meevakeel/assets/css/style.css">
<style>
body{background:var(--off-white);font-family:var(--font-body);}
.dash-sidebar{width:240px;min-height:100vh;background:var(--primary-dark);position:fixed;top:0;left:0;z-index:200;overflow-y:auto;}
.dash-main{margin-left:240px;min-height:100vh;}
.dash-topbar{background:white;padding:14px 28px;border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;position:sticky;top:0;z-index:100;box-shadow:0 2px 8px rgba(0,0,0,.05);}
.dash-content{padding:28px;}
.dash-nav-link{display:flex;align-items:center;gap:12px;padding:11px 20px;color:rgba(255,255,255,.65);font-size:.87rem;font-weight:500;border-radius:8px;margin:2px 10px;text-decoration:none;transition:var(--transition);}
.dash-nav-link:hover,.dash-nav-link.active{background:rgba(212,175,55,.15);color:var(--gold);}
.dash-nav-link i{width:18px;text-align:center;}
.nav-section-label{font-size:.68rem;color:rgba(255,255,255,.3);font-weight:700;letter-spacing:.1em;text-transform:uppercase;padding:16px 20px 6px;}
.stat-card{background:white;border-radius:14px;padding:22px;border:1px solid var(--border);box-shadow:var(--shadow);display:flex;align-items:center;gap:16px;}
.stat-icon{width:52px;height:52px;border-radius:12px;display:flex;align-items:center;justify-content:center;font-size:1.3rem;}
.stat-num{font-size:1.7rem;font-weight:800;color:var(--primary);font-family:var(--font-heading);}
.stat-lbl{font-size:.78rem;color:var(--text-light);font-weight:500;}
.badge-pending{background:rgba(255,193,7,.15);color:#b8860b;padding:4px 10px;border-radius:50px;font-size:.72rem;font-weight:700;}
.badge-confirmed{background:rgba(40,167,69,.12);color:#1a7a34;padding:4px 10px;border-radius:50px;font-size:.72rem;font-weight:700;}
.badge-completed{background:rgba(10,35,66,.1);color:var(--primary);padding:4px 10px;border-radius:50px;font-size:.72rem;font-weight:700;}
.badge-cancelled,.badge-rejected{background:rgba(220,53,69,.1);color:#c0392b;padding:4px 10px;border-radius:50px;font-size:.72rem;font-weight:700;}
@media(max-width:768px){.dash-sidebar{display:none;}.dash-main{margin-left:0;}}
</style>
</head>
<body>

<!-- Sidebar -->
<div class="dash-sidebar">
  <div style="padding:22px 20px;border-bottom:1px solid rgba(255,255,255,.08);">
    <div style="font-family:var(--font-heading);font-size:1.3rem;font-weight:800;color:white;"><span style="color:var(--gold);">Nyaya</span> Guru</div>
    <div style="font-size:.72rem;color:rgba(255,255,255,.4);margin-top:2px;">Lawyer Portal</div>
  </div>

  <!-- Lawyer info -->
  <div style="padding:18px 20px;border-bottom:1px solid rgba(255,255,255,.06);display:flex;align-items:center;gap:12px;">
    <div style="width:40px;height:40px;background:var(--gold);border-radius:50%;display:flex;align-items:center;justify-content:center;font-weight:800;color:var(--primary);font-family:var(--font-heading);"><?= strtoupper(substr($lawyer['name'],0,1)) ?></div>
    <div>
      <div style="color:white;font-size:.85rem;font-weight:600;"><?= htmlspecialchars(explode(' ',$lawyer['name'])[0]) ?></div>
      <div style="font-size:.7rem;color:<?= $lawyer['verification_status']==='approved'?'#4caf50':'rgba(255,193,7,.8)' ?>;">
        <?= $lawyer['verification_status']==='approved' ? '✅ Verified' : '⏳ '.ucfirst($lawyer['verification_status']) ?>
      </div>
    </div>
  </div>

  <div class="nav-section-label">Main</div>
  <?php
  $navItems = [
    [SITE_URL . '/lawyer/dashboard.php',     'fas fa-tachometer-alt', 'Dashboard'],
    [SITE_URL . '/lawyer/appointments.php',  'fas fa-calendar-check', 'Appointments'],
    [SITE_URL . '/lawyer/availability.php',  'fas fa-clock',          'Availability'],
];
  foreach ($navItems as [$url,$icon,$label]):
    $active = strpos($_SERVER['REQUEST_URI'], $url) !== false ? 'active' : '';
  ?>
  <a href="<?= $url ?>" class="dash-nav-link <?= $active ?>">
    <i class="<?= $icon ?>"></i> <?= $label ?>
  </a>
  <?php endforeach; ?>

  <div class="nav-section-label">Account</div>

<a href="<?= SITE_URL . '/lawyer/profile.php' ?>" class="dash-nav-link">
    <i class="fas fa-user-edit"></i> My Profile
</a>

<a href="<?= SITE_URL ?>/lawyer/logout.php" class="dash-nav-link" style="color:rgba(220,53,69,.8);">
    <i class="fas fa-sign-out-alt"></i> Logout
</a>

</div>

<!-- Main -->
<div class="dash-main">
  <div class="dash-topbar">
    <div>
      <h5 style="margin:0;font-size:1rem;color:var(--primary);">Dashboard</h5>
      <span style="font-size:.78rem;color:var(--text-light);">Welcome back, <?= htmlspecialchars(explode(' ',$lawyer['name'])[0]) ?>!</span>
    </div>
    <div style="display:flex;align-items:center;gap:14px;">
      <?php if ($lawyer['verification_status'] !== 'approved'): ?>
      <span style="background:rgba(255,193,7,.15);color:#b8860b;padding:6px 14px;border-radius:50px;font-size:.78rem;font-weight:700;">
        <i class="fas fa-clock me-1"></i>Profile Under Review
      </span>
      <?php endif; ?>
      <a href="logout.php" style="color:var(--text-light);font-size:.82rem;">Logout</a>
    </div>
  </div>

  <div class="dash-content">

    <!-- Stat Cards -->
    <div class="row g-3 mb-4">
      <?php
      $stats = [
        ['bg'=>'rgba(10,35,66,.1)',    'color'=>'var(--primary)', 'icon'=>'fas fa-calendar', 'num'=>$total,     'lbl'=>'Total Bookings'],
        ['bg'=>'rgba(255,193,7,.12)',  'color'=>'#b8860b',        'icon'=>'fas fa-hourglass','num'=>$pending,   'lbl'=>'Pending'],
        ['bg'=>'rgba(40,167,69,.1)',   'color'=>'#1a7a34',        'icon'=>'fas fa-check-circle','num'=>$completed,'lbl'=>'Completed'],
        ['bg'=>'rgba(212,175,55,.15)', 'color'=>'var(--gold-dark)','icon'=>'fas fa-rupee-sign','num'=>'₹'.number_format($earnings,0),'lbl'=>'Total Earned'],
      ];
      foreach ($stats as $s): ?>
      <div class="col-6 col-md-3">
        <div class="stat-card">
          <div class="stat-icon" style="background:<?= $s['bg'] ?>;color:<?= $s['color'] ?>;"><i class="<?= $s['icon'] ?>"></i></div>
          <div>
            <div class="stat-num"><?= $s['num'] ?></div>
            <div class="stat-lbl"><?= $s['lbl'] ?></div>
          </div>
        </div>
      </div>
      <?php endforeach; ?>
    </div>

    <!-- Quick links -->
    <div class="row g-3 mb-4">
      <div class="col-md-4">
        <a href="appointments.php" style="display:flex;align-items:center;gap:14px;background:white;border-radius:14px;padding:20px;border:1px solid var(--border);text-decoration:none;transition:var(--transition);"
           onmouseover="this.style.borderColor='var(--gold)'" onmouseout="this.style.borderColor='var(--border)'">
          <div style="width:46px;height:46px;background:linear-gradient(135deg,var(--primary),#1a3a6b);border-radius:12px;display:flex;align-items:center;justify-content:center;color:var(--gold);font-size:1.1rem;"><i class="fas fa-calendar-check"></i></div>
          <div><div style="font-weight:700;color:var(--primary);font-size:.92rem;">View Appointments</div><div style="font-size:.78rem;color:var(--text-light);"><?= $pending ?> pending</div></div>
        </a>
      </div>
      <div class="col-md-4">
        <a href="availability.php" style="display:flex;align-items:center;gap:14px;background:white;border-radius:14px;padding:20px;border:1px solid var(--border);text-decoration:none;transition:var(--transition);"
           onmouseover="this.style.borderColor='var(--gold)'" onmouseout="this.style.borderColor='var(--border)'">
          <div style="width:46px;height:46px;background:linear-gradient(135deg,var(--gold),var(--gold-dark));border-radius:12px;display:flex;align-items:center;justify-content:center;color:var(--primary);font-size:1.1rem;"><i class="fas fa-clock"></i></div>
          <div><div style="font-weight:700;color:var(--primary);font-size:.92rem;">Set Availability</div><div style="font-size:.78rem;color:var(--text-light);">Manage your slots</div></div>
        </a>
      </div>
      <div class="col-md-4">
        <a href="profile.php" style="display:flex;align-items:center;gap:14px;background:white;border-radius:14px;padding:20px;border:1px solid var(--border);text-decoration:none;transition:var(--transition);"
           onmouseover="this.style.borderColor='var(--gold)'" onmouseout="this.style.borderColor='var(--border)'">
          <div style="width:46px;height:46px;background:rgba(10,35,66,.08);border-radius:12px;display:flex;align-items:center;justify-content:center;color:var(--primary);font-size:1.1rem;"><i class="fas fa-user-edit"></i></div>
          <div><div style="font-weight:700;color:var(--primary);font-size:.92rem;">Edit Profile</div><div style="font-size:.78rem;color:var(--text-light);">Update info & photo</div></div>
        </a>
      </div>
    </div>

    <!-- Recent Appointments -->
    <div style="background:white;border-radius:16px;border:1px solid var(--border);box-shadow:var(--shadow);overflow:hidden;">
      <div style="padding:18px 22px;border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;">
        <h6 style="margin:0;font-size:.95rem;color:var(--primary);">Recent Appointments</h6>
        <a href="/lawyer/appointments.php" style="font-size:.8rem;color:var(--primary);font-weight:600;">View All →</a>
      </div>
      <?php if (empty($appointments)): ?>
      <div style="text-align:center;padding:40px;color:var(--text-light);font-size:.88rem;">No appointments yet.</div>
      <?php else: ?>
      <div style="overflow-x:auto;">
        <table class="table-ng table mb-0">
          <thead><tr>
            <th>Booking ID</th><th>Client</th><th>Type</th><th>Date</th><th>Status</th><th>Action</th>
          </tr></thead>
          <tbody>
          <?php foreach ($appointments as $a): ?>
          <tr>
            <td><span style="font-size:.78rem;font-weight:700;color:var(--primary);"><?= $a['booking_id'] ?></span></td>
            <td>
              <div style="font-weight:600;font-size:.85rem;"><?= htmlspecialchars($a['client_name']) ?></div>
              <div style="font-size:.72rem;color:var(--text-light);"><?= $a['client_mobile'] ?></div>
            </td>
            <td><span style="text-transform:capitalize;font-size:.82rem;"><?= $a['consultation_type'] ?></span></td>
            <td style="font-size:.82rem;"><?= $a['appointment_date'] ? date('d M Y', strtotime($a['appointment_date'])) : '—' ?></td>
            <td><span class="badge-<?= $a['status'] ?>"><?= ucfirst($a['status']) ?></span></td>
            <td>
              <?php if ($a['status'] === 'pending'): ?>
              <div style="display:flex;gap:6px;">
                <a href="/lawyer/update-appointment.php?id=<?= $a['id'] ?>&action=confirm" style="background:rgba(40,167,69,.1);color:#1a7a34;padding:5px 12px;border-radius:8px;font-size:.75rem;font-weight:700;text-decoration:none;">Confirm</a>
                <a href="/lawyer/update-appointment.php?id=<?= $a['id'] ?>&action=reject"  style="background:rgba(220,53,69,.1);color:#c0392b;padding:5px 12px;border-radius:8px;font-size:.75rem;font-weight:700;text-decoration:none;">Reject</a>
              </div>
              <?php elseif ($a['status'] === 'confirmed'): ?>
              <a href="/lawyer/update-appointment.php?id=<?= $a['id'] ?>&action=complete" style="background:rgba(10,35,66,.08);color:var(--primary);padding:5px 12px;border-radius:8px;font-size:.75rem;font-weight:700;text-decoration:none;">Mark Done</a>
              <?php else: ?>
              <span style="font-size:.75rem;color:var(--text-light);">—</span>
              <?php endif; ?>
            </td>
          </tr>
          <?php endforeach; ?>
          </tbody>
        </table>
      </div>
      <?php endif; ?>
    </div>

  </div><!-- /dash-content -->
</div><!-- /dash-main -->

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>