<?php
session_start();
require_once '../includes/config.php';

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

$id = $_POST['id'];
$officer_id = $_SESSION['user_id'];

$stmt = $pdo->prepare("SELECT * FROM leave_requests WHERE id = ? AND officer_id = ? AND status = 'pending'");
$stmt->execute([$id, $officer_id]);

if ($stmt->rowCount()) {
  $pdo->prepare("DELETE FROM leave_requests WHERE id = ?")->execute([$id]);
}

header("Location: leave.php?cancelled=1");
exit;
