<?php
include("../core/connect.php");

if(isset($_GET["actionx"])){

$actionx=$_GET["actionx"];

if($actionx=='update'){
  $roleno=$_GET["roleno"];
  $pwdexp=$_GET["pwdexp"];

$update_keyfields=$dbh->query("update keyfields set pswdexpiry='$pwdexp' where rolenumber='$roleno'");

if($update_keyfields){echo "<div class='alert alert-success'>SUCCESS: PASSWORD EXPIRY DATE HAS BEEN UPDATED. </div>";} 
}

elseif($actionx=='update_menu'){
  $ai=$_GET["ai"];
  $selectedroles=$_GET["selectedroles"];
  $update_menu=$dbh->query("update menu set role='$selectedroles' where autoid='$ai'");

if($update_menu){echo "<div class='alert alert-success'>SUCCESS: MENU HAS BEEN UPDATED. </div>";} }

}


// pseudocode
//$variable = $_POST['variable'];
$page = $_GET['page'];



  echo "<table id='sorttable1'>
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Username</th>
<th>Password</th>
<th>Contact</th>
</tr></thead>
<tbody>";


if(!empty($_GET["search"])){
$search=$_GET["search"];
$es=explode("*",$search);

if(isset($es[0])&&isset($es[1])){
$v=$es[0];
$v2=$es[1];
$daquery="and (firstname like '%$v%' and lastname like '%$v2%') or (firstname like '%$v2%' and lastname like '%$v%')" ;}
else{$daquery="and (firstname like '%$search%' or lastname like '%$search%')";}
}else{$daquery='';}


// Get the total number of records from our table "payments".
$pets = $dbh->query("select * from users where role!='tech'  $daquery ");
$total_pages=$pets->rowCount();
// Check if the page number is specified and check if it's a number, if not return the default page number which is 1.
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;

// Number of results to show on each page.
$num_results_on_page = 10;

$calc_page = ($page - 1) * $num_results_on_page;
if(isset($_POST["submit_search"])){$dalimit='';}else{$dalimit="limit $calc_page, $num_results_on_page";}

$result_users=$dbh->query("select * from users where role!='tech'  $daquery $dalimit");
$row_users=$result_users->fetchObject();
$count_users=$result_users->rowCount();

if($count_users>0){ $z=1; do{


echo "
<tr>
<td>".$z++."</td>
<td>".$row_users->firstname." ".$row_users->lastname."</td>
<td>".$row_users->username."</td>
<td>".$row_users->password."</td>
<td>";

if(!empty($row_users->phone1)){echo $row_users->phone1; ?>&nbsp;<a onClick="fun_send('<?php echo $row_users->phone1; ?>','<?php echo $row_users->autoid; ?>','<?php echo $row_users->username; ?>','<?php echo $row_users->password; ?>','<?php echo $row_users->firstname; ?>','sms')" style='color:blue;cursor:pointer;' title='Send Reminder' class='fa fa-paper-plane'></a>&nbsp;&nbsp; <?php }

if(!empty($row_users->phone2)){echo ",".$row_users->phone2; ?>&nbsp;<a onClick="fun_send('<?php echo $row_users->phone2; ?>','<?php echo $row_users->autoid; ?>','<?php echo $row_users->username; ?>','<?php echo $row_users->password; ?>','<?php echo $row_users->firstname; ?>','sms')" style='color:blue;cursor:pointer;' title='Send Reminder' class='fa fa-paper-plane'></a>&nbsp;&nbsp; <?php }

echo "<br>";

if(!empty($row_users->email)){echo $row_users->email; ?> &nbsp;<a onClick="fun_send('<?php echo $row_users->email; ?>','<?php echo $row_users->autoid; ?>','<?php echo $row_users->username; ?>','<?php echo $row_users->password; ?>','<?php echo $row_users->firstname; ?>','email')" style='color:red;cursor:pointer;' title='Send Reminder' class='fa fa-paper-plane'></a>&nbsp;&nbsp; <?php } echo "<div id='alerte".$row_users->autoid."'></div></td>

</tr>"; 
}while($row_users=$result_users->fetchObject());}


?>
</tbody>
</table>


<!--pagination-->
<?php if (ceil($total_pages / $num_results_on_page) > 0): $page_name='system_users.php'; ?>
<ul class="pagination">
  <?php if ($page > 1): ?>
  <li class="prev"><a href=<?php echo $page_name; ?>?page=<?php echo $page-1 ?>>Prev</a></li>
  <?php endif; ?>

  <?php if ($page > 3): ?>
  <li class="start"><a href=<?php echo $page_name; ?>?page=1>1</a></li>
  <li class="dots">...</li>
  <?php endif; ?>

  <?php if ($page-2 > 0): ?><li class="page"><a href=<?php echo $page_name; ?>?page=<?php echo $page-2 ?>><?php echo $page-2 ?></a></li><?php endif; ?>
  <?php if ($page-1 > 0): ?><li class="page"><a href=<?php echo $page_name; ?>?page=<?php echo $page-1 ?>><?php echo $page-1 ?></a></li><?php endif; ?>

  <li class="currentpage"><a href=<?php echo $page_name; ?>?page=<?php echo $page ?>><?php echo $page ?></a></li>

  <?php if ($page+1 < ceil($total_pages / $num_results_on_page)+1): ?><li class="page"><a href=<?php echo $page_name; ?>?page=<?php echo $page+1 ?>><?php echo $page+1 ?></a></li><?php endif; ?>
  <?php if ($page+2 < ceil($total_pages / $num_results_on_page)+1): ?><li class="page"><a href=<?php echo $page_name; ?>?page=<?php echo $page+2 ?> ><?php echo $page+2 ?></a></li><?php endif; ?>

  <?php if ($page < ceil($total_pages / $num_results_on_page)-2): ?>
  <li class="dots">...</li>
  <li class="end"><a href=<?php echo $page_name; ?>?page=<?php echo ceil($total_pages / $num_results_on_page) ?>><?php echo ceil($total_pages / $num_results_on_page) ?></a></li>
  <?php endif; ?>

  <?php if ($page < ceil($total_pages / $num_results_on_page)): ?>
  <li class="next"><a href=<?php echo $page_name; ?>?page=<?php echo $page+1 ?>>Next</a></li>
  <?php endif; ?>
