<?php
require 'excelupload/vendor/autoload.php'; // Include PhpSpreadsheet library

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

// Function to generate 6 Tambola tickets with unique numbers between 1 to 90
function generateUniqueSixTickets() {
    // Generate a pool of numbers from 1 to 90
    $numbersPool = range(1, 90);
    shuffle($numbersPool); // Shuffle the numbers for randomness

    // Prepare an array to hold the tickets
    $tickets = [];

    // Generate 6 tickets, each with 15 unique numbers
    for ($ticketIndex = 0; $ticketIndex < 6; $ticketIndex++) {
        $ticket = array_fill(0, 3, array_fill(0, 5, null)); // 3 rows, 5 columns per ticket

        // Fill the ticket with 15 unique numbers
        for ($rowIndex = 0; $rowIndex < 3; $rowIndex++) {
            for ($colIndex = 0; $colIndex < 5; $colIndex++) {
                // Assign one number from the shuffled pool to the ticket
                $ticket[$rowIndex][$colIndex] = array_pop($numbersPool); // Pop one number from the pool
            }
        }

        // Add the ticket to the tickets array
        $tickets[] = $ticket;
    }

    return $tickets;
}

// Generate tickets
$tickets = generateUniqueSixTickets();

// Create an Excel file
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// Styling for borders
$borderStyle = [
    'borders' => [
        'allBorders' => [
            'borderStyle' => Border::BORDER_THIN,
            'color' => ['argb' => '000000'],
        ],
    ],
];

// Row index for placing the tickets in the Excel sheet
$rowIndex = 1;

// Populate Excel with tickets
foreach ($tickets as $ticketIndex => $ticket) {
    // Set ticket number in the first cell of each ticket block
    $sheet->setCellValue("A$rowIndex", "Ticket #" . ($ticketIndex + 1));
    $sheet->mergeCells("A$rowIndex:E$rowIndex");
    $sheet->getStyle("A$rowIndex")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
    $rowIndex++;

    // Fill ticket data (3 rows and 5 columns)
    foreach ($ticket as $row) {
        $colIndex = 1; // Start column from 1 (not A, B, C, etc.)
        foreach ($row as $number) {
            // Convert column number to letter (Excel columns)
            $colLetter = chr(64 + $colIndex); // Convert column index to A, B, C, etc.
            $sheet->setCellValue("$colLetter$rowIndex", $number);
            $colIndex++;
        }
        $rowIndex++;
    }

    // Apply border to the ticket
    $sheet->getStyle("A" . ($rowIndex - 4) . ":E" . ($rowIndex - 1))->applyFromArray($borderStyle);

    // Add a blank row between tickets (separate the tickets by a blank row)
    $rowIndex++;
}

// Save Excel file
$writer = new Xlsx($spreadsheet);
$filename = 'Tambola_Tickets_6_Unique_Combination.xlsx';
$writer->save($filename);

// Download the file
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Cache-Control: max-age=0');
readfile($filename);

// Delete the file after download
unlink($filename);

exit;
?>
