<?php 
    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
    
    $report             = isset($_POST["report"]) ? $_POST["report"] : "";
    $rows               = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

    $spreadsheet = new Spreadsheet();
    
    switch ($report){
        case "answers":
        case "":
            $guid               = isset($_POST["id"]) ? $_POST["id"] : '';
            $survey_details     = $survey->getSurvey($guid);
            $survey_questions   = $survey->getSurveyQuestions($guid);
            $survey_applications  = $survey->getSurveyApplications($guid);

            $question_count     = count($survey_questions);
            $last_column        = $rows[$question_count+2];

            $header_row_array   = array('#', 'Kullanıcı Adı', 'Kullanıcı Email');

            foreach($survey_questions as $question) {
                $header_row_array[] = $question['title'];
            }

            $sheet = $spreadsheet->getActiveSheet();
            $sheet->getStyle('A1:'.$last_column.'1')->getFont()->setBold( true );
            $sheet->setAutoFilter($sheet->calculateWorksheetDimension());
            $sheet->fromArray( $header_row_array, NULL, 'A1' );
            for($i = 0; $i<$question_count+3; $i++ ) {
                $sheet->getColumnDimension($rows[$i])->setAutoSize(true);
            }

            $i     = 2;
            if(count($survey_applications)>0) {
                $separator = '-CF2022-';
                foreach($survey_applications as $row) {
                    $sheet->setCellValue('A'.$i, $i-1);
                    $sheet->setCellValue('B'.$i, $row['user_name']);
                    $sheet->setCellValue('C'.$i, $row['user_email']);
                    $answers = $survey->getSurveyAnswers($row['id']);
                    $j = 3;
                    foreach($answers as $answer) {
                        $answer_text = $answer['answer'];
                        if(strpos($answer_text, $separator) !== false) {
                            $answer_text = str_replace($separator, "\n", $answer_text);
                            $sheet->setCellValue($rows[$j].$i, $answer_text);
                            $sheet->getStyle($rows[$j].$i)->getAlignment()->setWrapText(true);
                        }else{
                            $sheet->setCellValue($rows[$j].$i, $answer_text);
                        }
                        $j++;
                    }
                    $i++;
                }
            }
            $filename = "Anket Sonuçları.xlsx";
        break;
        case "applications":
            $guid               = isset($_POST["id"]) ? $_POST["id"] : '';
            $survey_applications  = $survey->getSurveyApplications($guid);

            $header_row_array   = array('#', 'Kullanıcı Adı', 'Kullanıcı Email', 'Tarih');

            $sheet = $spreadsheet->getActiveSheet();
            $sheet->getStyle('A1:D1')->getFont()->setBold( true );
            $sheet->setAutoFilter($sheet->calculateWorksheetDimension());
            $sheet->fromArray( $header_row_array, NULL, 'A1' );
            $sheet->getColumnDimension('A')->setAutoSize(true);
            $sheet->getColumnDimension('B')->setAutoSize(true);
            $sheet->getColumnDimension('C')->setAutoSize(true);
            $sheet->getColumnDimension('D')->setAutoSize(true);

            $i     = 2;
            if(count($survey_applications)>0) {
                foreach($survey_applications as $row) {
                    $sheet->setCellValue('A'.$i, $i-1);
                    $sheet->setCellValue('B'.$i, $row['user_name']);
                    $sheet->setCellValue('C'.$i, $row['user_email']);
                    $sheet->setCellValue('D'.$i, $row['date_added']);
                    $i++;
                }
            }
            $filename = "Anket Katılımcıları.xlsx";
        break;
    }

    
    try {
        $writer = new Xlsx($spreadsheet);
        $writer->save($filename);
        $content = file_get_contents($filename);
    } catch(Exception $e) {
        exit($e->getMessage());
    }

    header("Content-Disposition: attachment; filename=".$filename);

    unlink($filename);
    exit($content);