<?php
$page_title = 'Leave Management';
require_once __DIR__ . '/header.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['apply_leave'])) {
    $type = $_POST['leave_type'];
    $start = $_POST['start_date'];
    $end = $_POST['end_date'];
    $reason = trim($_POST['reason']);
    $db->prepare("INSERT INTO emp_leaves (employee_id, leave_type, start_date, end_date, reason, status) VALUES (?, ?, ?, ?, ?, 'Pending')")
       ->execute([$emp_id, $type, $start, $end, $reason]);
    redirect(SITE_URL . '/employee/leaves.php?success=1');
}

$leaves = $db->query("SELECT * FROM emp_leaves WHERE employee_id={$emp_id} ORDER BY created_at DESC")->fetchAll();
?>

<?php if(isset($_GET['success'])): ?>
<div style="background:#dcfce7;color:#166534;padding:12px 16px;border-radius:8px;margin-bottom:20px;border:1px solid #86efac;font-weight:500;">Leave application submitted successfully. Pending approval.</div>
<?php endif; ?>

<div class="grid-2">
  <div class="card">
    <h3 style="margin:0 0 16px;color:#0f172a;">Apply for Leave</h3>
    <form method="POST" style="display:flex;flex-direction:column;gap:12px;">
      <input type="hidden" name="apply_leave" value="1">
      <div>
        <label style="display:block;font-size:.85rem;font-weight:600;margin-bottom:4px;color:#334155;">Leave Type</label>
        <select name="leave_type" required style="width:100%;padding:10px;border:1px solid #cbd5e1;border-radius:6px;outline:none;">
          <option value="Casual">Casual Leave</option>
          <option value="Sick">Sick Leave</option>
          <option value="Earned">Earned Leave</option>
          <option value="Unpaid">Unpaid Leave</option>
        </select>
      </div>
      <div style="display:flex;gap:12px;">
        <div style="flex:1;">
          <label style="display:block;font-size:.85rem;font-weight:600;margin-bottom:4px;color:#334155;">Start Date</label>
          <input type="date" name="start_date" required min="<?= date('Y-m-d') ?>" style="width:100%;padding:10px;border:1px solid #cbd5e1;border-radius:6px;outline:none;">
        </div>
        <div style="flex:1;">
          <label style="display:block;font-size:.85rem;font-weight:600;margin-bottom:4px;color:#334155;">End Date</label>
          <input type="date" name="end_date" required min="<?= date('Y-m-d') ?>" style="width:100%;padding:10px;border:1px solid #cbd5e1;border-radius:6px;outline:none;">
        </div>
      </div>
      <div>
        <label style="display:block;font-size:.85rem;font-weight:600;margin-bottom:4px;color:#334155;">Reason</label>
        <textarea name="reason" rows="3" required style="width:100%;padding:10px;border:1px solid #cbd5e1;border-radius:6px;outline:none;resize:vertical;"></textarea>
      </div>
      <button type="submit" style="background:#2557d6;color:#fff;border:none;padding:12px;border-radius:6px;font-weight:600;cursor:pointer;margin-top:8px;">Submit Application</button>
    </form>
  </div>

  <div class="card">
    <h3 style="margin:0 0 16px;color:#0f172a;">Leave History</h3>
    <div style="max-height:400px;overflow-y:auto;padding-right:8px;">
      <?php foreach($leaves as $l): ?>
      <?php 
        $c = 'info';
        if($l['status']==='Approved') $c='success';
        if($l['status']==='Rejected') $c='danger';
        if($l['status']==='Pending') $c='warning';
      ?>
      <div style="border:1px solid #e2e8f0;border-radius:8px;padding:12px;margin-bottom:12px;background:#f8fafc;">
        <div style="display:flex;justify-content:space-between;margin-bottom:8px;">
          <strong style="color:#0f172a;"><?= h($l['leave_type']) ?> Leave</strong>
          <span class="badge badge-<?= $c ?>" style="padding:4px 8px;border-radius:4px;font-size:.75rem;"><?= h($l['status']) ?></span>
        </div>
        <div style="font-size:.85rem;color:#64748b;margin-bottom:4px;">
          <i class="fas fa-calendar-alt" style="margin-right:4px;"></i> <?= date('d M', strtotime($l['start_date'])) ?> to <?= date('d M Y', strtotime($l['end_date'])) ?>
        </div>
        <div style="font-size:.85rem;color:#475569;font-style:italic;">"<?= h($l['reason']) ?>"</div>
      </div>
      <?php endforeach; ?>
      <?php if(!$leaves): ?>
      <p style="text-align:center;color:#64748b;font-size:.9rem;padding:20px;">No leave requests found.</p>
      <?php endif; ?>
    </div>
  </div>
</div>

<?php require_once __DIR__ . '/footer.php'; ?>
