<?php 
    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

    $report             = isset($_POST["report"]) ? $_POST["report"] : "";

    $spreadsheet = new Spreadsheet();
    
    switch ($report){
        case "":
            $search_keywords    = isset($_POST['q']) ? htmlspecialchars($_POST['q'], ENT_QUOTES, 'UTF-8') : "";
            $status             = isset($_POST["status"]) ? $_POST["status"] : "";
            $user_group         = isset($_POST["user_group"]) ? $_POST["user_group"] : "";
            $sort               = isset($_POST["sort"]) ? $_POST["sort"] : "id";
            $sort_order         = isset($_POST["order"]) ? $_POST["order"] : "DESC";
            $filter_data = array(
                'search_keywords'    => $search_keywords,
                'status'             => $status,
                'user_group'         => $user_group,
                'sort'               => $sort,
                'order'              => $sort_order
            );
            
            $users                  = $user->getUsers($filter_data);
            $total_users 	        = $user->getTotalUsers($filter_data);

            $spreadsheet = new Spreadsheet();
            $spreadsheet->getActiveSheet()->getStyle('A1:K1')->getFont()->setBold( true );
            $spreadsheet->getActiveSheet()->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension());
            $header_row_array = ['ID', 'DURUM', 'ADI', 'SOYADI', 'EPOSTA', 'TELEFON', 'GLN', 'KURUM ADI', 'ÜYE GRUBU', 'KAYIT TARİHİ', 'SON GİRİŞ'];
            $sheet = $spreadsheet->getActiveSheet();
            $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);
            $sheet->getColumnDimension('E')->setAutoSize(true);
            $sheet->getColumnDimension('F')->setAutoSize(true);
            $sheet->getColumnDimension('G')->setAutoSize(true);
            $sheet->getColumnDimension('H')->setAutoSize(true);
            $sheet->getColumnDimension('I')->setAutoSize(true);
            $sheet->getColumnDimension('J')->setAutoSize(true);
            $sheet->getColumnDimension('K')->setAutoSize(true);
            

            $i     = 2;
            if($total_users>0) {
                foreach($users as $row) {
                    if($row['status_id'] == 0){
                        $status = "Pasif";
                    }else if($row['status_id'] == 1){
                        $status = "Aktif";
                    }else if($row['status_id'] == 2){
                        $status = "Aktarım";
                    }else{
                        $status = "Bilinmiyor";
                    }

                    $sheet->setCellValue('A'.$i, $row['id']);
                    $sheet->setCellValue('B'.$i, $status); 
                    $sheet->setCellValue('C'.$i, $row['name']); 
                    $sheet->setCellValue('D'.$i, $row['surname']); 
                    $sheet->setCellValue('E'.$i, $row['email']); 
                    $sheet->setCellValue('F'.$i, $row['phone']); 
                    $sheet->setCellValue('G'.$i, $row['gln_number']); 
                    $sheet->setCellValue('H'.$i, $row['company']);
                    $sheet->setCellValue('I'.$i, $row['user_group_name']);
                    $sheet->setCellValue('J'.$i, $row['date_created']);
                    $sheet->setCellValue('K'.$i, $row['last_login']);
                    $i++;
                }
            }

            $filename = "Kullanıcı Listesi.xlsx";
        break;
        case "user-course-progress":
            $guid             = isset($_POST["id"]) ? $_POST["id"] : "";
            $user_course      = $course->getUserCourseByGuid($guid);

            $progress       = $course->getUserCourseProgressReport($user_course['id'], $user_course['user_id']);
            
            $spreadsheet = new Spreadsheet();
            $spreadsheet->getActiveSheet()->getStyle('A1:F1')->getFont()->setBold( true );
            $spreadsheet->getActiveSheet()->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension());
            $header_row_array = ['#', 'İÇERİK TÜRÜ', 'BAŞLIK', 'BAŞLAMA TARİHİ', 'SON ERİŞİM', 'İLERLEME DURUMU (%)'];
            $sheet = $spreadsheet->getActiveSheet();
            $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);
            $sheet->getColumnDimension('E')->setAutoSize(true);
            $sheet->getColumnDimension('F')->setAutoSize(true);
            

            $i     = 2;
            if(count($progress)>0) {
                foreach($progress as $row) {
                    $sheet->setCellValue('A'.$i, $i-1);
                    $sheet->setCellValue('B'.$i, $row['type']); 
                    $sheet->setCellValue('C'.$i, $row['title']); 
                    $sheet->setCellValue('D'.$i, $row['date_start']); 
                    $sheet->setCellValue('E'.$i, $row['date']);
                    $sheet->setCellValue('F'.$i, "%".$row['progress']);
                    $i++;
                }
            }
            $filename = "Eğitim İlerleme Raporu.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);