<?php
/* ============================================================================
 *  company/hr_leave_reset.php — TEST HELPER (admin only).
 *  Deletes ALL leave records for THIS company so you can re-test from scratch.
 *  Only affects your own company. Safe to delete this file after testing.
 * ==========================================================================*/
ob_start();
session_start();
if (!isset($_SESSION["user_id"]) || ($_SESSION["role"] ?? '') !== "company_admin" || empty($_SESSION["company_id"])) {
    header("Location: ../auth/login.php"); exit;
}
require_once '../config/database.php';
$companyId   = (int)$_SESSION["company_id"];
$companyName = $_SESSION["company_name"] ?? "My Company";
function e($v){ return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }

$done = false; $deleted = 0; $err = '';
if ($_SERVER["REQUEST_METHOD"] === "POST" && ($_POST["confirm"] ?? '') === 'DELETE') {
    try {
        $st = $pdo->prepare("DELETE FROM hr_leave WHERE company_id=?");
        $st->execute([$companyId]);
        $deleted = $st->rowCount();
        $done = true;
    } catch (Throwable $ex) { $err = $ex->getMessage(); }
}

$count = 0;
try { $c = $pdo->prepare("SELECT COUNT(*) FROM hr_leave WHERE company_id=?"); $c->execute([$companyId]); $count = (int)$c->fetchColumn(); } catch (Throwable $ex) {}
?>
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>Reset leave records</title>
<style>
    body{font-family:Inter,Arial,sans-serif;background:#f0f4f8;color:#0f172a;margin:0;padding:24px;}
    .card{max-width:460px;margin:40px auto;background:#fff;border:1px solid #e2e8f0;border-radius:16px;box-shadow:0 12px 30px rgba(15,23,42,.08);padding:26px;}
    h1{font-size:20px;margin:0 0 6px;}
    p{color:#475569;font-size:14px;line-height:1.6;}
    .big{font-size:34px;font-weight:800;color:#0f172a;margin:8px 0;}
    .warn{background:#fef2f2;border:1px solid #fecaca;color:#b91c1c;border-radius:10px;padding:12px 14px;font-size:13px;font-weight:600;margin:16px 0;}
    .ok{background:#ecfdf5;border:1px solid #a7f3d0;color:#047857;border-radius:10px;padding:12px 14px;font-weight:700;margin:16px 0;}
    button{background:#dc2626;color:#fff;border:none;padding:13px 20px;border-radius:11px;font-weight:800;font-size:15px;cursor:pointer;width:100%;}
    button:hover{background:#b91c1c;}
    a{display:inline-block;margin-top:16px;color:#2563eb;text-decoration:none;font-weight:700;}
    code{background:#eef2f7;padding:1px 6px;border-radius:5px;}
</style></head><body>
<div class="card">
    <h1>🧹 Reset leave records</h1>
    <p><b><?= e($companyName) ?></b> only. This permanently deletes every leave request for your company so you can test fresh with all users.</p>

    <?php if ($done): ?>
        <div class="ok">✅ Deleted <?= (int)$deleted ?> leave record(s). You can now re-test.</div>
        <a href="hr_leave.php">→ Go to Leave Requests</a> &nbsp;·&nbsp; <a href="dashboard.php">Dashboard</a>
    <?php elseif ($err): ?>
        <div class="warn">Error: <?= e($err) ?></div>
        <a href="dashboard.php">← Back</a>
    <?php else: ?>
        <p>Current leave records for your company:</p>
        <div class="big"><?= $count ?></div>
        <div class="warn">⚠️ This cannot be undone. It removes all pending, approved and rejected leave for <b><?= e($companyName) ?></b>.</div>
        <form method="POST" onsubmit="return confirm('Delete ALL <?= $count ?> leave records for your company? This cannot be undone.');">
            <input type="hidden" name="confirm" value="DELETE">
            <button type="submit">🗑️ Delete all <?= $count ?> leave records</button>
        </form>
        <a href="dashboard.php">← Cancel</a>
    <?php endif; ?>
    <p style="margin-top:22px;font-size:12px;color:#94a3b8;">Delete this file (<code>company/hr_leave_reset.php</code>) after testing.</p>
</div>
</body></html>