<?php  
    $startDate=$startTime=$reportDate=$reportTime=$pass=$explanation="";
    if(isset($_POST['submit']))
    {
        $startDate = mysql_real_escape_string($_POST['startDate']);
        $startTime = mysql_real_escape_string($_POST['startTime']);
        $reportDate = mysql_real_escape_string($_POST['reportDate']);
        $reportTime = mysql_real_escape_string($_POST['reportTime']);
        $pass = mysql_real_escape_string($_POST['reason']);
        $explanation = mysql_real_escape_string($_POST['explanation']);
        $time = round(abs(strtotime($reportTime) - strtotime($startTime)))/60;
        $currentDate = date("Y-m-d G:i:s");
        
        if(!$startDate){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Enter start date</span>
                </div>';
        }else
        if(!$startTime){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Enter start time</span>
                </div>';
        }else 
        if($currentDate > ($startDate.' '.$startTime)){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Your begin date and time must be greator than the current date and time</span>
                </div>';
        }else
        if(!$reportDate){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Enter reporting date</span>
                </div>';
        }else
        if($reportDate < $startDate){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Reporting date cannot be before the pass start date</span>
                </div>';
        }else    
        if(!$reportTime){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Enter reporting time</span>
                </div>';
        }else 
        if($reportDate == $startDate && $reportTime <= $startTime){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">You choose a less than one day gate pass. Your report time should be after the start time.</span>
                </div>';
        }else 
        if($reportDate == $startDate && $time < 60){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">You choose a less than one day gate pass. Your gate pass time should be at least one hour.</span>
                </div>';
        }else 
        if(!$pass){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Select Gate Pass Type</span>
                </div>';
        }else
        {
            /*if(!$select = mysql_query("SELECT * FROM gate_pass WHERE studentID = '$studentID' ")){
                    echo'<div class="alert alert-danger absolue center text-center" role="alert">
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">×</span>
                            </button>
                            <span class="text-danger">'.mysql_error().'</span>
                        </div>';
            }else{
                $countrow=  mysql_num_rows($select);
                if($countrow > 0){
                    echo'<div class="alert alert-danger absolue center text-center" role="alert">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">×</span>
                                </button>
                                <span class="text-danger">Error: Gate Pass Type already exists!</span>
                            </div>';
                }else
                {*/
                    $start = date_format(date_create($startDate),"Y-m-d")." ".$startTime.":00";
                    $end = date_format(date_create($reportDate),"Y-m-d")." ".$reportTime.":00";
                           
                    //generate course code
                    if(!$insert = mysql_query("INSERT INTO gate_pass (date_applied,studentID,start_date,end_date,passID,explanation)VALUES(NOW(),'$studentID','$start','$end','$pass','$explanation')")){
                        echo'<div class="alert alert-danger absolue center text-center" role="alert">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">×</span>
                                </button>
                                <span class="text-danger">'.mysql_error().'</span>
                            </div>';
                    }else
                    {
                        $pass="";
                        $_SESSION['success']="true";
                        header('Location:pass.php');
                        echo "<script type='text/javascript'> document.location = 'pass.php'; </script>";
                        exit();
                    }
                //}    
            //}
        }
       
    }  
?>