<?php
require_once 'includes/config.php';
require_once 'includes/functions.php';
require_once __DIR__ . '/includes/module_helpers.php';
require_once __DIR__ . '/includes/settings_helpers.php';

session_start();
if (empty($_SESSION['authenticated'])) {
    header('Location: signin.php');
    exit();
}
require_once __DIR__ . '/db.php';

settings_ensure_salary_hold_tables($conn);

$context = payroll_org_context($conn);
$orgId = (int) ($context['org_id'] ?? 0);
$orgName = $context['org_name'] ?? 'demo';
$displayName = $context['display_name'] ?? $orgName;
$employees = payroll_load_employees($conn);

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['quick_action'], $_POST['employee_id'])) {
    $employeeId = (int) $_POST['employee_id'];
    $quickAction = (string) $_POST['quick_action'];

    if ($employeeId > 0 && in_array($quickAction, ['release', 'hold'], true)) {
        $currentMonth = date('Y-m');
        $holdStatus = $quickAction === 'release' ? 'released' : 'held';
        $approvalStatus = $quickAction === 'release' ? 'approved' : 'pending';
        $releaseMonth = $quickAction === 'release' ? $currentMonth : null;
        $approvedBy = $quickAction === 'release' ? (string) ($_SESSION['username'] ?? $_SESSION['user'] ?? 'system') : null;
        $approvedAt = $quickAction === 'release' ? date('Y-m-d H:i:s') : null;
        $releasedBy = $quickAction === 'release' ? (string) ($_SESSION['username'] ?? $_SESSION['user'] ?? 'system') : null;
        $releasedAt = $quickAction === 'release' ? date('Y-m-d H:i:s') : null;
        $holdReason = $quickAction === 'release' ? 'Marked as released from Salary Holds page.' : 'Marked as held from Salary Holds page.';

        $existingStmt = $conn->prepare(
            "SELECT id
             FROM employee_salary_holds
             WHERE organization_id = ? AND employee_id = ?
             ORDER BY updated_at DESC, id DESC
             LIMIT 1"
        );
        $existingHoldId = 0;
        if ($existingStmt) {
            $existingStmt->bind_param('ii', $orgId, $employeeId);
            $existingStmt->execute();
            $existingResult = $existingStmt->get_result();
            if ($existingResult && ($existingRow = $existingResult->fetch_assoc())) {
                $existingHoldId = (int) ($existingRow['id'] ?? 0);
            }
            $existingStmt->close();
        }

        $ok = false;
        if ($existingHoldId > 0) {
            $updateStmt = $conn->prepare(
                "UPDATE employee_salary_holds
                 SET salary_month_from = ?, salary_month_to = ?, release_month = ?, hold_status = ?, approval_status = ?, hold_reason = ?, created_by = ?, approved_by = ?, approved_at = ?, released_by = ?, released_at = ?
                 WHERE id = ? AND organization_id = ?"
            );
            if ($updateStmt) {
                $createdBy = (string) ($_SESSION['username'] ?? $_SESSION['user'] ?? 'system');
                $updateStmt->bind_param(
                    'sssssssssssii',
                    $currentMonth,
                    $currentMonth,
                    $releaseMonth,
                    $holdStatus,
                    $approvalStatus,
                    $holdReason,
                    $createdBy,
                    $approvedBy,
                    $approvedAt,
                    $releasedBy,
                    $releasedAt,
                    $existingHoldId,
                    $orgId
                );
                $ok = $updateStmt->execute();
                $updateStmt->close();
            }
        } else {
            $ok = settings_save_salary_hold_entry(
                $conn,
                $orgId,
                $employeeId,
                $currentMonth,
                $currentMonth,
                $releaseMonth,
                $holdStatus,
                $approvalStatus,
                $holdReason,
                (string) ($_SESSION['username'] ?? $_SESSION['user'] ?? 'system'),
                null,
                $approvedBy,
                $releasedBy,
                $approvedAt,
                $releasedAt
            );
        }

        $_SESSION['flash_ok'] = $ok
            ? ($quickAction === 'release' ? 'Salary marked as released successfully.' : 'Salary marked as held successfully.')
            : 'Unable to update salary status.';
    }

    header('Location: salary_holds.php');
    exit();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['hold_action'], $_POST['hold_id'])) {
    $holdId = (int) $_POST['hold_id'];
    $action = (string) $_POST['hold_action'];

    $stmt = $conn->prepare("SELECT * FROM employee_salary_holds WHERE id = ? AND organization_id = ? LIMIT 1");
    if ($stmt) {
        $stmt->bind_param('ii', $holdId, $orgId);
        $stmt->execute();
        $current = $stmt->get_result()->fetch_assoc();
        $stmt->close();

        if ($current) {
            $holdStatus = (string) ($current['hold_status'] ?? 'pending');
            $approvalStatus = (string) ($current['approval_status'] ?? 'pending');
            $approvedBy = $current['approved_by'] ?? null;
            $approvedAt = $current['approved_at'] ?? null;
            $releasedBy = $current['released_by'] ?? null;
            $releasedAt = $current['released_at'] ?? null;
            $releaseMonth = $current['release_month'] ?? null;

            if ($action === 'approve') {
                $holdStatus = 'held';
                $approvalStatus = 'approved';
                $approvedBy = (string) ($_SESSION['username'] ?? $_SESSION['user'] ?? 'system');
                $approvedAt = date('Y-m-d H:i:s');
            } elseif ($action === 'release') {
                $holdStatus = 'released';
                $approvalStatus = 'approved';
                $releasedBy = (string) ($_SESSION['username'] ?? $_SESSION['user'] ?? 'system');
                $releasedAt = date('Y-m-d H:i:s');
                if (trim((string) $releaseMonth) === '') {
                    $releaseMonth = date('Y-m');
                }
            } elseif ($action === 'hold') {
                $holdStatus = 'held';
                $approvalStatus = 'pending';
            } elseif ($action === 'reject') {
                $holdStatus = 'pending';
                $approvalStatus = 'rejected';
            }

            $update = $conn->prepare(
                "UPDATE employee_salary_holds
                 SET hold_status = ?, approval_status = ?, release_month = ?, approved_by = ?, approved_at = ?, released_by = ?, released_at = ?
                 WHERE id = ? AND organization_id = ?"
            );
            if ($update) {
                $update->bind_param(
                    'sssssssii',
                    $holdStatus,
                    $approvalStatus,
                    $releaseMonth,
                    $approvedBy,
                    $approvedAt,
                    $releasedBy,
                    $releasedAt,
                    $holdId,
                    $orgId
                );
                if ($update->execute()) {
                    $_SESSION['flash_ok'] = 'Salary hold updated successfully.';
                } else {
                    $_SESSION['flash_err'] = 'Unable to update salary hold.';
                }
                $update->close();
            } else {
                $_SESSION['flash_err'] = 'Unable to prepare salary hold update.';
            }
        } else {
            $_SESSION['flash_err'] = 'Salary hold entry not found.';
        }
    }

    header('Location: salary_holds.php');
    exit();
}

