<?php
header('Content-Type: application/json');

$type = $_GET['type'] ?? 'student';

if ($type === 'student') {
    // Create sample student data Excel
    require_once '../vendor/autoload.php';
    
    $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    
    // Headers
    $sheet->setCellValue('A1', 'S/N');
    $sheet->setCellValue('B1', 'Matricnum');
    $sheet->setCellValue('C1', 'Appnum');
    $sheet->setCellValue('D1', 'Surname');
    $sheet->setCellValue('E1', 'Firstname');
    $sheet->setCellValue('F1', 'Othername');
    $sheet->setCellValue('G1', 'Programme');
    $sheet->setCellValue('H1', 'Level');
    $sheet->setCellValue('I1', 'Amount');
    $sheet->setCellValue('J1', 'Receipt No.');
    $sheet->setCellValue('K1', 'Phone');
    $sheet->setCellValue('L1', 'Date');
    
    // Sample data
    $sampleData = [
        [1, '20010311001', 'GTS2010001', 'ADEBOWALE', 'CHINEDU', 'MICHAEL', 'ND ACCOUNTANCY FULL TIME', 'ND II', 55000, 'GXP3000001', '08012345678', '04/08/24'],
        [2, '20010311002', 'GTS2010002', 'AKINWUNMI', 'FUNKE', 'RUTH', 'ND COMPUTER SCIENCE FULL TIME', 'ND II', 55000, 'GXP3000002', '08023456789', '04/08/24'],
        [3, '20010311003', 'GTS2010003', 'OLATUNJI', 'SEGUN', 'DAVID', 'ND ELECTRICAL ENGINEERING FULL TIME', 'ND II', 55000, 'GXP3000003', '08034567890', '04/08/24']
    ];
    
    $row = 2;
    foreach ($sampleData as $data) {
        $sheet->fromArray($data, null, 'A' . $row);
        $row++;
    }
    
    // Set column widths
    foreach(range('A','L') as $column) {
        $sheet->getColumnDimension($column)->setAutoSize(true);
    }
    
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="sample_student_data.xlsx"');
    header('Cache-Control: max-age=0');
    
    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
    $writer->save('php://output');
    
} elseif ($type === 'certificate') {
    // Create sample certificate records Excel
    require_once '../vendor/autoload.php';
    
    $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    
    // Headers
    $sheet->setCellValue('A1', 'S/N');
    $sheet->setCellValue('B1', 'SessionName');
    $sheet->setCellValue('C1', 'MatricNum');
    $sheet->setCellValue('D1', 'Surname');
    $sheet->setCellValue('E1', 'Firstname');
    $sheet->setCellValue('F1', 'Othername');
    $sheet->setCellValue('G1', 'Programme');
    $sheet->setCellValue('H1', 'Cgpa');
    $sheet->setCellValue('I1', 'Grade');
    $sheet->setCellValue('J1', 'LevelName');
    $sheet->setCellValue('K1', 'Semester');
    $sheet->setCellValue('L1', 'DateGraduated');
    $sheet->setCellValue('M1', 'Phone');
    $sheet->setCellValue('N1', 'Email');
    
    // Sample data
    $sampleData = [
        [1, '2022/2023', '20010311001', 'ADEBOWALE', 'CHINEDU', 'MICHAEL', 'ND ACCOUNTANCY FULL TIME', 2.45, 'PASS', 'ND 2', 'SECOND', '07 DECEMBER 2023', '08012345678', 'chinedu@email.com'],
        [2, '2022/2023', '20010311002', 'AKINWUNMI', 'FUNKE', 'RUTH', 'ND COMPUTER SCIENCE FULL TIME', 3.12, 'UPPER CREDIT', 'ND 2', 'SECOND', '07 DECEMBER 2023', '08023456789', 'funke@email.com'],
        [3, '2022/2023', '20010311003', 'OLATUNJI', 'SEGUN', 'DAVID', 'ND ELECTRICAL ENGINEERING FULL TIME', 2.85, 'LOWER CREDIT', 'ND 2', 'SECOND', '07 DECEMBER 2023', '08034567890', 'segun@email.com']
    ];
    
    $row = 2;
    foreach ($sampleData as $data) {
        $sheet->fromArray($data, null, 'A' . $row);
        $row++;
    }
    
    // Set column widths
    foreach(range('A','N') as $column) {
        $sheet->getColumnDimension($column)->setAutoSize(true);
    }
    
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="sample_certificate_records.xlsx"');
    header('Cache-Control: max-age=0');
    
    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
    $writer->save('php://output');
} else {
    echo json_encode(['success' => false, 'message' => 'Invalid sample type']);
}
?>