include 'sessionmanagement.php';
// If user is not an adminstrator or a manager redirect them back to the home
// page.
if (!$isManager and !$isAdministrator) {
    header('Location: index.php');
    exit;
}
// If user has clicked the submit button, try and create the ad hoc request in
// the database.
if (isset($_POST["submit"])) {
    ClearStatus();
    $employeeID = NULL;
    if (isset($_POST["employeeID"])) {
        $employeeID = $_POST["employeeID"];
    }
    $request = CreateAdHocAbsenceRequest($employeeID, $_POST["startDate"], $_POST["endDate"], $_POST["absenceType"]);
}
// If user has clicked the amend button, redirect them to the edit adhoc absence
// page, using a GET parameter with the ID of the record to edit.
if (isset($_POST["amend"])) {
    ClearStatus();
    $url = "Location:editAdHocAbsenceRequest.php?ID=" . $_POST["amend"] . "&back=adminAdHocAbsenceRequest.php";
    header($url);
}
// If user has clicked the delete button, delete the record from the table.
if (isset($_POST["delete"])) {
    ClearStatus();
    DeleteAdHocAbsenceRequest($_POST["delete"]);
}
//-----------------------------------------------------------------------------
// This function will generate the HTML necessary for the Employee select
function CreateNewDatabase($destroyExistingDB = false, $createWithTestData = false)
{
    if ($destroyExistingDB) {
        DropDB();
    }
    CreateDB();
    UseDB();
    CreateDateTable();
    CreatePublicHolidayTable();
    CreateAbsenceTypeTable();
    CreateCompanyRoleTable();
    CreateEmployeeTable();
    CreateApprovedAbsenceBookingTable();
    CreateApprovedAbsenceDateTable();
    CreateAdHocAbsenceRequestTable();
    CreateMainVacationRequestTable();
    //    CreateDefaultRecords("admin", "*****@*****.**","Zaq12wsx","2015-01-01", 19);
    if ($createWithTestData) {
        $annualLeave = CreateAbsenceType("Annual Leave", 1, 1);
        $training = CreateAbsenceType("Training", 0, 1);
        $sickness = CreateAbsenceType("Sickness", 0, 0);
        $compasionate = CreateAbsenceType("Compasionate Leave", 0, 1);
        $cashier = CreateCompanyRole("Cashier", 3);
        $customerAdvisor = CreateCompanyRole("Customer Advisor", 2);
        $manager = CreateCompanyRole("Manager", 1);
        $filter[COMP_ROLE_NAME] = "Admin";
        $results = RetrieveCompanyRoles($filter);
        $admin = $results[0];
        $steveBrookstein = CreateEmployee("Steve Brookstein", "*****@*****.**", "Zaq12wsx", "2005-01-01", 20, NULL, $cashier[COMP_ROLE_ID], 0, 0);
        $shayneWard = CreateEmployee("Shane Ward", "*****@*****.**", "Zaq12wsx", "2006-01-01", 20, NULL, $cashier[COMP_ROLE_ID], 0, 0);
        $leonJackson = CreateEmployee("Leon Jackson", "*****@*****.**", "Zaq12wsx", "2008-01-01", 20, NULL, $manager[COMP_ROLE_ID], 0, 0);
        $alexandraBurke = CreateEmployee("Alexandra Burke", "*****@*****.**", "Zaq12wsx", "2009-01-01", 20, NULL, $cashier[COMP_ROLE_ID], 0, 0);
        $joeMcElderry = CreateEmployee("Joe McElderry", "*****@*****.**", "Zaq12wsx", "2010-01-01", 20, NULL, $customerAdvisor[COMP_ROLE_ID], 0, 0);
        $mattCardle = CreateEmployee("Matt Cardle", "*****@*****.**", "Zaq12wsx", "2011-01-01", 20, NULL, $customerAdvisor[COMP_ROLE_ID], 0, 0);
        $jamesArthur = CreateEmployee("James Arthur", "*****@*****.**", "Zaq12wsx", "2012-01-01", 20, NULL, $customerAdvisor[COMP_ROLE_ID], 0, 0);
        $samBailey = CreateEmployee("Sam Bailey", "*****@*****.**", "Zaq12wsx", "2013-01-01", 20, NULL, $customerAdvisor[COMP_ROLE_ID], 0, 0);
        $benHaenow = CreateEmployee("Ben Haenow", "*****@*****.**", "Zaq12wsx", "2014-01-01", 20, NULL, $manager[COMP_ROLE_ID], 0, 1);
        $dates = RetrieveDates();
        if (count($dates) == 0) {
            date_default_timezone_set('UTC');
            // Start date
            $date = '2015-01-01';
            // End date
            $end_date = '2055-12-31';
            while (strtotime($date) <= strtotime($end_date)) {
                CreateDate($date, NULL);
                $date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
            }
        }
        $dateID = RetrieveDateIDByDate("2015-04-03");
        $goodFriday = CreatePublicHoliday("Good Friday", $dateID);
        $dateID = RetrieveDateIDByDate("2015-04-06");
        $easterMonday = CreatePublicHoliday("Easter Monday", $dateID);
        $dateID = RetrieveDateIDByDate("2015-05-04");
        $earlyMay = CreatePublicHoliday("Early May Bank Holiday", $dateID);
        $dateID = RetrieveDateIDByDate("2015-05-25");
        $springHoliday = CreatePublicHoliday("Spring Bank Holiday", $dateID);
        $dateID = RetrieveDateIDByDate("2015-08-31");
        $summerHoliday = CreatePublicHoliday("Summer Bank Holiday", $dateID);
        $dateID = RetrieveDateIDByDate("2015-12-25");
        $christmasDay = CreatePublicHoliday("Christmas Day", $dateID);
        $dateID = RetrieveDateIDByDate("2015-12-28");
        $boxingDay = CreatePublicHoliday("Boxing Day (substitute day)", $dateID);
        $request = CreateMainVactionRequest($steveBrookstein[EMP_ID], "2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
        $request = CreateMainVactionRequest($shayneWard[EMP_ID], "2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
        $request = CreateMainVactionRequest($leonJackson[EMP_ID], "2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
        $request = CreateMainVactionRequest($alexandraBurke[EMP_ID], "2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
        $request = CreateMainVactionRequest($joeMcElderry[EMP_ID], "2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
        $request = CreateMainVactionRequest($jamesArthur[EMP_ID], "2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
        $request = CreateMainVactionRequest($mattCardle[EMP_ID], "2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
        $request = CreateMainVactionRequest($samBailey[EMP_ID], "2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
        $request = CreateMainVactionRequest($benHaenow[EMP_ID], "2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
        $request = CreateAdHocAbsenceRequest($steveBrookstein[EMP_ID], "2015-03-10", "2015-03-15", $annualLeave[ABS_TYPE_ID]);
        $request = CreateAdHocAbsenceRequest($shayneWard[EMP_ID], "2015-03-10", "2015-03-15", $annualLeave[ABS_TYPE_ID]);
        $request = CreateAdHocAbsenceRequest($leonJackson[EMP_ID], "2015-03-10", "2015-03-15", $sickness[ABS_TYPE_ID]);
        $request = CreateAdHocAbsenceRequest($alexandraBurke[EMP_ID], "2015-03-10", "2015-03-15", $training[ABS_TYPE_ID]);
        $request = CreateAdHocAbsenceRequest($joeMcElderry[EMP_ID], "2015-03-10", "2015-03-15", $training[ABS_TYPE_ID]);
        $request = CreateAdHocAbsenceRequest($mattCardle[EMP_ID], "2015-03-10", "2015-03-15", $training[ABS_TYPE_ID]);
        $request = CreateAdHocAbsenceRequest($jamesArthur[EMP_ID], "2015-03-10", "2015-03-15", $training[ABS_TYPE_ID]);
        $request = CreateAdHocAbsenceRequest($samBailey[EMP_ID], "2015-03-10", "2015-03-15", $compasionate[ABS_TYPE_ID]);
        $request = CreateAdHocAbsenceRequest($benHaenow[EMP_ID], "2015-03-10", "2015-03-15", $compasionate[ABS_TYPE_ID]);
        $leonaLewis = CreateEmployee("Leona Lewis", "*****@*****.**", "Zaq12wsx", "2007-01-01", 20, NULL, $admin[COMP_ROLE_ID], 1, 1);
        $request = CreateAdHocAbsenceRequest($leonaLewis[EMP_ID], "2015-03-10", "2015-03-15", $sickness[ABS_TYPE_ID]);
        $request = CreateMainVactionRequest($leonaLewis[EMP_ID], "2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
        $request = CreateApprovedAbsenceBooking($leonaLewis[EMP_ID], "2015-04-01", "2015-04-10", $training[ABS_TYPE_ID]);
    }
}