<?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: /' );
        $db = null;
        die;
	}
?>

    <script>
        function saveCmdProc() {
            $.post(
                'setCmdProc.php',
                {
                    campus: myCampus,
                    building: myBuilding,
                    room: myRoom,
                    proc: $("#listCmdProcs").val(),
                    arguments: $("#cmdProcArgs").val()
                }
            );
        }
        
        function getAssociatedButtons(){
            $.get(
                'admin_showAssociatedButtons.php',
                {
                    campus: myCampus,
                    building: myBuilding,
                    room: myRoom
                },
                function(data) {
                    $('#associatedButtons').html(data);
                }
            );
        }
        
        function getCmdProcOptions() {
            $.get(
                "getCmdProcOptions.php",
                {
                    campus: myCampus,
                    building: myBuilding,
                    room: myRoom
                },
                function(data){
                    $("#listCmdProcs").html(data); 
                    getCmdProcArgs();
                }
            );
        }
        
        function getCmdProcArgs() {
            $.get(
                "getCmdProcArgs.php",
                {
                    campus: myCampus,
                    building: myBuilding,
                    room: myRoom
                },
                function(data){
                    $("#cmdProcArgs").val(data); 
                }
            );
        }
        
        function deleteCmdButton(ndx) {
            var is_delete = confirm("Are you sure you want to delete this Button?!");
            if (is_delete) {
                $.post(
                    "deleteCmdButton.php", 
                    { id: ndx },
                    function(data) {
                        getAssociatedButtons();
                    }
                );
            }

        }
        
        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 saveButton() {
            $.post(
                'admin_saveBtnCommand.php', 
                {
                    campus: myCampus,
                    building: myBuilding,
                    room: myRoom,
                    group_name: $('#groupName').val(),
                    button_name: $('#buttonName').val(),
                    command: $('#buttonCommand').val()
                },
                function(data) {
                    getAssociatedButtons();
                }
            );
        }
        
        $(function() {
            getCampusOptions();
            getAssociatedButtons();
        });
    </script>

		<div align="center" ><h2>Button Editor</h2></div>
		<div id="boxWrap" align="center" >
			<div id="pickTable">
				<div id="dvCampusList">
					<label for="campus">Campus:</label>
					<div id="campus"></div>
				</div>
				<div id="dvBuildingList">
					<label for="building">Building:</label>
					<div id="building"></div>
				</div>
				<div id="dvRoomList">
					<label for="room">Room:</label>
					<div id="room"></div>
				</div>
                    	</div>

			<div id="pickTable">
				<div id="dvCmdProc">
					<label for="listCmdProcs">Command Processor:</label>
					<select id="listCmdProcs">
					</select>
				</div>
				<div id="dvCmdArgs">
					<label for="cmdProcArgs">Arguments:</label>
					<input id="cmdProcArgs"/>
				</div>
                <button onclick="saveCmdProc()">Save</button>
			</div>

			<div id="pickTable">
				<div>
					<label for="groupName">Group Name: </label>
					<input id="groupName" />
				</div>
				<div>
					<label for="buttonName">Button Name: </label>
					<input id="buttonName" />
				</div>
				<div>					
					<label for="buttonCommand">Command: </label>
					<input id="buttonCommand" />
				</div>					
				<button id="newButtonSave" onclick="saveButton()">Save</button> 
			</div>

			<div id="associatedButtons"></div>
		</div>