<?php

require("config/conn.php");
require("functions.php");
session_start();


require "vendor/autoload.php";






use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;


if (isset($_GET['export_user_excel'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'uId');
    $sheet->setCellValue("C1", 'name');
    $sheet->setCellValue("D1", 'email');
    $sheet->setCellValue("E1", 'avatar');
    $sheet->setCellValue("F1", 'coins');
    $sheet->setCellValue("G1", 'totalCoins');
    $sheet->setCellValue("H1", 'isBanned');
    $sheet->setCellValue("I1", 'levelId');
    $sheet->setCellValue("J1", 'quizPlayed');
    $sheet->setCellValue("K1", 'inviteCode');
    $sheet->setCellValue("L1", 'invitedBy');
    $sheet->setCellValue("M1", 'accountCreated');

    $sql = "SELECT * FROM `users`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['uId']);
            $sheet->setCellValue("C".$cell."", $row['name']);
            $sheet->setCellValue("D".$cell."", $row['email']);
            $sheet->setCellValue("E".$cell."", $row['avatar']);
            $sheet->setCellValue("F".$cell."", $row['coins']);
            $sheet->setCellValue("G".$cell."", $row['totalCoins']);
            $sheet->setCellValue("H".$cell."", $row['isBanned']);
            $sheet->setCellValue("I".$cell."", $row['levelId']);
            $sheet->setCellValue("J".$cell."", $row['quizPlayed']);
            $sheet->setCellValue("K".$cell."", $row['inviteCode']);
            $sheet->setCellValue("L".$cell."", $row['invitedBy']);
            $sheet->setCellValue("M".$cell."", $row['accountCreated']);

            $cell++;
        }


    $filename = "users.xlsx";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Xlsx");
    $writer->save("php://output");

}

if (isset($_GET['export_user_csv'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'uId');
    $sheet->setCellValue("C1", 'name');
    $sheet->setCellValue("D1", 'email');
    $sheet->setCellValue("E1", 'avatar');
    $sheet->setCellValue("F1", 'coins');
    $sheet->setCellValue("G1", 'totalCoins');
    $sheet->setCellValue("H1", 'isBanned');
    $sheet->setCellValue("I1", 'levelId');
    $sheet->setCellValue("J1", 'quizPlayed');
    $sheet->setCellValue("K1", 'inviteCode');
    $sheet->setCellValue("L1", 'invitedBy');
    $sheet->setCellValue("M1", 'accountCreated');

    $sql = "SELECT * FROM `users`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['uId']);
            $sheet->setCellValue("C".$cell."", $row['name']);
            $sheet->setCellValue("D".$cell."", $row['email']);
            $sheet->setCellValue("E".$cell."", $row['avatar']);
            $sheet->setCellValue("F".$cell."", $row['coins']);
            $sheet->setCellValue("G".$cell."", $row['totalCoins']);
            $sheet->setCellValue("H".$cell."", $row['isBanned']);
            $sheet->setCellValue("I".$cell."", $row['levelId']);
            $sheet->setCellValue("J".$cell."", $row['quizPlayed']);
            $sheet->setCellValue("K".$cell."", $row['inviteCode']);
            $sheet->setCellValue("L".$cell."", $row['invitedBy']);
            $sheet->setCellValue("M".$cell."", $row['accountCreated']);

            $cell++;
        }


    $filename = "users.csv";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Csv");
    $writer->save("php://output");

}

