<?php
    session_start();
	if ( empty($_SESSION['login']) ) {
        header( 'Location: login.php' );
        die;
	}

    include 'include/database.php';

    $connection = connect($database);

    $canAdmin = canAdministrate($connection, $_SESSION['login']);

    if ($canAdmin) {
        $selectAll = 'Select All';

        $campus = $_POST['campus'];
        $building = $_POST['building'];
        $room = $_POST['room'];
        
        $campus_id = ($campus === $selectAll) ? 0 : getCampusId($connection, $campus);
        $building_id = ($building === $selectAll) ? 0 : getBuildingId($connection, $campus, $building);
        $room_id = ($room === $selectAll) ? 0 : getRoomId($connection, $campus, $building, $room);

        $event_date = "{$_POST['year']}/{$_POST['month']}/{$_POST['day']}";
        $event_time = "{$_POST['hour']}:{$_POST['minute']}:00";
        
        insertUpdateTask(
            $connection, $campus_id, $building_id, $room_id,
            $_POST['taskname'], $_POST['buttongroup'], $_POST['buttonname'],
            $_POST['byday'], $event_date, $event_time, $_POST['days']
        );
    } else {
        header( 'Location: /' );
        $connection = null;
        die;
    }

    $connection = null;
?>