<?php 
if(file_exists('../../'.$_REQUEST['tenant'].'/config.php')){require_once('../../'.$_REQUEST['tenant'].'/config.php');}
if(file_exists('/config.php')){require_once('/config.php'); }
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
switch ($_REQUEST['FCN']){
default:break;
case "changeSec":
if($_REQUEST['type']=="admins"){$t="id";}
if($_REQUEST['type']=="surveyor"){$t="id";}
if($_REQUEST['type']=="agents"){$t="agent_number";}
$stmt = $GLOBALS['con']->prepare("UPDATE `".$_REQUEST['type']."` SET `access`='".$_REQUEST['sec']."' WHERE `".$t."`='".$_REQUEST['id']."' ")or die(mysqli_error($GLOBALS['con']));$stmt->execute();$a=$stmt->get_result();$stmt->close();

$stmt = $GLOBALS['con']->prepare("INSERT `log` VALUES('','".$_SESSION['usertype']."|".$_SESSION['userid']."','','".$_SESSION['user']." changed access privilages for ".$_REQUEST['name']."','".strtotime('now')."') ")or die(mysqli_error($GLOBALS['con']));$stmt->execute();$a=$stmt->get_result();$stmt->close();
exit();
break;
}
?>

<?php

if($_REQUEST['type']=="admins"){$t="id"; $target="admins";}
if($_REQUEST['type']=="surveyor"){$t="id"; $target="surveyor";}
if($_REQUEST['type']=="agents"){$t="agent_number"; $target="agents";}
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `".$_REQUEST['type']."` WHERE `".$t."`='".$_REQUEST['id']."' ")or die(mysqli_error($GLOBALS['con']));$stmt->execute();$a=$stmt->get_result();$stmt->close();
$i=mysqli_fetch_array($a);

switch ($_REQUEST['op']){
default:break;

case "seclev":?>
<h3>Change security level</h3>
<div class="row">
    <div class="col-md-3 <?php if($_REQUEST['type']=="admins"){?>offset-md-1<?php }?> text-center"><a href="javascript:void(0)" class="btn <?php if($i['access']==0){echo "btn-success";}else{echo "btn-warning";}?>"
           onClick="changeSec('<?= $_REQUEST['id']?>','0')">0</a><br>Suspend Access</div>

    <div class="col-md-3 text-center"><a href="javascript:void(0)" class="btn <?php if($i['access']==1){echo "btn-success";}else{echo "btn-secondary";}?>" onClick="changeSec('<?= $_REQUEST['id']?>','1')">1</a><br>Full administrative access
    </div>

    <?php if($_REQUEST['type']!="admins"){?>
    <div class="col-md-3 text-center"><a href="javascript:void(0)" class="btn <?php if($i['access']==2){echo "btn-success";}else{echo "btn-secondary";}?>" onClick="changeSec('<?= $_REQUEST['id']?>','2')">2</a><br>Elevated
        (<?= $_REQUEST['type']?>) access</div>

    <div class="col-md-3 text-center"><a href="javascript:void(0)" class="btn <?php if($i['access']==3){echo "btn-success";}else{echo "btn-secondary";}?>" onClick="changeSec('<?= $_REQUEST['id']?>','3')">3</a><br>Assigned
        (<?= $_REQUEST['type']?>) access</div>
    <?php }?>


</div>
<?php break;

case "edit":?>
<h3>Editing</h3>

<?php break;

case "remove":?>
<h3>Confirm removal</h3>

<?php break;	
}?>

<script>
$('#popupBox .modal-header').html(
    '<a href="javascript:void(0)" class="sa">Security Options: <?= $_REQUEST['name']?></a><div class="pull-right"><a href="javascript:void(0)" class="btn" onclick="cancel()"><i class="fad fa-window-close fa-2x" aria-hidden="true"></i></a></div>'
);

function changeSec(i, s, n) {
    $.post('functions/edit_user.php', {
        FCN: "changeSec",
        id: i,
        sec: s,
        type: "<?= $_REQUEST['type']?>",
        name: "<?= $_REQUEST['name']?>"
    }, function(data) {
        $('#<?= $_REQUEST['type']?>L' + i).load('functions/edit_user.php?op=<?= $_REQUEST['op']?>&id=<?= $_REQUEST['id']?>&type=<?= $_REQUEST['type']?>')
        $('#<?= $_REQUEST['type']?>A<?= $_REQUEST['id']?>').text(s)
    })
}
</script>