if (isset($_GET['export_user_pdf'])) {

  $sql = "SELECT * FROM `users`";
  $query_run = mysqli_query($conn, $sql);

  if (mysqli_num_rows($query_run) > 0) {
        $html = '
        <style>
        .heading, .heading > td{
            background: #15122d;
            color: #fff;
            padding: 10px 20px;
          }
          
           td{
            border-bottom: 1px solid #15122d;
             border-right: 1px solid #15122d;
          }
        </style>
        <table>';
            $html.='<tr class="heading"><td style="color: #fff;">ID</td><td  style="color: #fff;">uID</td><td  style="color: #fff;">Name</td><td  style="color: #fff;">Avatar</td><td  style="color: #fff;">Email</td><td  style="color: #fff;">Coins</td><td  style="color: #fff;">Total Coins </td><td  style="color: #fff;">Quiz Played</td><td  style="color: #fff;">Status</td><td  style="color: #fff;">Account Created</td><td  style="color: #fff;">Level ID</td><td  style="color: #fff;">Invite Code</td><td  style="color: #fff;">Invited By</td></tr>';
            while ($row = mysqli_fetch_assoc($query_run)) {
                if ($row['isBanned'] == 0) {
                    $status = 'Active';
                } else{
                    $status = 'Banned';
                }
                $html.='<tr><td>'.$row['id'].'</td><td>'.$row['uId'].'</td><td>'.$row['name'].'</td><td><img style="width: 50px; height: 50px;" src="'.$row['avatar'].'"  ></td><td>'.$row['email'].'</td><td>'.$row['coins'].'</td><td>'.$row['totalCoins'].'</td><td>'.$row['quizPlayed'].'</td><td>'.$status.'</td><td>'.$row['accountCreated'].'</td><td>'.$row['levelId'].'</td><td>'.$row['inviteCode'].'</td><td>'.$row['invitedBy'].'</td></tr>';
            }
        $html.= '</table>';
  } else{
    $html = 'Data not found!';
  }
        // echo $html;

    $mpdf = new \Mpdf\Mpdf();
    $mpdf->writeHTML($html);
    $mpdf->output('users.pdf', 'D');


}





if (isset($_GET['export_category_csv'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'name');
    $sheet->setCellValue("C1", 'icon');
    $sheet->setCellValue("D1", 'colorCode');
    $sheet->setCellValue("E1", 'totalQuizzes');
    $sheet->setCellValue("F1", 'priority');


    $sql = "SELECT * FROM `categories`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['name']);
            $sheet->setCellValue("C".$cell."", $row['icon']);
            $sheet->setCellValue("D".$cell."", $row['colorCode']);
            $sheet->setCellValue("E".$cell."", $row['totalQuizzes']);
            $sheet->setCellValue("F".$cell."", $row['priority']);

            $cell++;
        }


    $filename = "category.csv";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Csv");
    $writer->save("php://output");

}

if (isset($_GET['export_category_excel'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'name');
    $sheet->setCellValue("C1", 'icon');
    $sheet->setCellValue("D1", 'colorCode');
    $sheet->setCellValue("E1", 'totalQuizzes');
    $sheet->setCellValue("F1", 'priority');


    $sql = "SELECT * FROM `categories`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['name']);
            $sheet->setCellValue("C".$cell."", $row['icon']);
            $sheet->setCellValue("D".$cell."", $row['colorCode']);
            $sheet->setCellValue("E".$cell."", $row['totalQuizzes']);
            $sheet->setCellValue("F".$cell."", $row['priority']);

            $cell++;
        }


    $filename = "category.xlsx";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Xlsx");
    $writer->save("php://output");

}

if (isset($_GET['export_quizzes_csv'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'categoryId');
    $sheet->setCellValue("C1", 'name');
    $sheet->setCellValue("D1", 'icon');
    $sheet->setCellValue("E1", 'entryFee');
    $sheet->setCellValue("F1", 'prizePerQuestions');
    $sheet->setCellValue("G1", 'colorCode');
    $sheet->setCellValue("H1", 'priority');
    $sheet->setCellValue("I1", 'questionLimit');
    $sheet->setCellValue("J1", 'livesAvailable');
    $sheet->setCellValue("K1", 'easy');
    $sheet->setCellValue("L1", 'medium');
    $sheet->setCellValue("M1", 'hard');
    $sheet->setCellValue("N1", 'time');


    $sql = "SELECT * FROM `quizzes`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['categoryId']);
            $sheet->setCellValue("C".$cell."", $row['name']);
            $sheet->setCellValue("D".$cell."", $row['icon']);
            $sheet->setCellValue("E".$cell."", $row['entryFee']);
            $sheet->setCellValue("F".$cell."", $row['prizePerQuestion']);
            $sheet->setCellValue("G".$cell."", $row['colorCode']);
            $sheet->setCellValue("H".$cell."", $row['priority']);
            $sheet->setCellValue("I".$cell."", $row['questionLimit']);
            $sheet->setCellValue("J".$cell."", $row['livesAvailable']);
            $sheet->setCellValue("K".$cell."", $row['easy']);
            $sheet->setCellValue("L".$cell."", $row['medium']);
            $sheet->setCellValue("M".$cell."", $row['hard']);
            $sheet->setCellValue("N".$cell."", $row['time']);

            $cell++;
        }


    $filename = "quiz.csv";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Csv");
    $writer->save("php://output");

}

