<?php

function admin()
{

  global $tblUser;
  global $isStaff;

  if($isStaff==0)
  {
    echo "Sorry - your user account does not have access to this area.";
    return 0;
  }

  $subop="!"; if(array_key_exists('subop',$_REQUEST)) $subop=$_REQUEST['subop'];

  switch($subop)
  {
    case "!":                  adminMenu();
                               break;

    case 'listLocations':      adminListLocations();
                               break;

    case 'listOnlineCourses':  adminListOnlineCourses();
                               break;

    case 'viewLocation':       adminViewLocation();
                               break;

    default:                   echo $subop;
                               break;
  }
}

function adminListOnlineCourses()
{
  echo "Hello";
  echo '<div class="video-container"><iframe width="853" height="480" src="https://www.youtube.com/embed/nZP1FO6VWyI" frameborder="0" allowfullscreen></iframe></div>';
  echo '<div class="video-container"><iframe width="853" height="480" src="https://www.youtube.com/embed/ef_KxnumaI8" frameborder="0" allowfullscreen></iframe></div>';
  echo '<div class="video-container"><iframe width="853" height="480" src="https://www.youtube.com/embed/Or6M5P62Gpg" frameborder="0" allowfullscreen></iframe></div>';
}

function adminMenu()
{
  echo "<p><a class=\"button\" href=\"admin.php?subop=listLocations\">Locations</a></p>";
  echo "<p><a class=\"button\" href=\"admin.php?subop=listClasses\">Classes</a></p>";
  echo "<p><a class=\"button\" href=\"admin.php?subop=listTerms\">Terms</a></p>";
  echo "<p><a class=\"button\" href=\"admin.php?subop=listOnlineCourses\">Online Courses</a></p>";
}

function adminListLocations()
{
  global $cxn;
  global $m;

  $sql="select id, post_title title, post_name location, post_content description from wp_posts where post_type='location' order by post_date desc ";

  $locations=loadTable($cxn,$sql);

  echo "<table class=\"with-borders\">";
  echo "<tr><th>Location</th>";
  echo "<th>Title</th>";
  echo "<th>Details</th>";
  echo "<th>Operation</th></tr>";

  foreach($locations as $lkp) 
  {

    $sql="select * from wp_postmeta where post_id=".$lkp['id'];
    $meta=loadTable($cxn,$sql);
    foreach($meta as $m) $lkp[$m['meta_key']]=$m['meta_value'];

    echo "<tr><td>";
    if(array_key_exists('location',$lkp)) echo $lkp['location'];
    echo "</td><td>";
    if(array_key_exists('title',$lkp)) echo $lkp['title'];
    echo "</td><td>";
    if(array_key_exists('wpcf-location-timing',$lkp)) echo "<strong>".$lkp['wpcf-location-timing']."</strong><br />";
    if(array_key_exists('wpcf-location-address',$lkp)) echo $lkp['wpcf-location-address'];
    echo "</td><td>";
    if(array_key_exists('id',$lkp)) echo "<a href=\"admin.php?subop=viewLocation&id=".$lkp['id']."\" class=\"button\">View</a>";
    echo "</td></tr>";
  }

  echo "</table>";

}

function adminViewLocation()
{
  global $cxn;
  global $m;

  if(array_key_exists('id',$_REQUEST))
  {
    $sql="select id, post_title title, post_name location, post_content description from wp_posts where post_type='location' and id=".dbEscape($cxn,$_REQUEST['id']);

    $locations=loadTable($cxn,$sql);

    foreach($locations as $lkp) 
    {
      $sql="select * from wp_postmeta where post_id=".$lkp['id'];
      $meta=loadTable($cxn,$sql);
      foreach($meta as $m) $lkp[$m['meta_key']]=$m['meta_value'];

      echo "<table class=\"with-borders\">";
      echo "<tr><th>Location</th><td>";
      if(array_key_exists('location',$lkp)) echo $lkp['location'];
      echo "</td></tr>"; 
      echo "<th>Title</th><td>";
      if(array_key_exists('title',$lkp)) echo $lkp['title'];
      echo "</td></tr>"; 
      echo "<th>Timing</th><td>";
      if(array_key_exists('wpcf-location-timing',$lkp)) echo $lkp['wpcf-location-timing'];
      echo "</td></tr>"; 
      echo "<th>Address</th><td>";
      if(array_key_exists('wpcf-location-address',$lkp)) echo $lkp['wpcf-location-address'];
      echo "</td></tr>"; 
      echo "<th>Map</th><td>";

      print_r($lkp['wpcf-add-real-google-map']); 
      echo "</td></tr>"; 
      echo "<th>Description</th><td>";
      $description=substr($lkp['description'],0,strpos($lkp['description'],"<a href"));
      $description=str_replace("\n","<br />",$description);
      print_r($description); 
      echo "</td></tr>"; 
      echo "</table>";

    }
  }
  else
  {
    echo "Location Not Found";
  }
}

function adminAddLocation()
{
  if(array_key_exists('address',$_REQUEST));
  echo "<p><a class=\"button\" href=\"admin.php?subop=addLocation\">Add Location</a></p>";
}

