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

	include 'include/header.php';
?>

    <script>
        var myCampus = '';
        var myBuilding = '';
        var myRoom = '';
        var timer;

        $(function() {
            getCampusOptions();
            startTimer();
        });
        
        function startTimer(){
            getPropertiesTable(myRoom);
        
            timer = setInterval(function(){
                getPropertiesTable(myRoom);
            }, 5000);
        }
        
        function stopTimer(){
            clearInterval(timer);
        }

        function handleCheck(cb, ndx, col) {
          setTimeout(function() {
                if (cb.checked == true){
                    var reslt = 1;
                }else{
                    var reslt = 0;
                }
                propertyBlurred(ndx, col, reslt);
            }, 0);
        }

        function propChange(pCol, ndx){
            var pNewVal = document.getElementById(ndx + pCol).value; // new val to be placed into db
            propertyBlurred(ndx, pCol, pNewVal);
        }

        function resetAlert(id){
            $.post(
                'resetAlert.php',
                { id: id },
				function(data) {
					getPropertiesTable(myRoom);
				}
            );
        }

        function propertyBlurred(ndx, propCol, propVal){ // property edited
            $.post(
                'updatePropertyCol.php',
                {
                    id: ndx,
                    column: propCol,
                    value: propVal
                }
            );
        }

        function deviceBlurred(devNdx){
            var dvName = document.getElementById(devNdx).value;
            $.post(
                "updateDeviceAltName.php",
                {
                    id: devNdx,
                    altname: dvName
                }
            );
        }

        function getSelectedText(elementId) {
            var elt = document.getElementById(elementId);

            if (elt.selectedIndex == -1)
                return null;

            return elt.options[elt.selectedIndex].text;
        }

        function selectClick(selectedList) {
            var elt = document.getElementById(selectedList);
            if (elt.selectedIndex == -1)
                return null;
            var mySelection = elt.options[elt.selectedIndex].text;

            if ( selectedList == 'campusList' ) {
                myCampus = mySelection;
                getBuildingOptions(myCampus);
                //getRoomOptions(myBuilding);
                //document.getElementById('buildingList').focus();
            }
            if ( selectedList == 'buildingList' ) {
                myBuilding = mySelection;
                getRoomOptions(myBuilding);
            }
            if ( selectedList == 'roomList' ) {
                myRoom = mySelection;
                getPropertiesTable(myRoom);
                //getCommandButtons(myRoom);
            }
            return mySelection;
        }

        function getCampusOptions(){
            $.get(
                "getCampusOptions.php",
				{'editable': true},
                function(data,status) {
                    $('#dvCampusList').html(data);
                    myCampus = getSelectedText('campusList');
                    getBuildingOptions(myCampus);
                }
            );
        }

        function getBuildingOptions(sCampus){
            $.get(
                "getBuildingOptions.php",
                {
					campus: sCampus,
					'editable': true
				},
                function(data,status) {
                    $('#dvBuildingList').html(data);
                    myBuilding = getSelectedText('buildingList');
                    getRoomOptions(myBuilding);
                }
            );
        }

        function getRoomOptions(sBuilding){
            $.get(
                "getRoomOptions.php",
                {
                    campus: myCampus,
                    building: sBuilding,
					'editable': true
                },
                function(data,status) {
                    $('#dvRoomList').html(data);
                    myRoom = getSelectedText('roomList');
                    getPropertiesTable(myRoom);
                }
            );
        }

        function getPropertiesTable(prpRoom) {
            $.get(
                "getPropertiesTable.php",
                {
                    campus: myCampus,
                    building: myBuilding,
                    room: prpRoom
                },
                function(data,status) {
                    $('#propertiesTable').html(data);
                    getCommandButtons(myRoom);
                    
                    $("input[type=text]").focus(function(){
                        stopTimer();
                    });
                    $("input[type=text]").blur(function(){
                        startTimer();
                    });
                }
            );
        }

        function getCommandButtons(prpRoom) {
            $.get(
                "getCommandButtons.php",
                {
                    campus: myCampus,
                    building: myBuilding,
                    room: prpRoom
                },
                function(data,status) {
                    $('#commandButtons').html(data);
                }
            );
        }

        function cmdBtnClick(button_id) {
            var click = confirm("Are you *sure* you want to send this command?");
            if (click){
                $.post(
                    "cmdButtonClick.php",
                    { button_id: button_id }
                );
            }
        }

        function deleteCampus() {
            var ans = confirm("Delete "+myCampus+"?");
            if(ans) {
                $.post(
                    "deleteCampus.php",
                    { campus: myCampus },
                    function(data, status) {
                        getCampusOptions();
                    }
                );
            }
        }

        function editCampus() {
            var ans = window.prompt("Edit "+myCampus+"?", myCampus);
            if(ans && ans != myCampus) {
                $.ajax({
                    type: 'POST',
                    url: 'editCampus.php',
                    data: {
                        campus: myCampus,
                        newName: ans
                    },
                    success: function(data, status) {
                        getCampusOptions();
                    },
                    statusCode: {
                        409: function() {
                            alert('That campus already exists!');
                        }
                    }
                });
            }
        }

        function deleteBuilding() {
            var ans = confirm("Delete "+myBuilding+"?");
            if(ans) {
                $.post(
                    "deleteBuilding.php",
                    {
                        campus: myCampus,
                        building: myBuilding
                    },
                    function(data, status) {
                        getBuildingOptions(myCampus);
                    }
                );
            }
        }

        function editBuilding() {
            var ans = window.prompt("Edit "+myBuilding+"?", myBuilding);

            if(ans && ans != myBuilding) {
                $.ajax({
                    type: 'POST',
                    url: 'editBuilding.php',
                    data: {
                        campus: myCampus,
                        building: myBuilding,
                        newName: ans
                    },
                    success: function(data, status) {
                        getBuildingOptions(myCampus);
                    },
                    statusCode: {
                        409: function() {
                            alert('That building already exists!');
                        }
                    }
                });
            }
        }

        function deleteRoom() {
            var ans = confirm("Delete "+myRoom+"?");
            if(ans) {
                $.post(
                    "deleteRoom.php",
                    {
                        campus: myCampus,
                        building: myBuilding,
                        room: myRoom
                    },
                    function(data,status) {
                        getRoomOptions(myBuilding);
                    }
                );
            }
        }

        function editRoom() {
            var ans = window.prompt("Edit "+myRoom+"?", myRoom);
            if(ans && ans != myRoom) {
                $.ajax({
                    type: 'POST',
                    url: 'editRoom.php',
                    data: {
                        campus: myCampus,
                        building: myBuilding,
                        room: myRoom,
                        newName: ans
                    },
                    success: function(data, status) {
                        getRoomOptions(myBuilding);
                    },
                    statusCode: {
                        409: function() {
                            alert('That room already exists!');
                        }
                    }
                });
            }
        }

        function deleteDevice(id, devName) {
            var ans = confirm("Delete " + devName + "?");
            if(ans) {
                $.post(
                    "deleteDevice.php",
                    {
                        deviceID: id
                    },
                    function(data,status) {
                        getPropertiesTable(myRoom);
                    }
                );
            }
        }

        function deleteProperty(PropID, devId, devName, propName) {
            var ans = confirm("Delete " + propName + "?");
            if(ans) {
                $.post(
                    "deleteProperty.php",
                    {
                        propertyID: PropID
                    },
                    function(data,status) {
                        getPropertiesTable(myRoom);
                    }
                );
            }
        }
    </script>

    <div class="wrap">

        <h1 class="pad">USM - Room View and Control</h1>

        <div id="nav">
            <a href="/">Home</a>
            <a href="roomList.php" class="selected">Room View</a>
            <a href="reports.php">Reports</a>
            <a href="admin.php">Administration</a>
            <a href="logout.php">Logout</a>
        </div>

        <hr>

        <div id="leftmenu">
            <h2>Campus</h2>
            <div id="dvCampusList"></div>

            <h2>Building</h2>
            <div id="dvBuildingList"></div>

            <h2>Room</h2>
            <div id="dvRoomList"></div>
        </div>

        <div id="properties" class="center pad-bottom">
            <div id="propertiesTable"></div>
            <div id="commandButtons"></div>
        </div>

    </div>

<?php include('include/footer.php'); ?>