if (isset($_GET['export_quizzes_excel'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'categoryId');
    $sheet->setCellValue("C1", 'name');
    $sheet->setCellValue("D1", 'icon');
    $sheet->setCellValue("E1", 'entryFee');
    $sheet->setCellValue("F1", 'prizePerQuestion');
    $sheet->setCellValue("G1", 'colorCode');
    $sheet->setCellValue("H1", 'priority');
    $sheet->setCellValue("I1", 'questionLimit');
    $sheet->setCellValue("J1", 'livesAvailable');
    $sheet->setCellValue("K1", 'easy');
    $sheet->setCellValue("L1", 'medium');
    $sheet->setCellValue("M1", 'hard');
    $sheet->setCellValue("N1", 'time');


    $sql = "SELECT * FROM `quizzes`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['categoryId']);
            $sheet->setCellValue("C".$cell."", $row['name']);
            $sheet->setCellValue("D".$cell."", $row['icon']);
            $sheet->setCellValue("E".$cell."", $row['entryFee']);
            $sheet->setCellValue("F".$cell."", $row['prizePerQuestion']);
            $sheet->setCellValue("G".$cell."", $row['colorCode']);
            $sheet->setCellValue("H".$cell."", $row['priority']);
            $sheet->setCellValue("I".$cell."", $row['questionLimit']);
            $sheet->setCellValue("J".$cell."", $row['livesAvailable']);
            $sheet->setCellValue("K".$cell."", $row['easy']);
            $sheet->setCellValue("L".$cell."", $row['medium']);
            $sheet->setCellValue("M".$cell."", $row['hard']);
            $sheet->setCellValue("N".$cell."", $row['time']);

            $cell++;
        }


    $filename = "quiz.xlsx";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Xlsx");
    $writer->save("php://output");

}


if (isset($_GET['export_questions_csv'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'question');
    $sheet->setCellValue("C1", 'image');
    $sheet->setCellValue("D1", 'optionA');
    $sheet->setCellValue("E1", 'optionB');
    $sheet->setCellValue("F1", 'optionC');
    $sheet->setCellValue("G1", 'optionD');
    $sheet->setCellValue("H1", 'answer');
    $sheet->setCellValue("I1", 'quizId');
    $sheet->setCellValue("J1", 'difficulty');


    $sql = "SELECT * FROM `questions`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['question']);
            $sheet->setCellValue("C".$cell."", $row['image']);
            $sheet->setCellValue("D".$cell."", $row['optionA']);
            $sheet->setCellValue("E".$cell."", $row['optionB']);
            $sheet->setCellValue("F".$cell."", $row['optionC']);
            $sheet->setCellValue("G".$cell."", $row['optionD']);
            $sheet->setCellValue("H".$cell."", $row['answer']);
            $sheet->setCellValue("I".$cell."", $row['quizId']);
            $sheet->setCellValue("J".$cell."", $row['difficulty']);

            $cell++;
        }


    $filename = "questions.csv";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Csv");
    $writer->save("php://output");

}
if (isset($_GET['export_questions_excel'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'question');
    $sheet->setCellValue("C1", 'image');
    $sheet->setCellValue("D1", 'optionA');
    $sheet->setCellValue("E1", 'optionB');
    $sheet->setCellValue("F1", 'optionC');
    $sheet->setCellValue("G1", 'optionD');
    $sheet->setCellValue("H1", 'answer');
    $sheet->setCellValue("I1", 'quizId');
    $sheet->setCellValue("J1", 'difficulty');


    $sql = "SELECT * FROM `questions`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['question']);
            $sheet->setCellValue("C".$cell."", $row['image']);
            $sheet->setCellValue("D".$cell."", $row['optionA']);
            $sheet->setCellValue("E".$cell."", $row['optionB']);
            $sheet->setCellValue("F".$cell."", $row['optionC']);
            $sheet->setCellValue("G".$cell."", $row['optionD']);
            $sheet->setCellValue("H".$cell."", $row['answer']);
            $sheet->setCellValue("I".$cell."", $row['quizId']);
            $sheet->setCellValue("J".$cell."", $row['difficulty']);

            $cell++;
        }


    $filename = "questions.xlsx";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Xlsx");
    $writer->save("php://output");

}