function displayUsers()
{

  global $tblUser;
  global $isStaff;

  if($isStaff==0)
  {
    echo "Sorry - your user account does not have access to this area.";
    return 0;
  }

  echo "<h1 style=\"padding-top: 30px; padding-bottom: 10px;\">User List</h1>";

  echo "<table class=\"with-borders\"><tr><th style=\"text-align: center;\">Picture</th><th>Name</th><th>Email</th><th>Roles</th><th>Operation</th></tr>";

  $sort=['family_name'=>1,'given_name'=>1,'email'=>1];
  $tbl=$tblUser->find([],['sort'=>$sort]);

  foreach($tbl as $rec)
  {
    echo "<tr>";
    echo "<td style=\"text-align: center;\"><img src=\"".$rec['picture']."\" style=\"width: 40px; height: 40px;\"></td>";
    echo "<td style=\"padding: 10px;\">".$rec['name']."</td>";
    echo "<td style=\"padding: 10px;\">".$rec['email']."</td>";
    echo "<td style=\"padding: 10px;\">";
    if(array_key_exists("role",$rec))
    {
      foreach($rec['role'] as $role=>$ck)
      {
        if($ck==1) echo $role."<br />";
      }
    }
    echo "</td>";
    echo "<td style=\"padding: 10px;\">";
    echo "<a href=\"user.php?id=".$rec['id']."\">Edit User</a>";
    echo "</td>";
    echo "</tr>";
  }
  echo "</table>";
}

function processLoginSessions() # No longer used
{

  global $tblSess;
  global $tblUser;

  $tbl=$tblSess->find(['user'=>['$exists'=>1]]);

  foreach($tbl as $rec)
  {
    $cnt=-1;
    if(array_key_exists('user',$rec))
    {
      if(array_key_exists('id',$rec['user']))
      {
        $id=$rec['user']['id'];
        $cnt=$tblUser->count(['id'=>$id]);
        if($cnt==0)
        {
          $tblUser->insertOne($rec['user']);
        }
      } 
    } 
  }
}

function displayLoginSessions()
{
  global $tblSess;
  global $tblUser;
  global $isStaff;

  if($isStaff==0)
  {
    echo "Sorry - your user account does not have access to this area.";
    return 0;
  }

  echo "<h1 style=\"padding-top: 30px; padding-bottom: 10px;\">User List</h1>";
  echo "<table class=\"with-borders\"><tr><th style=\"text-align: center;\">Picture</th><th>id</th><th>Name</th><th>Email</th><th>Roles</th><th>Operation</th></tr>";

  $sort=['family_name'=>1,'given_name'=>1,'email'=>1];
  $tbl=$tblSess->find([],['sort'=>$sort]);

  foreach($tbl as $rec)
  {
    echo "<tr>";
    echo "<td style=\"text-align: center;\"><img src=\"".$rec['picture']."\" style=\"width: 40px; height: 40px;\"></td>";
    echo "<td style=\"padding: 10px;\">".$rec['id']."</td>";
    echo "<td style=\"padding: 10px;\">".$rec['name']."</td>";
    echo "<td style=\"padding: 10px;\">".$rec['email']."</td>";
    echo "<td style=\"padding: 10px;\">";
    if(array_key_exists("role",$rec))
    {
      foreach($rec['role'] as $role)
      {
        echo $role."<br />";
      }
    }
    echo "</td>";
    echo "<td style=\"padding: 10px;\">";
    echo "<a href=\"user.php?id=".$rec['id']."\">Edit User</a>";
    echo "</td>";
    echo "</tr>";
  }
  echo "</table>";
}

function editUser()
{
  global $tblUser;
  global $isStaff;

  if($isStaff==0)
  {
    echo "Sorry - your user account does not have access to this area.";
    return 0;
  }


  $id=-1;

  if(array_key_exists('id',$_REQUEST)) $id=$_REQUEST['id'];

  $filter=['id'=>$id];

  echo "<h1 style=\"padding-top: 30px; padding-bottom: 10px;\">Edit User</h1>";

  $cnt=$tblUser->count($filter);

  if($cnt>0)
  {
    if(array_key_exists("operation",$_REQUEST))
    {
      if($_REQUEST['operation']=="staff")
      {
        $op=['$set'=>["role.staff"=>1]];
        $ck=$tblUser->updateOne($filter,$op);
      }

      if($_REQUEST['operation']=="removestaff")
      {
        $op=['$unset'=>["role.staff"=>0]];
        $ck=$tblUser->updateOne($filter,$op);
      }
    }

    $rec=$tblUser->findOne($filter);
    echo "<table class=\"with-borders\">";
    echo "<tr><th>id:</th><td>".$rec['id']."</td></tr>";
    echo "<tr><th>Verified Email:</th><td>".$rec['verified_email']."</td></tr>";
    echo "<tr><th>Picture:</th><td><img src=\"".$rec['picture']."\" style=\"width: 40px; height: 40px;\"></td></tr>";
    echo "<tr><th>Given Name:</th><td>".$rec['given_name']."</td></tr>";
    echo "<tr><th>Family Name:</th><td>".$rec['family_name']."</td></tr>";
    echo "<tr><th>Name:</th><td>".$rec['name']."</td></tr>";
    echo "<tr><th>Roles:</th><td>";

    $staff=0;

    if(array_key_exists("role",$rec))
    {
      foreach($rec['role'] as $role=>$val)
      {
        echo $role."<br />";
      }
      if (array_key_exists('staff',$rec['role']))
      {
        $staff=$rec['role']['staff'];
      }
    }
    echo "</td></tr>";

    echo "<tr><th>Operations:</th><td>";
    if($staff==0) echo "<a class=\"button\" href=\"user.php?id=$id&operation=staff\">Add Staff Role</a> <br />";
    if($staff==1) echo "<a class=\"button\" href=\"user.php?id=$id&operation=removestaff\">Remove Staff Role</a> <br />";
        
    echo "</td></tr>";
    echo "<tr><th>Email:</th><td>".$rec['email']."</td></tr>";
    echo "</table>";

  }
  else
  {
    echo "Could not find user";
  }
}

?>
