<?php  
    $startDate=$startTime=$reportDate=$reportTime=$pass=$explanation="";
    if(isset($_POST['submit']))
    {
        $startDate = mysql_real_escape_string($_POST['startDate']);
        $reportDate = mysql_real_escape_string($_POST['reportDate']);
        $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($currentDate > $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">Your leave start date must be greator than today\'s date</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 leave start date</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 Leave Type</span>
                </div>';
        }else
        {

            //generate course code
            if(!$insert = mysql_query("INSERT INTO school_leave (leaveID,staffID,applied,from_date,to_date,description,status)VALUES('$pass','$staffID',NOW(),'$startDate','$reportDate','$explanation','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">'.mysql_error().'</span>
                    </div>';
            }else
            {
                $pass="";
                $_SESSION['success']="true";
                header('Location:leave.php');
                echo "<script type='text/javascript'> document.location = 'leave.php'; </script>";
                exit();
            }
                
        }
       
    }  
?>