if(isset($_POST['import_questions_btn'])){
    
    $quiz = filter($_POST['quiz_id']);


    $allowed_ext = ['xls', 'csv', 'xlsx'];
    $fileName = $_FILES['impFile']['name'];
    $checking = explode(".", $fileName);
    $file_ext = end($checking);
   
       if (in_array($file_ext, $allowed_ext)) {
           
           $targetPath =  $_FILES['impFile']['tmp_name'];
           $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($targetPath);
        
           $data = $spreadsheet->getActiveSheet()->toArray();
   
           foreach ($data as $row) {
               $question = filter($row['0']);
               $image = filter($row['1']);
               $optionA = filter($row['2']);
               $optionB = filter($row['3']);
               $optionC = filter($row['4']);
               $optionD = filter($row['5']);
               $answer = filter($row['6']);
               $difficulty = filter($row['7']);
            

            //    echo   $question;
            // echo '<br>';
   
            
       
   
   
                $sql = "INSERT INTO `questions`(`question`, `image`, `optionA`, `optionB`, `optionC`, `optionD`, `answer`, `quizId`, `difficulty`) VALUES ('$question','$image','$optionA','$optionB','$optionC','$optionD','$answer','$quiz','$difficulty')";
               
                $query_run = mysqli_query($conn, $sql);
   
            if ($query_run) {
                $_SESSION['status'] = "Questions Imported";
                $_SESSION['message'] = "Question Imported Successfully";
                $_SESSION['icon'] =  'success';
                header('Location: questions.php');
          } else{
            $_SESSION['status'] = "Questions Importing Failed";
            $_SESSION['message'] = "Questions Importing failed";
            $_SESSION['icon'] =  'error';
            header('Location: questions.php');
               }
   
   
           }
   
   
       } else {
        $_SESSION['status'] = "Questions Importing Failed";
        $_SESSION['message'] = "Questions Importing failed ['Invalid File']";
        $_SESSION['icon'] =  'error';
        header('Location: questions.php');
       }
       
   
   
   }
   
   
