<?php
chdir('../../');
require_once 'autoload.php';

$courses = new courses_controller;

require_once 'simplexlsx/vendor/autoload.php';

use Shuchkin\SimpleXLSX;

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['csrf_token']) && isset($_POST['csrf_form_id']) && $waf->csrf_validate($_POST['csrf_form_id'], $_POST['csrf_token'], $utility->action_name())) {

$file = $_FILES['file']['tmp_name'];
$current_record = 0;

if ( $xlsx = SimpleXLSX::parse($file) ) {

foreach( $xlsx->rows() as $row ) {

if ($current_record !== 0) {

$data['programme_id'] = $utility->sanitize($_POST['programme_id']);
$data['code'] = $utility->sanitize(ucwords($row[0]));
$data['name'] = $utility->sanitize(ucwords($row[1]));
$data['fee'] = $utility->sanitize(ucwords($row[2]));
$data['status'] = $utility->sanitize($row[3]);

if (empty($courses->check($data['name']))) {

$courses->store($data);

}
}
$current_record++;
}

$utility->setMessage("success", 'Data import completed');

header("location:../courses");

} else {

echo SimpleXLSX::parseError();

}
}else{

header("location:../courses");

}
?>
