<?php
    require '../assets/config-data.php';
    require '../../vendor/autoload.php';

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

    $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();

    if(isset($_POST['upload'])) {
        $filetype = ['application/vnd.ms-excel','text/xls', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'text/xlsx','application/vnd.oasis.opendocument.spreadsheet'];

        if(in_array($_FILES['turnamen']['type'], $filetype)) {
               $filexl = $_FILES['turnamen']['name'];
               $exfile = $_FILES['turnamen']['tmp_name'];

               $ssheet = $reader->load($exfile);

               $countsheet = $ssheet->getSheet(0)->toArray();

                    $dataexcel = $ssheet->getActiveSheet()->toArray();

                        foreach($dataexcel as $datas) {
                            $user = $datas[0];
                            $touser = $datas[1];
                            $gameuser = $datas[2];
                            $hadiahuser = $datas[3];
                            $ito = "INSERT INTO peserta_to(nama_user, to_user, game_user, hadiah_user, tanggal_to) VALUES('$user', '$touser', '$gameuser', '$hadiahuser', '$today')";
                            $qto = mysqli_query($data, $ito); 

                        }

                        echo '
                        <script>
                            alert("Turnamen Successfully Added!!");
                            window.location.href = "../dashboard.php?hal=turnamen";
                        </script>
                        ';


        } else {
            echo '
                        <script>
                            alert("File tidak didukung mohon dicek ulang!!, Gagal Upload");
                            window.location.href = "../dashboard.php?hal=turnamen";
                        </script>
                        ';
        }

    
    }

?>