if(isset($_POST['import_daily_questions_btn'])){
    
 


    $allowed_ext = ['xls', 'csv', 'xlsx'];
    $fileName = $_FILES['impFile']['name'];
    $checking = explode(".", $fileName);
    $file_ext = end($checking);
   
       if (in_array($file_ext, $allowed_ext)) {
           
           $targetPath =  $_FILES['impFile']['tmp_name'];
           $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($targetPath);
        
           $data = $spreadsheet->getActiveSheet()->toArray();
   
           foreach ($data as $row) {
               $question = filter($row['0']);
               $image = filter($row['1']);
               $optionA = filter($row['2']);
               $optionB = filter($row['3']);
               $optionC = filter($row['4']);
               $optionD = filter($row['5']);
               $answer = filter($row['6']);
            

            //    echo   $question;
            // echo '<br>';
   
            
       
   
   
                $sql = "INSERT INTO `dailyquizquestions` (`question`, `image`, `optionA`, `optionB`, `optionC`, `optionD`, `answer`, `questionDate`) VALUES ('$question', '$image', '$optionA', '$optionB', '$optionC', '$optionD', '$answer', current_timestamp());";
               
                $query_run = mysqli_query($conn, $sql);
   
            if ($query_run) {
                $_SESSION['status'] = "Questions Imported";
                $_SESSION['message'] = "Question Imported Successfully";
                $_SESSION['icon'] =  'success';
                header('Location: dailyquiz.php');
          } else{
            $_SESSION['status'] = "Questions Importing Failed";
            $_SESSION['message'] = "Questions Importing failed";
            $_SESSION['icon'] =  'error';
            header('Location: dailyquiz.php');
               }
   
   
           }
   
   
       } else {
        $_SESSION['status'] = "Questions Importing Failed";
        $_SESSION['message'] = "Questions Importing failed ['Invalid File']";
        $_SESSION['icon'] =  'error';
        header('Location: dailyquiz.php');
       }
       
   
   
   }
   
   
   if (isset($_GET['export_daily_questions_excel'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'question');
    $sheet->setCellValue("C1", 'image');
    $sheet->setCellValue("D1", 'optionA');
    $sheet->setCellValue("E1", 'optionB');
    $sheet->setCellValue("F1", 'optionC');
    $sheet->setCellValue("G1", 'optionD');
    $sheet->setCellValue("H1", 'answer');
    $sheet->setCellValue("I1", 'questionDate');


    $sql = "SELECT * FROM `dailyquizquestions`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['question']);
            $sheet->setCellValue("C".$cell."", $row['image']);
            $sheet->setCellValue("D".$cell."", $row['optionA']);
            $sheet->setCellValue("E".$cell."", $row['optionB']);
            $sheet->setCellValue("F".$cell."", $row['optionC']);
            $sheet->setCellValue("G".$cell."", $row['optionD']);
            $sheet->setCellValue("H".$cell."", $row['answer']);
            $sheet->setCellValue("I".$cell."", $row['questionDate']);

            $cell++;
        }


    $filename = "daily-questions.xlsx";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Xlsx");
    $writer->save("php://output");

}
   if (isset($_GET['export_daily_questions_csv'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'question');
    $sheet->setCellValue("C1", 'image');
    $sheet->setCellValue("D1", 'optionA');
    $sheet->setCellValue("E1", 'optionB');
    $sheet->setCellValue("F1", 'optionC');
    $sheet->setCellValue("G1", 'optionD');
    $sheet->setCellValue("H1", 'answer');
    $sheet->setCellValue("I1", 'questionDate');


    $sql = "SELECT * FROM `dailyquizquestions`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['question']);
            $sheet->setCellValue("C".$cell."", $row['image']);
            $sheet->setCellValue("D".$cell."", $row['optionA']);
            $sheet->setCellValue("E".$cell."", $row['optionB']);
            $sheet->setCellValue("F".$cell."", $row['optionC']);
            $sheet->setCellValue("G".$cell."", $row['optionD']);
            $sheet->setCellValue("H".$cell."", $row['answer']);
            $sheet->setCellValue("I".$cell."", $row['questionDate']);

            $cell++;
        }


    $filename = "daily-questions.csv";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Csv");
    $writer->save("php://output");

}




if (isset($_GET['export_games_csv'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'name');
    $sheet->setCellValue("C1", 'icon');
    $sheet->setCellValue("D1", 'gameUrl');
    $sheet->setCellValue("E1", 'colorCode');
    $sheet->setCellValue("F1", 'bgImage');
    $sheet->setCellValue("G1", 'priority');


    $sql = "SELECT * FROM `games`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['name']);
            $sheet->setCellValue("C".$cell."", $row['icon']);
            $sheet->setCellValue("D".$cell."", $row['gameUrl']);
            $sheet->setCellValue("E".$cell."", $row['colorCode']);
            $sheet->setCellValue("F".$cell."", $row['bgImage']);
            $sheet->setCellValue("G".$cell."", $row['priority']);

            $cell++;
        }


    $filename = "games.csv";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Csv");
    $writer->save("php://output");

}
if (isset($_GET['export_games_excel'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'name');
    $sheet->setCellValue("C1", 'icon');
    $sheet->setCellValue("D1", 'gameUrl');
    $sheet->setCellValue("E1", 'colorCode');
    $sheet->setCellValue("F1", 'bgImage');
    $sheet->setCellValue("G1", 'priority');


    $sql = "SELECT * FROM `games`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['name']);
            $sheet->setCellValue("C".$cell."", $row['icon']);
            $sheet->setCellValue("D".$cell."", $row['gameUrl']);
            $sheet->setCellValue("E".$cell."", $row['colorCode']);
            $sheet->setCellValue("F".$cell."", $row['bgImage']);
            $sheet->setCellValue("G".$cell."", $row['priority']);

            $cell++;
        }


    $filename = "games.xlsx";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Xlsx");
    $writer->save("php://output");

}




