<?php

require_once '../core/init.php';
if (Input::exists() && privilege() != NULL) {
    $validate = new Validate();
    $validation = $validate->check($_POST, array('course_id' => array('required' => true), 'examtype' => array('required' => true), 'category' => array('required' => true)));
    if ($validate->passed()) {
        $m = new Marks();
        $lastdate = $m->getLastDate(Input::get('examtype'))->fetch_object()->date;
        $today = date('Y-m-d');
        if ($today >= $lastdate) {
            $date = date_create($lastdate);
            echo '<div class="alert alert-danger alert-dismissible" role="alert">';
            echo '<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>';
            echo "Last Date for " . strtoupper(Input::get('examtype')) . " was " . date_format($date, 'd-M-Y');
            echo '</div>';
            die;
        }
        if (empty($_FILES["file"]["tmp_name"])) {
            echo '<div class="alert alert-danger alert-dismissible" role="alert">';
            echo '<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>';
            echo 'Please Select CSV File';
            echo '</div>';
            die;
        }
        $input = explode(',', Input::get('course_id'));
        $m = new Marks();
        $import = $m->importCSV($input[0], $input[1], Input::get('examtype'), Input::get('category'), $_FILES["file"]["tmp_name"]);
        if ($import) {
            echo '<div class="alert alert-success alert-dismissible" role="alert">';
            echo '<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>';
<?php

require_once '../core/init.php';
if (!Input::exists('get')) {
    die;
}
if (privilege() == NULL) {
    die;
} else {
    if (Input::get('examtype') == '') {
        ?>
	<input type="text" placeholder="mm/dd/yyyy" class="form-control" data-provide="datepicker" data-inputmask="'alias': 'mm/dd/yyyy'" data-mask="" name="lastdate" id="lastdate">
<?php 
    } else {
        $dep = new Marks();
        $last_date = $dep->getLastDate(Input::get('examtype'));
        if ($last_date->num_rows) {
            $last_date = changeDateFormatFromDB($last_date->fetch_object()->date);
            if ($last_date == '00/00/0000') {
                $last_date = '';
            }
        } else {
            $last_date = '';
        }
        ?>
<input type="text" placeholder="mm/dd/yyyy" class="form-control" data-provide="datepicker" data-inputmask="'alias': 'mm/dd/yyyy'" data-mask="" name="lastdate" id="lastdate" value="<?php 
        echo $last_date;
        ?>
">
<?php 
    }