$rows = settings_get_salary_hold_notifications($conn, $orgId);
$activeRows = $rows;
$holdEmployeeIds = [];
foreach ($rows as $row) {
    $holdEmployeeIds[(int) ($row['hold_employee_id'] ?? $row['employee_id'] ?? 0)] = true;
}
$employeeHoldCandidates = [];
foreach ($employees as $employee) {
    $employeeId = (int) ($employee['id'] ?? 0);
    $employeeHoldCandidates[] = [
        'id' => $employeeId,
        'name' => payroll_employee_name($employee),
        'code' => payroll_employee_code($employee),
        'designation' => trim((string) ($employee['designation'] ?? $employee['job_title'] ?? '')),
        'email' => trim((string) ($employee['work_email'] ?? $employee['email'] ?? $employee['office_email'] ?? '')),
        'salary_link' => 'employees.php?step=salary&emp_id=' . urlencode((string) $employeeId),
    ];
}
$pendingEmployees = array_values(array_filter($employeeHoldCandidates, static function (array $employee) use ($holdEmployeeIds): bool {
    return empty($holdEmployeeIds[(int) ($employee['id'] ?? 0)]);
}));

$pendingCount = 0;
$heldCount = 0;
$releasedCount = 0;
foreach ($rows as $row) {
    $status = (string) ($row['hold_status'] ?? 'pending');
    if ($status === 'released') {
        $releasedCount++;
    } elseif ($status === 'held') {
        $heldCount++;
    } else {
        $pendingCount++;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="<?php echo billing_escape(billing_base_href()); ?>">
<title>Salary Holds | Payroll</title>
<link rel="stylesheet" href="dashboard.css">
<link rel="stylesheet" href="assets/font-inter.css">
<link rel="stylesheet" href="assets/fontawesome/css/all.min.css">
<style>
body.dash-page.salary-hold-page .dash-main{background:#f4f6fb;}
.hold-shell{padding:24px 28px 32px;}
.hold-hero{
    background:linear-gradient(135deg,#ffffff 0%,#f8fbff 100%);
    border:1px solid #e5e7eb;
    border-radius:18px;
    padding:22px;
    box-shadow:0 14px 30px rgba(15,23,42,.06);
    margin-bottom:18px;
}
.hold-hero-top{display:flex;align-items:flex-start;justify-content:space-between;gap:16px;flex-wrap:wrap;}
.hold-title{font-size:28px;font-weight:800;color:#0f172a;margin:0 0 6px;}
.hold-sub{margin:0;color:#475569;line-height:1.5;max-width:820px;}
.hold-actions{display:flex;align-items:center;gap:10px;flex-wrap:wrap;}
.hold-btn{
    display:inline-flex;align-items:center;gap:8px;
    padding:10px 14px;border-radius:12px;border:1px solid #dbe3f0;
    background:#fff;color:#1d4ed8;font-weight:700;
}
.hold-stats{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:12px;margin-top:16px;}
.hold-stat{background:#fff;border:1px solid #e5e7eb;border-radius:14px;padding:14px 16px;}
.hold-stat .label{font-size:12px;text-transform:uppercase;letter-spacing:.04em;color:#64748b;font-weight:700;}
.hold-stat .value{font-size:24px;font-weight:800;color:#111827;margin-top:4px;}
.hold-card{
    background:#fff;border:1px solid #e5e7eb;border-radius:18px;overflow:hidden;
    box-shadow:0 12px 26px rgba(15,23,42,.05);
}
.hold-card-head{
    padding:14px 18px;border-bottom:1px solid #e5e7eb;
    display:flex;align-items:center;justify-content:space-between;gap:12px;flex-wrap:wrap;
}
.hold-card-head strong{font-size:16px;}
.hold-table{width:100%;border-collapse:collapse;}
.hold-table th,.hold-table td{padding:14px 18px;border-top:1px solid #eef2f7;vertical-align:top;}
.hold-table th{font-size:12px;text-transform:uppercase;letter-spacing:.04em;color:#64748b;background:#f8fafc;text-align:left;}
.hold-employee{font-weight:700;color:#111827;}
.hold-code{font-size:12px;color:#64748b;margin-top:2px;}
.hold-employee,
.hold-employee-card-name,
.hold-employee-card-code{
    text-decoration:none;
    display:block;
}
.hold-employee:hover,
.hold-employee-card-name:hover,
.hold-employee-card-code:hover{
    color:#1d4ed8;
}
.hold-period{font-weight:700;color:#1f2937;}
.hold-meta{font-size:12px;color:#64748b;margin-top:4px;line-height:1.45;}
.hold-actions-cell{display:flex;flex-wrap:wrap;gap:8px;}
.hold-action-btn{
    border:1px solid #dbe3f0;background:#fff;border-radius:10px;
    padding:8px 10px;font-weight:700;color:#1d4ed8;cursor:pointer;
}
.hold-action-btn.danger{color:#b91c1c;}
.hold-empty{
    padding:32px;text-align:center;color:#64748b;
}
.hold-empty i{font-size:28px;color:#cbd5e1;margin-bottom:10px;}
.hold-empty-employee-list{
    display:grid;
    grid-template-columns:repeat(auto-fit,minmax(240px,1fr));
    gap:12px;
    margin-top:18px;
    text-align:left;
}
.hold-employee-card{
    border:1px solid #e5e7eb;
    border-radius:14px;
    padding:14px 16px;
    background:#fff;
    box-shadow:0 8px 18px rgba(15,23,42,.04);
}
.hold-employee-card-top{
    display:flex;
    align-items:flex-start;
    justify-content:space-between;
    gap:12px;
}
.hold-employee-card-name{
    font-weight:800;
    color:#111827;
    font-size:15px;
}
.hold-employee-card-code{
    margin-top:3px;
    font-size:12px;
    color:#64748b;
}
.hold-employee-card-meta{
    margin-top:10px;
    font-size:12px;
    color:#64748b;
    line-height:1.45;
}
.hold-employee-card-actions{
    margin-top:14px;
    display:flex;
    flex-wrap:wrap;
    gap:8px;
}
.hold-empty-action{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    padding:8px 12px;
    border-radius:10px;
    border:1px solid #dbe3f0;
    background:#fff;
    color:#1d4ed8;
    font-weight:700;
    text-decoration:none;
    cursor:pointer;
    appearance:none;
}
.hold-pending-section{
    margin-top:18px;
    background:#fff;
    border:1px solid #e5e7eb;
    border-radius:18px;
    box-shadow:0 12px 26px rgba(15,23,42,.05);
    overflow:hidden;
}
.hold-pending-head{
    padding:14px 18px;
    border-bottom:1px solid #e5e7eb;
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:12px;
    flex-wrap:wrap;
}
.hold-pending-title{
    font-size:16px;
    font-weight:800;
    color:#111827;
}
.hold-pending-count{
    color:#64748b;
    font-size:13px;
}
.hold-pending-body{
    padding:16px 18px 18px;
}
@media (max-width: 900px){
    .hold-shell{padding:18px;}
    .hold-stats{grid-template-columns:1fr;}
    .hold-table thead{display:none;}
    .hold-table, .hold-table tbody, .hold-table tr, .hold-table td{display:block;width:100%;}
    .hold-table tr{border-top:1px solid #eef2f7;padding:6px 0;}
    .hold-table td{border-top:none;padding:10px 18px;}
    .hold-table td::before{content:attr(data-label);display:block;font-size:11px;text-transform:uppercase;color:#64748b;font-weight:700;margin-bottom:6px;}
}
</style>
</head>
<body class="dash-page salary-hold-page">
<div class="dash-layout">
    <aside class="dash-sidebar">
        <div class="sidebar-brand">
            <img src="images/zoho-payroll-logo.svg" class="logo" alt="Zoho Payroll">
        </div>
        <div class="sidebar-scroll">
            <div class="sidebar-section">
                <div class="getting-started">
                    <div class="gs-top"><i class="fa-solid fa-rocket-launch"></i><span>Getting Started</span></div>
                    <div class="progress-bar"><div class="progress-fill"></div></div>
                </div>
            </div>
            <nav class="sidebar-nav">
                <a class="nav-item" href="dashboard.php"><i class="fa-solid fa-gauge-high"></i><span>Dashboard</span></a>
                <a class="nav-item" href="employees.php"><i class="fa-solid fa-users"></i><span>Employees</span></a>
                <a class="nav-item" href="payruns.php"><i class="fa-solid fa-money-bill-wave"></i><span>Pay Runs</span></a>
                <div class="nav-group open">
                    <button class="nav-item btn" type="button"><i class="fa-regular fa-square-check"></i><span>Approvals</span></button>
                    <div class="nav-sub">
                        <a class="nav-sub-item" href="reimbursements.php">Reimbursements</a>
                        <a class="nav-sub-item" href="proof_of_investments.php">Proof Of Investments</a>
                        <a class="nav-sub-item" href="salary_revision.php">Salary Revision</a>
                        <a class="nav-sub-item active" href="salary_holds.php">Salary Holds</a>
                    </div>
                </div>
                <a class="nav-item" href="form16.php"><i class="fa-regular fa-file-lines"></i><span>Form 16</span></a>
                <a class="nav-item" href="loans.php"><i class="fa-solid fa-hand-holding-dollar"></i><span>Loans</span></a>
                <a class="nav-item" href="giving.php"><i class="fa-solid fa-gift"></i><span>Giving</span></a>
                <a class="nav-item" href="documents.php"><i class="fa-regular fa-folder-open"></i><span>Documents</span></a>
                <a class="nav-item" href="reports.php"><i class="fa-solid fa-chart-pie"></i><span>Reports</span></a>
                <a class="nav-item" href="settings.php"><i class="fa-solid fa-gear"></i><span>Settings</span></a>
            </nav>
        </div>
        <div class="sidebar-footer">
            <a class="support-link" href="support.php">Contact Support</a>
        </div>
    </aside>

    <main class="dash-main">
        <header class="dash-topbar">
            <div class="search-box">
                <i class="fa-solid fa-magnifying-glass search-icon"></i>
                <input type="text" placeholder="Search in Employee">
            </div>
            <div class="topbar-actions">
                <span class="trial">Signed in as <?php echo htmlspecialchars($displayName); ?></span>
                <a class="pill ghost org-toggle" href="dashboard.php?drawer=org" data-tooltip="Organisation menu"><span class="pill-label"><?php echo htmlspecialchars($orgName ?: 'demo'); ?></span><i class="fa-solid fa-angle-down"></i></a>
                <a class="top-icon icon-btn notif-link" href="dashboard.php?drawer=notif" data-tooltip="Notifications"><i class="fa-regular fa-bell"></i></a>
                <a class="top-icon top-link" href="settings.php" data-tooltip="Settings"><i class="fa-solid fa-gear"></i></a>
                <a class="top-icon icon-btn app-menu" href="dashboard.php?drawer=apps" data-tooltip="Apps"><i class="fa-solid fa-table-cells-large"></i></a>
                <a class="top-icon icon-btn top-avatar" href="dashboard.php?drawer=profile" data-tooltip="Profile"><i class="fa-regular fa-circle-user"></i></a>
            </div>
        </header>

        <div class="hold-shell">
            <?php if (!empty($_SESSION['flash_ok'])) { ?>
                <div class="alert-ok" style="margin-bottom:14px;"><i class="fa-solid fa-circle-check"></i><?php echo htmlspecialchars($_SESSION['flash_ok']); unset($_SESSION['flash_ok']); ?></div>
            <?php } ?>
            <?php if (!empty($_SESSION['flash_err'])) { ?>
                <div class="alert-err" style="margin-bottom:14px;"><?php echo htmlspecialchars($_SESSION['flash_err']); unset($_SESSION['flash_err']); ?></div>
            <?php } ?>

            <section class="hold-hero">
                <div class="hold-hero-top">
                    <div>
                        <h1 class="hold-title">Salary Holds</h1>
                        <p class="hold-sub">Track month-wise salary holds, remember which months are pending release, and keep approval updates visible in one place. Use this page when salary is held for March and April and released only in May, or when any employee needs a delayed payout.</p>
                    </div>
                    <div class="hold-actions">
                        <a class="hold-btn" href="employees.php?step=salary">
                            <i class="fa-solid fa-user-plus"></i>
                            Add Hold from Salary Section
                        </a>
                    </div>
                </div>
                <div class="hold-stats">
                    <div class="hold-stat"><div class="label">Pending</div><div class="value"><?php echo (int) $pendingCount; ?></div></div>
                    <div class="hold-stat"><div class="label">Held</div><div class="value"><?php echo (int) $heldCount; ?></div></div>
                    <div class="hold-stat"><div class="label">Released</div><div class="value"><?php echo (int) $releasedCount; ?></div></div>
                </div>
            </section>

            <section class="hold-card">
                <div class="hold-card-head">
                    <strong>Monthly Salary Hold Table</strong>
                    <span style="color:#64748b;font-size:13px;">Showing <?php echo count($activeRows); ?> record(s)</span>
                </div>
                <?php if ($activeRows): ?>
                    <table class="hold-table">
                        <thead>
                            <tr>
                                <th>Employee</th>
                                <th>Hold Period</th>
                                <th>Release Month</th>
                                <th>Status</th>
                                <th>Pending Months</th>
                                <th>Reason</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($activeRows as $row): ?>
                                <tr>
                                    <td data-label="Employee">
                                        <?php $employeeLinkId = (int) ($row['hold_employee_id'] ?? $row['employee_id'] ?? 0); ?>
                                        <a class="hold-employee" href="employees.php?emp_id=<?php echo (int) $employeeLinkId; ?>"><?php echo htmlspecialchars(payroll_employee_name($row)); ?></a>
                                        <div class="hold-code"><?php echo htmlspecialchars(payroll_employee_code($row)); ?></div>
                                    </td>
                                    <td data-label="Hold Period">
                                        <div class="hold-period"><?php echo htmlspecialchars(settings_salary_hold_month_label((string) ($row['salary_month_from'] ?? ''))); ?> to <?php echo htmlspecialchars(settings_salary_hold_month_label((string) ($row['salary_month_to'] ?? ''))); ?></div>
                                        <div class="hold-meta">Created <?php echo htmlspecialchars(date('d M Y', strtotime((string) ($row['created_at'] ?? 'now')))); ?></div>
                                    </td>
                                    <td data-label="Release Month"><?php echo htmlspecialchars(settings_salary_hold_month_label((string) ($row['release_month'] ?? ''))); ?></td>
                                    <td data-label="Status"><span class="salary-pill status-<?php echo htmlspecialchars((string) ($row['hold_status'] ?? 'pending')); ?>"><?php echo htmlspecialchars(ucfirst((string) ($row['hold_status'] ?? 'pending'))); ?></span></td>
                                    <td data-label="Pending Months"><?php echo (int) settings_salary_hold_pending_months($row); ?> month(s)</td>
                                    <td data-label="Reason"><?php echo htmlspecialchars((string) ($row['hold_reason'] ?? '-')); ?></td>
                                    <td data-label="Actions">
                                        <div class="hold-actions-cell">
                                            <form method="post">
                                                <input type="hidden" name="hold_id" value="<?php echo (int) $row['id']; ?>">
                                                <input type="hidden" name="hold_action" value="approve">
                                                <button class="hold-action-btn" type="submit">Approve</button>
                                            </form>
                                            <form method="post">
                                                <input type="hidden" name="hold_id" value="<?php echo (int) $row['id']; ?>">
                                                <input type="hidden" name="hold_action" value="release">
                                                <button class="hold-action-btn" type="submit">Release</button>
                                            </form>
                                            <form method="post">
                                                <input type="hidden" name="hold_id" value="<?php echo (int) $row['id']; ?>">
                                                <input type="hidden" name="hold_action" value="hold">
                                                <button class="hold-action-btn" type="submit">Hold</button>
                                            </form>
                                            <form method="post">
                                                <input type="hidden" name="hold_id" value="<?php echo (int) $row['id']; ?>">
                                                <input type="hidden" name="hold_action" value="reject">
                                                <button class="hold-action-btn danger" type="submit">Reject</button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php else: ?>
                    <div class="hold-empty">
                        <i class="fa-regular fa-bell"></i>
                        <div>No salary hold records yet.</div>
                        <div style="margin-top:6px;">Create one from the salary section on the employee page to see the month-wise hold and release table here.</div>
                    </div>
                <?php endif; ?>
            </section>

            <?php if (!empty($pendingEmployees)): ?>
                <section class="hold-pending-section">
                    <div class="hold-pending-head">
                        <div class="hold-pending-title">Employees Pending Salary Hold Setup</div>
                        <div class="hold-pending-count"><?php echo count($pendingEmployees); ?> employee(s) need a hold record</div>
                    </div>
                    <div class="hold-pending-body">
                        <div class="hold-empty-employee-list">
                            <?php foreach ($pendingEmployees as $employee): ?>
                                <div class="hold-employee-card">
                                    <div class="hold-employee-card-top">
                                        <div>
                                            <a class="hold-employee-card-name" href="employees.php?emp_id=<?php echo (int) $employee['id']; ?>"><?php echo htmlspecialchars($employee['name']); ?></a>
                                            <a class="hold-employee-card-code" href="employees.php?emp_id=<?php echo (int) $employee['id']; ?>"><?php echo htmlspecialchars($employee['code'] !== '' ? $employee['code'] : 'No employee code'); ?></a>
                                        </div>
                                        <span class="salary-pill status-pending">Pending</span>
                                    </div>
                                    <div class="hold-employee-card-meta">
                                        <?php if ($employee['designation'] !== ''): ?>
                                            <div><?php echo htmlspecialchars($employee['designation']); ?></div>
                                        <?php endif; ?>
                                        <?php if ($employee['email'] !== ''): ?>
                                            <div><?php echo htmlspecialchars($employee['email']); ?></div>
                                        <?php endif; ?>
                                    </div>
                                    <div class="hold-employee-card-actions">
                                        <a class="hold-empty-action" href="<?php echo htmlspecialchars($employee['salary_link']); ?>">Open Salary Section</a>
                                        <a class="hold-empty-action" href="employees.php?emp_id=<?php echo (int) $employee['id']; ?>">View Employee</a>
                                        <form method="post" action="salary_holds.php" style="display:inline;">
                                            <input type="hidden" name="employee_id" value="<?php echo (int) $employee['id']; ?>">
                                            <input type="hidden" name="quick_action" value="release">
                                            <button class="hold-empty-action" type="submit" name="quick_action_btn" value="release">Mark Released</button>
                                        </form>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </section>
            <?php endif; ?>
        </div>
    </main>
</div>
</body>
</html>