if (isset($_GET['export_avatars_csv'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'name');
    $sheet->setCellValue("C1", 'image');


    $sql = "SELECT * FROM `avatars`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['name']);
            $sheet->setCellValue("C".$cell."", $row['image']);

            $cell++;
        }


    $filename = "avatars.csv";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Csv");
    $writer->save("php://output");

}


if (isset($_GET['export_avatars_excel'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'name');
    $sheet->setCellValue("C1", 'image');


    $sql = "SELECT * FROM `avatars`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['name']);
            $sheet->setCellValue("C".$cell."", $row['image']);

            $cell++;
        }


    $filename = "avatars.xlsx";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Xlsx");
    $writer->save("php://output");

}


if (isset($_GET['export_badge_csv'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'name');
    $sheet->setCellValue("C1", 'image');
    $sheet->setCellValue("D1", 'greetings');
    $sheet->setCellValue("E1", 'minLimit');
    $sheet->setCellValue("F1", 'maxLimit');


    $sql = "SELECT * FROM `earninglevels`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['name']);
            $sheet->setCellValue("C".$cell."", $row['icon']);
            $sheet->setCellValue("D".$cell."", $row['greetings']);
            $sheet->setCellValue("E".$cell."", $row['minLimit']);
            $sheet->setCellValue("F".$cell."", $row['maxLimit']);

            $cell++;
        }


    $filename = "badges.csv";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Csv");
    $writer->save("php://output");

}


if (isset($_GET['export_badge_excel'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'name');
    $sheet->setCellValue("C1", 'image');
    $sheet->setCellValue("D1", 'greetings');
    $sheet->setCellValue("E1", 'minLimit');
    $sheet->setCellValue("F1", 'maxLimit');


    $sql = "SELECT * FROM `earninglevels`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['name']);
            $sheet->setCellValue("C".$cell."", $row['icon']);
            $sheet->setCellValue("D".$cell."", $row['greetings']);
            $sheet->setCellValue("E".$cell."", $row['minLimit']);
            $sheet->setCellValue("F".$cell."", $row['maxLimit']);

            $cell++;
        }


    $filename = "badges.xlsx";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Xlsx");
    $writer->save("php://output");

}



if (isset($_GET['export_withdraw-methods_csv'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'name');
    $sheet->setCellValue("C1", 'icon');
    $sheet->setCellValue("D1", 'idName');
    $sheet->setCellValue("E1", 'enabled');


    $sql = "SELECT * FROM `withdrawmethods`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['name']);
            $sheet->setCellValue("C".$cell."", $row['icon']);
            $sheet->setCellValue("D".$cell."", $row['idName']);
            $sheet->setCellValue("E".$cell."", $row['enabled']);

            $cell++;
        }


    $filename = "withdraw-methdos.csv";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Csv");
    $writer->save("php://output");

}

if (isset($_GET['export_withdraw-methods_excel'])) {

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue("A1", 'id');

    $sheet->setCellValue("B1", 'name');
    $sheet->setCellValue("C1", 'icon');
    $sheet->setCellValue("D1", 'idName');
    $sheet->setCellValue("E1", 'enabled');


    $sql = "SELECT * FROM `withdrawmethods`";

    $query_run = mysqli_query($conn, $sql) or DIE("Error Occured");

        $cell = 2;
        foreach ($query_run as $row) {
            $sheet->setCellValue("A".$cell."", $row['id']);

            $sheet->setCellValue("B".$cell."", $row['name']);
            $sheet->setCellValue("C".$cell."", $row['icon']);
            $sheet->setCellValue("D".$cell."", $row['idName']);
            $sheet->setCellValue("E".$cell."", $row['enabled']);

            $cell++;
        }


    $filename = "withdraw-methdos.xlsx";

    // Redirect output to the admion

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

   // If use IE - 9
    header("Cache-Control: max-age=1");

    $writer = IOFactory::createWriter($spreadsheet, "Xlsx");
    $writer->save("php://output");

}





?>