</ul>
<?php endif; ?>
<!--end of pagination-->


<table id='sorttable1'>
<thead>
<tr>
<th>No</th>
<th>Menu Item</th>
<th>Roles</th>
</tr></thead>
<tbody>
<?php

// Get the total number of records from our table "payments".
$pets = $dbh->query("select * from menu");
$total_pages=$pets->rowCount();
// Check if the page number is specified and check if it's a number, if not return the default page number which is 1.
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;

// Number of results to show on each page.
$num_results_on_page = 10;

$calc_page = ($page - 1) * $num_results_on_page;
if(isset($_POST["submit_search"])){$dalimit='';}else{$dalimit="limit $calc_page, $num_results_on_page";}


$result_menu=$dbh->query("select * from menu $dalimit");
$row_menu=$result_menu->fetchObject();
$count_menu=$result_menu->rowCount();

if($count_menu>0){ $z=1;  do{
echo "
<tr>
<td>".$z++."</td>
<td>".$row_menu->item."
<form method='post'>
  <input type='hidden' name='ai' value='".$row_menu->autoid."'>
  <input type='hidden' name='selectedroles".$row_menu->autoid."' id='selectedroles".$row_menu->autoid."' value='".$row_menu->role."'>
  <input type='submit' class='btn btn-sm btn-primary' name='submit_menu' value='Submit'>
  </form>
</td>
<td>

<div class='row'>"; 
$result_scrap=$dbh->query("select * from scrap where type='role'");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap>0){
  do{
    $exps = explode(",",$row_menu->role);
    $search_results = in_array($row_scrap->item2, $exps);
    if($search_results > 0){$checked = "checked";}
    else {$checked = "";}
    echo "<div class='col-lg-2'>
    <div style='border:1px solid grey;padding:5px;margin:2px'>
    <input type='checkbox' id='chkout".$row_scrap->item2.$row_menu->autoid."' onclick='myRole_off(".$row_menu->autoid.")' value='".$row_scrap->item2."' $checked /> ".$row_scrap->item."
    </div></div>";
    
  }while($row_scrap=$result_scrap->fetchObject());
}

echo "</div></td>

<td>
</tr>"; 
}while($row_menu=$result_menu->fetchObject());}


?>
</tbody>
</table>
</form>

<!--pagination-->
<?php if (ceil($total_pages / $num_results_on_page) > 0): $page_name='system_users.php'; ?>
<ul class="pagination">
  <?php if ($page > 1): ?>
  <li class="prev"><a href=<?php echo $page_name; ?>?page=<?php echo $page-1 ?>>Prev</a></li>
  <?php endif; ?>

  <?php if ($page > 3): ?>
  <li class="start"><a href=<?php echo $page_name; ?>?page=1>1</a></li>
  <li class="dots">...</li>
  <?php endif; ?>

  <?php if ($page-2 > 0): ?><li class="page"><a href=<?php echo $page_name; ?>?page=<?php echo $page-2 ?>><?php echo $page-2 ?></a></li><?php endif; ?>
  <?php if ($page-1 > 0): ?><li class="page"><a href=<?php echo $page_name; ?>?page=<?php echo $page-1 ?>><?php echo $page-1 ?></a></li><?php endif; ?>

  <li class="currentpage"><a href=<?php echo $page_name; ?>?page=<?php echo $page ?>><?php echo $page ?></a></li>

  <?php if ($page+1 < ceil($total_pages / $num_results_on_page)+1): ?><li class="page"><a href=<?php echo $page_name; ?>?page=<?php echo $page+1 ?>><?php echo $page+1 ?></a></li><?php endif; ?>
  <?php if ($page+2 < ceil($total_pages / $num_results_on_page)+1): ?><li class="page"><a href=<?php echo $page_name; ?>?page=<?php echo $page+2 ?> ><?php echo $page+2 ?></a></li><?php endif; ?>

  <?php if ($page < ceil($total_pages / $num_results_on_page)-2): ?>
  <li class="dots">...</li>
  <li class="end"><a href=<?php echo $page_name; ?>?page=<?php echo ceil($total_pages / $num_results_on_page) ?>><?php echo ceil($total_pages / $num_results_on_page) ?></a></li>
  <?php endif; ?>

  <?php if ($page < ceil($total_pages / $num_results_on_page)): ?>
  <li class="next"><a href=<?php echo $page_name; ?>?page=<?php echo $page+1 ?>>Next</a></li>
  <?php endif; ?>
</ul>
<?php endif; ?>
<!--end of pagination-->

</div></div></div>

<script>
function myRole_off(ai){
var selectedroles = document.getElementById("selectedroles"+ai);
var values = [];


<?php
$result_scrap=$dbh->query("select * from scrap where type='role'");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap>0){
  do{
?>
if(document.getElementById("chkout<?php echo $row_scrap->item2; ?>"+ai).checked==true) {
 var user = document.getElementById("chkout<?php echo $row_scrap->item2; ?>"+ai).value;
  values.push(user);

}
<?php
}while($row_scrap=$result_scrap->fetchObject());
}
?>

selectedroles.value = values.join(",");

}
</script>