<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

require_once __DIR__ . '/../../../includes/auth.php';
requireLogin();
requirePermission('view_operations');

require_once __DIR__ . '/../../../config/database.php';

$company_id = $_SESSION['company_id'];
$user_id = (int)($_GET['user_id'] ?? 0);

if ($user_id <= 0) {
    exit('Invalid employee');
}

/* LOAD EMPLOYEE */
$userStmt = $pdo->prepare("
    SELECT id, full_name
    FROM users
    WHERE id = ? AND company_id = ?
    LIMIT 1
");
$userStmt->execute([$user_id, $company_id]);
$user = $userStmt->fetch(PDO::FETCH_ASSOC);

if (!$user) {
    exit('Employee not found');
}

/* LOAD PIP TICKETS (FIXED LOGIC) */
$tStmt = $pdo->prepare("
    SELECT *
    FROM operations_tickets
    WHERE company_id = ?
      AND category = 'Performance / PIP'
      AND (
            assigned_to = ?
            OR assigned_to IS NULL
          )
    ORDER BY created_at DESC
");
$tStmt->execute([$company_id, $user_id]);
$tickets = $tStmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>PIP Report - <?= htmlspecialchars($user['full_name']) ?></title>
    <style>
        body{
            font-family: Arial, sans-serif;
            font-size: 13px;
            color:#111;
            margin:40px;
        }
        h1,h2,h3,h4{
            margin:0 0 10px 0;
        }
        .header{
            margin-bottom:25px;
            border-bottom:2px solid #f2531e;
            padding-bottom:10px;
        }
        .meta{
            margin-bottom:20px;
            color:#555;
        }
        table{
            width:100%;
            border-collapse:collapse;
            margin-top:15px;
        }
        th, td{
            border:1px solid #ccc;
            padding:10px;
            text-align:left;
            vertical-align:top;
        }
        th{
            background:#f5f5f5;
        }
        .notes-box{
            border:1px solid #ccc;
            min-height:120px;
            margin-top:10px;
            padding:10px;
        }
        .signatures{
            margin-top:50px;
            display:flex;
            justify-content:space-between;
            gap:40px;
        }
        .signature-box{
            width:45%;
        }
        .line{
            margin-top:50px;
            border-top:1px solid #000;
            padding-top:6px;
        }
        .no-data{
            padding:20px;
            text-align:center;
            color:#777;
            font-style:italic;
        }
        @media print{
            body{
                margin:20px;
            }
        }
    </style>
</head>
<body onload="window.print()">

<div class="header">
    <h1>PIP Performance Report</h1>
    <div class="meta">
        <strong>Employee:</strong> <?= htmlspecialchars($user['full_name']) ?><br>
        <strong>Report Date:</strong> <?= date('Y-m-d H:i') ?>
    </div>
</div>

<h3>PIP Cases</h3>

<?php if (!$tickets): ?>
    <div class="no-data">
        No PIP records found for this employee.
    </div>
<?php else: ?>

<table>
    <thead>
        <tr>
            <th>Ticket #</th>
            <th>Subject</th>
            <th>Stage</th>
            <th>Start Date</th>
            <th>Review Date</th>
            <th>End Date</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>

        <?php foreach ($tickets as $t): ?>
            <tr>
                <td><?= htmlspecialchars($t['ticket_number']) ?></td>
                <td><?= htmlspecialchars($t['subject']) ?></td>
                <td><?= htmlspecialchars($t['pip_stage'] ?? '-') ?></td>
                <td><?= !empty($t['pip_start_date']) ? date('Y-m-d', strtotime($t['pip_start_date'])) : '-' ?></td>
                <td><?= !empty($t['pip_review_date']) ? date('Y-m-d', strtotime($t['pip_review_date'])) : '-' ?></td>
                <td><?= !empty($t['pip_end_date']) ? date('Y-m-d', strtotime($t['pip_end_date'])) : '-' ?></td>
                <td><?= htmlspecialchars($t['status']) ?></td>
            </tr>
        <?php endforeach; ?>

    </tbody>
</table>

<?php endif; ?>

<h3 style="margin-top:30px;">Manager Notes</h3>
<div class="notes-box"></div>

<div class="signatures">
    <div class="signature-box">
        <div class="line">Employee Signature</div>
    </div>

    <div class="signature-box">
        <div class="line">Manager Signature</div>
    </div>
</div>

</body>
</html>