<?php
  require("php/common_begin.php");

  $lst = Listopia::getInstance();
  // -------------------------------
  if (!$lst->isLoggedIn())
  {
    $lst->unloggedInPage();
    return;
  }
  // -------------------------------


  $lst->pageBegin("Listopia Administration");

  // make sure the user has admin powers, otherwise they can feck off
  if (!$lst->isAdminUser())
  {
    die("<div style='line-height:2em; font-size:9em; text-align:center;'>NO.</div>");
  }
  
  // accessing the admin panel optimises the tables. and why not.
  $lst->Database()->optimizeHotTables();
  
  $lst->contentsBeginSinglePage(
    "Admin Overview",
    "And now we're balls-deep in dataguts");
?>

    <div id="accordion">
    
    <div id="UINewUserDialog" title="&nbsp;" style="display:none">
      <form id="NewUser" action="">
        <label for="AuthUser">Username</label><input spellcheck="false" autocapitalize="off" autocorrect="off" autocomplete="off" type="text" id="AuthUser" />
        <label for="AuthPwd">Password</label><input spellcheck="false" autocapitalize="off" autocorrect="off" autocomplete="off" type="text" id="AuthPwd" />
        <label for="AuthMail">Email</label><input spellcheck="false" autocapitalize="off" autocorrect="off" autocomplete="off" type="text" id="AuthMail" />
        <div id="Reason" style="font-size:0.8em; color:red;"></div>
      </form>
    </div>

      <div >
        <button id="UINewUser" style="width:250px">NEW USER</button>
      </div>

      <!-- -------------------------------------------------------------------- -->
    
      <h3><a href="#">User Data</a></h3>
      <div>
      <table id="box-table-a">

        <!-- table header -->
        <thead>
          <tr>
            <th scope="col">ID</th>
            <th scope="col">user</th>
            <th scope="col">status</th>
            <th scope="col">valid</th>
            <th scope="col">created</th>
          </tr>
        </thead>
        
        <!-- table body -->
        <tbody>
<?php
    $query = $lst->Database()->select(array(
                          'table' => 'users',
                          'fields' => '*',
                          'limit' => 0
                          ));
    
    foreach ($query as &$value)
    {
      $from_time = strtotime($value['users']['user_last_login']);
      $sinceLastLogin = round(abs(time() - $from_time) / 60 / 60 / 24, 1);
      
      $profileLink = ProfileLink($value['users']['user_id'], '#PageContent');
      
      echo("<tr>");
      echo("  <td>".$value['users']['user_id']."</td>");
      echo("  <td><a ".$profileLink.">".$value['users']['user_name']."</a></td>");
      echo("  <td>".$value['users']['user_status']."</td>");
      
      if ($value['users']['user_validation_code'] != 'validated')
      {
        echo "<td><a>RESEND</a></td>";
      }
      else
      {
        echo("  <td>&#10004;</td>");
      }

      echo("  <td>".$value['users']['user_created']."</td>");
      

      echo("</tr>");
    }    
?>
        </tbody>
      </table>
      </div> <!-- user data div -->
      
      <!-- -------------------------------------------------------------------- -->
      
      <h3><a href="#">List Data</a></h3>
      <div>
      <table id="box-table-a">

        <!-- table header -->
        <thead>
          <tr>
            <th scope="col">ID</th>
            <th scope="col">headline</th>
            <th scope="col">open to</th>
            <th scope="col">editable by</th>
          </tr>
        </thead>
        
        <!-- table body -->
        <tbody>
<?php
    $query = $lst->Database()->select(array(
                          'table' => 't_lists',
                          'fields' => 'list_id, list_headline, list_createdby, list_submission, list_featured',
                          'limit' => 0
                          ));
    
    foreach ($query as &$value)
    {
      echo("<tr>");
      echo("  <td>".$value['t_lists']['list_id']."</td>");
      echo("  <td><a " . ListLink($value['t_lists']['list_id']) . ">".stripslashes($value['t_lists']['list_headline'])."</a></td>");     
      echo("  <td>".$value['t_lists']['list_submission']."</td>");
      echo("  <td>".$value['t_lists']['list_featured']."</td>");
      echo("</tr>");
    }    
?>        
        </tbody>
      </table>
      </div> <!-- list data div -->
      
      <!-- -------------------------------------------------------------------- -->
      
    </div> <!-- accordion -->


<?php
  $lst->contentsEnd();

  // default script loading
  $lst->pageEndScripts();
?>


<script type="text/javascript">
  $(function()
  {
    $('#UINewUser').click(function(){
      $('#UINewUserDialog').dialog('open');
      return false;
    });

    $('#UINewUserDialog').dialog({
      autoOpen: false,
      modal: true,
      resizable: false,
      width: 250,
      buttons: {
        "Submit": function(event) 
        {
          var authDialog = $(this);
          var authReason = $("#NewUser #Reason");
          
          $(event.target).attr("disabled", true).addClass("ui-state-disabled");
          authReason.html("<img style='margin:0px auto' src='images/ajax-loader.gif' alt=''/>");
          
          $.post(
            "php/ajax_authCreateNewUser.php", 
            { user: $("#NewUser #AuthUser").val(), 
              pass: $("#NewUser #AuthPwd").val(), 
              email: $("#NewUser #AuthMail").val(), 
              foo:"bar" },
            function(data) 
            {
                location.reload();
                authReason.html(data["reason"]);
                $(event.target).attr("disabled", false).removeClass("ui-state-disabled");
            },
            "json");
        }
      }
    });

  });
</script>





<?php
  $lst->pageEnd();

  require("php/common_end.php");
?>
