<?php
require_once '../../config/config.php';

// Always serve as PNG image; allow optional download
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$download = isset($_GET['dl']);
$width = isset($_GET['w']) ? max(600, (int)$_GET['w']) : 1000;
$height = isset($_GET['h']) ? max(400, (int)$_GET['h']) : 700;

if ($id <= 0) {
    header('Content-Type: image/png');
    $im = imagecreatetruecolor(800, 200);
    $white = imagecolorallocate($im, 255, 255, 255);
    $red = imagecolorallocate($im, 200, 0, 0);
    imagefilledrectangle($im, 0, 0, 800, 200, $white);
    imagestring($im, 5, 10, 80, 'Invalid assignment id', $red);
    imagepng($im); imagedestroy($im); exit;
}

try {
    // Fetch assignment with staff and exam details
    $sql = "SELECT qps.id, qps.start_date, qps.end_date, e.exam_name,
                   s.first_name, s.last_name, s.email
            FROM qp_setter_assignments qps
            JOIN exams e ON qps.exam_id = e.exam_id
            JOIN staff s ON qps.staff_id = s.staff_id
            WHERE qps.id = ? LIMIT 1";
    $st = $pdo->prepare($sql);
    $st->execute([$id]);
    $row = $st->fetch(PDO::FETCH_ASSOC);
    if (!$row) { throw new Exception('Assignment not found'); }

    $setterName = trim(($row['first_name'] ?? '').' '.($row['last_name'] ?? ''));
    $examName = $row['exam_name'] ?? '';
    $start = $row['start_date'] ?? '';
    $end = $row['end_date'] ?? '';
    $today = date('d-m-Y');

    // Prepare image
    $im = imagecreatetruecolor($width, $height);
    $white = imagecolorallocate($im, 255, 255, 255);
    $black = imagecolorallocate($im, 34, 34, 34);
    $gray = imagecolorallocate($im, 120, 120, 120);
    $blue = imagecolorallocate($im, 0, 102, 204);
    imagefilledrectangle($im, 0, 0, $width, $height, $white);

    // Header band
    imagefilledrectangle($im, 0, 0, $width, 60, $blue);
    imagestring($im, 5, 15, 20, 'Appointment Order - Question Paper Setter', $white);

    $y = 90; $lineGap = 28; $left = 40;
    imagestring($im, 5, $left, $y, 'Date: '.$today, $gray); $y += $lineGap;
    imagestring($im, 5, $left, $y, 'To,', $black); $y += $lineGap;
    imagestring($im, 5, $left + 30, $y, $setterName, $black); $y += $lineGap;
    if (!empty($row['email'])) { imagestring($im, 4, $left + 30, $y, $row['email'], $gray); $y += $lineGap; }

    $y += 10;
    imagestring($im, 5, $left, $y, 'Subject: Appointment as Question Paper Setter', $black); $y += $lineGap + 6;

    // Body lines
    imagestring($im, 5, $left, $y, 'Exam: '.$examName, $black); $y += $lineGap;
    imagestring($im, 5, $left, $y, 'Assignment Window: '.$start.' to '.$end, $black); $y += $lineGap;
    imagestring($im, 5, $left, $y, 'Kindly prepare the question paper and follow the guidelines issued.', $black); $y += $lineGap;

    $y += 10;
    imagestring($im, 5, $left, $y, 'Enclosures:', $black); $y += $lineGap;
    $enc = [
        'Syllabus',
        'Instructions to the Question Paper Setters',
        'Bio-data',
        'Template for Question Paper',
        'Bloom''s Taxonomy action verbs',
        'Claim Form (MS Word and Excel)',
        'Declaration'
    ];
    foreach ($enc as $i => $t) { imagestring($im, 4, $left + 20, $y, ($i+1).'. '.$t, $gray); $y += $lineGap - 4; }

    // Footer signature placeholder
    $y = $height - 110;
    imagestring($im, 5, $width - 320, $y, 'Controller of Examinations', $black);

    if ($download) {
        header('Content-Disposition: attachment; filename="appointment_order_'.$id.'.png"');
    }
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Pragma: no-cache');
    header('Content-Type: image/png');
    imagepng($im); imagedestroy($im);
} catch (Throwable $e) {
    header('Content-Type: image/png');
    $im = imagecreatetruecolor(800, 220);
    $white = imagecolorallocate($im, 255, 255, 255);
    $red = imagecolorallocate($im, 200, 0, 0);
    imagefilledrectangle($im, 0, 0, 800, 220, $white);
    imagestring($im, 5, 10, 80, 'Error generating appointment order', $red);
    imagestring($im, 3, 10, 120, $e->getMessage(), $red);
    imagepng($im); imagedestroy($im);
}
?>


