<?php
	session_start();
    
	include 'myUniversalVars.php';
	include 'myDBFunctions.php';
	
	// if this user isn't logged in then...
	if(!isset($_SESSION['login'])){ // redirect to login page...
		header( 'Location: login.php' ) ;
	}
	
	$db = dbConnect();
	
	$perms = getPermissions($db, $_SESSION['login']);
	if (!($perms & $pmAdmin)){
        header( 'Location: /' );
        $connection = null;
        die;
	}
?>

    <script>
        function getAssociatedTasks(){
            $.get(
                'admin_showAssociatedTasks.php', 
                {
                    campus: myCampus, 
                    building: myBuilding,
                    room: myRoom
                },
                function(data,status) {
                    $('#associatedTasks').html(data);
                }
            );
        }
        
        function hourChange(){
            myHour = document.getElementById('hour').value;
            myMinute = document.getElementById('minute').value;
            myAMPM = getSelectedText('ampm');

            if (parseInt(myHour) > 12){ 
                myHour = (parseInt(myHour)-12)+''; 
                document.getElementById('hour').InnerHTML = myHour;
                setSelectedText("ampm", 1);
            }else{
                setSelectedText("ampm", 0);
            }
        }
        
        function clearEvent(){
            $('#taskName').val('');
            $('#btnGroup').val('');
            $('#btnName').val('');
            $('#hour').val('');
            $('#minute').val('');
            $('#ampm option')[0].selected = true;
            $('#byDay').prop('checked', true);
            $("#byDayBox").show();
            $("#byDateBox").hide();
            $('#sun').prop('checked', false);
            $('#mon').prop('checked', false);
            $('#tue').prop('checked', false);
            $('#wed').prop('checked', false);
            $('#thu').prop('checked', false);
            $('#fri').prop('checked', false);
            $('#sat').prop('checked', false);
            $('#year').val('');
            $('#month').val('');
            $('#day').val('');
        }
        
        function deleteTask(id) {
            var is_delete = confirm("Are you sure you want to delete this task?");
            if (is_delete) {
                $.post(
                    "deleteScheduledTask.php", 
                    { id: id },
                    function(data) {
                        getAssociatedTasks();
                    }
                );
            }
        }
        
        function saveEvent() {
            // if (has all values) {
                var days = 0;
            
                if ($('#sun').is(':checked')) { days = days | 64; }
                if ($('#mon').is(':checked')) { days = days | 32; }
                if ($('#tue').is(':checked')) { days = days | 16; }
                if ($('#wed').is(':checked')) { days = days | 08; }
                if ($('#thu').is(':checked')) { days = days | 04; }
                if ($('#fri').is(':checked')) { days = days | 02; }
                if ($('#sat').is(':checked')) { days = days | 01; }
                
                var ampm = getSelectedText('ampm');
                var hour = $('#hour').val();
                var minute = $('#minute').val();

                if ( ampm === "PM" && hour < 12) {
                    hour = parseInt(hour) + 12;
                }
                
                var isByDay = $('#byDay').is(':checked') ? 1 : 0;
                
                $.post(
                    'admin_setScheduleEvent.php', 
                    {
                        taskname: $('#taskName').val(), 
                        campus: getSelectedText('campusList'),
                        building: getSelectedText('buildingList'),
                        room: getSelectedText('roomList'),
                        buttongroup: $('#btnGroup').val(),
                        buttonname: $('#btnName').val(), 
                        year: $('#year').val(),
                        month: $('#month').val(),
                        day: $('#day').val(),
                        days: days,
                        hour: hour,
                        minute: minute,
                        byday: isByDay
                    },
                    function(data) {
                        getAssociatedTasks();
                    }
                );
            // }
        }
        
        $(function() {
            getCampusOptions(true);     // select_all=true;
            getAssociatedTasks();

            //Time pickers
            $( "#hour" ).spinner({
                spin: function( event, ui ) {
                    if ( ui.value > 12 ) {
                    $("#ampm").prop("selectedIndex", !$("#ampm").prop("selectedIndex"));	//flip am, pm
                    $( this ).spinner( "value", 1 );
                        return false;
                    } else if ( ui.value < 1 ) {
                    $("#ampm").prop("selectedIndex", !$("#ampm").prop("selectedIndex"));
                        $( this ).spinner( "value", 12 );
                        return false;
                    }
                }
            });

            $( "#minute" ).spinner({
                spin: function( event, ui ) {
                    if ( ui.value > 59 ) {
                        $( this ).spinner( "value", 0 );
                        return false;
                    } else if ( ui.value < 0 ) {
                        $( this ).spinner( "value", 59 );
                        return false;
                    }
                }
            });

            //Date Pickers	
            $( "#year" ).spinner({
                spin: function( event, ui ) {
                    if ( ui.value > 2100 ) {
                        $( this ).spinner( "value", 1969 );
                        return false;
                    } else if ( ui.value < 1969 ) {
                        $( this ).spinner( "value", 2100 );
                        return false;
                    }
                }
            });
            
            $( "#month" ).spinner({
                spin: function( event, ui ) {
                    if ( ui.value > 12 ) {
                        $( this ).spinner( "value", 1 );
                        return false;
                    } else if ( ui.value < 1 ) {
                        $( this ).spinner( "value", 12 );
                        return false;
                    }
                }
            });
            
            $( "#day" ).spinner({
                spin: function( event, ui ) {
                    if ( ui.value > 31 ) {
                        $( this ).spinner( "value", 1 );
                        return false;
                    } else if ( ui.value < 1 ) {
                        $( this ).spinner( "value", 31 );
                        return false;
                    }
                }
            });

            //Box Hide
            $("#byDay").click(function() {
                $("#byDayBox").show();
                $("#byDateBox").hide();
            });

            $("#byDate").click(function() {
                $("#byDateBox").show();
                $("#byDayBox").hide();
            });
        });
    </script>

    <!--Add any additional stylesheets/scripts above-->

        <div id="boxWrap" align="center" >
    <!--Campus/Building/Room Picker-->
                <div id="pickTable">
                    <h2>Pick Room</h2>
                
                    <div id="dvCampusList">
                        <label for="campusList">Campus:</label>
                        <div id="campus"></div>
                    </div>

                    <div id="dvBuildingList">
                        <label for="buildingList">Building:</label>
                        <div id="building"></div>
                    </div>

                    <div id="dvRoomList">
                        <label for="roomList">Room:</label>
                        <div id="room"></div>
                    </div>
                </div>
        
            <!-- <form> -->
                <div id="pickTable">
                    <h2>New Task</h2>
                
                    <div>
                        <label for="taskName">Task Name: </label>
                        <input id="taskName" />
                    </div>

                    <div>
                        <label for="btnGroup">Button Group: </label>
                        <input type="text" id="btnGroup"> 
                    </div>
                    <div>
                        <label for="button">Button Name: </label>
                        <input type="text" id="btnName"> 
                    </div>

                    <div>
                        <span style="margin:0 20px;">
                            <label for="hour">Hour:</label>
                            <input style="font-size:12" id="hour" size="1" />
                        </span>
                        <span style="margin:0 20px;">						
                            <label for="minute">Minute:</label>
                            <input style="font-size:12" id="minute" size="1"/>
                        </span>
                        <!--<input id="ampm" size="1"></span>-->
                        <select id="ampm" class="ui-spinner ui-widget ui-widget-content ui-corner-all" style="margin:0 20px;">
                            <option value="am">AM</option>
                            <option value="pm">PM</option>
                        </select>
                    </div>
    <!--Recurrence Pickers-->
                    <div style="margin:25px 0;">
                        <span style="margin:20px;"><input type="radio" name="format" value="byDay" id="byDay" checked="true"> By Day</span>
                        <span style="margin:20px;"><input type="radio" class="Recurrence" name="format" value="byDate" id="byDate"> By Date</span>
                    </div>
                    <!-- <a style="float: right" class="byDaySpace"><input type="checkbox"> Repeat</a> -->

                    <div>
                        <span id="byDayBox">
                            <span><input id="sun" type="checkbox">Sun</span>
                            <span><input id="mon" type="checkbox">Mon</span>
                            <span><input id="tue" type="checkbox">Tue</span>
                            <span><input id="wed" type="checkbox">Wed</span>
                            <span><input id="thu" type="checkbox">Thu</span>
                            <span><input id="fri" type="checkbox">Fri</span>
                            <span><input id="sat" type="checkbox">Sat</span>
                        </span>

                        <span id="byDateBox" style="display: none;">
                            <span>
                                <label for="year">Year:</label>
                                <input style="font-size:12" id="year" size="3"/>
                            </span>
                            <span>
                                <label for="month">Month:</label>
                                <input style="font-size:12" id="month" size="1"/>
                            </span>
                            <span>
                                <label for="day">Day:</label>
                                <input style="font-size:12" id="day" size="1"/>
                            </span>
                        </span>
                    </div>

                    <!-- <a style="float: right" class="byDaySpace"><input id="repeat" type="checkbox"> Repeat</a> -->
                    <button onclick="saveEvent()">Save</button>
                    <button onclick="clearEvent()">Clear</button>
            <!-- </form> -->
    <!--Results Table-->
            </div>
            
            <div class="pad-top">
                <h2>Task List</h2>
                <div id="associatedTasks"></div>
            </div>
        </div> <!--End boxWrap-->