<script>moment.updateLocale('nl', {
weekdays : [
"Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag"
]
});
</script>
<?php
//session_start();


use PhpOffice\PhpSpreadsheet\IOFactory;

$x = 0;
$z = 0;

$email = $_POST['Email'];
$naam = $_POST['Naam'];
$datum = $_POST['datumaankomst'];

$filename = $naam." ".$datum;


$totaalhuur = $_POST['totaalnachten'] + $_POST['totaalstart'];
$totaalextra = $_POST['totaaloverig'] + $_POST['totaalhout'];

require '../assets/vendor/autoload.php';


try {
    $reader = IOFactory::createReader("Xlsx");
} catch (\PhpOffice\PhpSpreadsheet\Reader\Exception $e) {
    die('Error loading file: '.$e->getMessage());
}
try {
    $spreadsheet = $reader->load("../assets/template.xlsx");
} catch (\PhpOffice\PhpSpreadsheet\Reader\Exception $e) {
    die('Error loading file: '.$e->getMessage());
}
try {
    $sheet = $spreadsheet->getActiveSheet();
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
    die('Error loading file: '.$e->getMessage());
}
$sheet->setCellValue('B2',$naam);
$sheet->setCellValue('B3',$_POST['type']);
$sheet->setCellValue('B4',$email);
$sheet->setCellValue('B5',$_POST['datumaankomst']);
$sheet->setCellValue('B6',$_POST['datumvertrek']);
$sheet->setCellValue('B9',$_POST['opmerkingen']);
$sheet->setCellValue('C12',$_POST['aantalstart']);
$sheet->setCellValue('D12',$_POST['totaalstart']);
$sheet->setCellValue('C13',$_POST['aantalnachten']);
$sheet->setCellValue('D13',$_POST['totaalnachten']);
$sheet->setCellValue('D14',$totaalhuur);
$sheet->setCellValue('C17',$_POST['hout']);
$sheet->setCellValue('D17',$_POST['totaalhout']);
$sheet->setCellValue('C18',$_POST['overig']);
$sheet->setCellValue('D18',$_POST['totaaloverig']);
$sheet->setCellValue('D19',$totaalextra);

foreach ($_POST['aantalpers'] as $y):
    $columnarray = array_chunk($_POST['aantalpers'], 1);
    try {
        $sheet->fromArray($columnarray, NULL, 'C22');
    } catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
        die('Error loading file: '.$e->getMessage());
    }
    $x = $x + 1;
    $z = 22 + $x;
    $sheet->setCellValue('C'.$z,array_sum($_POST['aantalpers']));
endforeach;
$x=0;

foreach ($_POST['totaalpers'] as $y):
    $columnarray2 = array_chunk($_POST['totaalpers'], 1);
    try {
        $sheet->fromArray($columnarray2, NULL, 'D22');
    } catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
        die('Error loading file: '.$e->getMessage());
    }
    $x = $x + 1;
    $sheet->setCellValue('A'.$z,"totaal");
    $sheet->setCellValue('D'.$z,array_sum($_POST['totaalpers']));
    endforeach;
$x=0;

foreach ($_POST['datearr'] as $y):
    $columnarray1 = array_chunk($_POST['datearr'], 1);
    try {
        $sheet->fromArray($columnarray1, NULL, 'B22');
    } catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
        die('Error loading file: '.$e->getMessage());
    }
    $x = $x + 1;
endforeach;
$x=0;

try {
    $writer = IOFactory::createWriter($spreadsheet, "Xlsx");
} catch (\PhpOffice\PhpSpreadsheet\Writer\Exception $e) {
        die('Error loading file: '.$e->getMessage());
}
try {
    $writer->save("../assets/" . $filename . ".xlsx");
} catch (\PhpOffice\PhpSpreadsheet\Writer\Exception $e) {
        die('Error loading file: '.$e->getMessage());
}

include_once "mail.php";
