<?
  $dbhost = '192.168.1.10';
  $dbuser = 'slowtwitch';
  $dbpass = 'k9volqlAcpq';
  $dbname = 'training';

  $conn = mysql_connect($dbhost, $dbuser, $dbpass)
    or die ('Error connecting to mysql server: ' . mysql_error());
  mysql_select_db($dbname)
    or die ('Error selecting ' . $dbname . ': ' . mysql_error());

  if (isset($_GET["action"]) && isset($_GET["id"])) {
    $action = $_GET["action"];
    $id = $_GET["id"];
    if ($action == "delete" && $id != "") {
      $queryCopy = "insert into ugomo_workout_challenge_history (challenge_id, created_by, challenge_type, challenge_name, start_date, end_date, challenge_value, unit, meassure, sport, notes, min_time, min_distance, min_distance_unit) SELECT challenge_id, created_by, challenge_type, challenge_name, start_date, end_date, challenge_value, unit, meassure, sport, notes, min_time, min_distance, min_distance_unit FROM ugomo_workout_challenge WHERE ugomo_workout_challenge.challenge_id = " . $id;
      $resultCopy = mysql_query($queryCopy);
      $queryDelete = "delete from ugomo_workout_challenge where challenge_id = " . $id;
      $resultDelete = mysql_query($queryDelete);
    }
    header("Location: /tladmin");
  }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ST - Training Log Admin</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<style>
  body { font-face: Verdana; font-size: 14px; }
  h1 { font-size: 18px; }
  td { padding: 0px 0px 0px 10px; }
  table.tabular td { text-align: right; }
  tr.headerRow td { font-weight: bold; }
</style>
</head>

<body>

<?
// Users with at least one logged workout
$query = "select user_id, count(*) from ugomo_workout group by user_id order by user_id";
$result = mysql_query($query);
$usersWithOneWorkout = mysql_num_rows($result);

// Total workouts logged
$query = "select count(*) AS total from ugomo_workout";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$totalWorkoutsLogged = $row["total"];

// Total workouts logged in the last 7 days
$yesterday = date("Y-m-d", strtotime("now") - 86400);
$oneWeekAgo = date("Y-m-d", strtotime("now") - (86400 * 7));
$query = "select count(*) AS total from ugomo_workout where workout_date >= '" . $oneWeekAgo . "' AND workout_date <= '" . $yesterday . "'";
// echo $query . '<br />';
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$workoutsLoggedLastWeek = $row["total"];

?>

<h1>Challenge Stats</h1>

<table class="tabular">
  <tr><td>Users with at least one workout logged:</td><td><?=number_format($usersWithOneWorkout)?></td></tr>
  <tr><td>Total workouts logged:</td><td><?=number_format($totalWorkoutsLogged)?></td></tr>
  <tr><td>Workouts logged in previous 7 days:</td><td><?=number_format($workoutsLoggedLastWeek)?></td></tr>
</table>

<h1>Challenge Admin</h1>

<table>
  <tr class="headerRow"><td>Challenge Name</td><td>Starts</td><td>Ends</td><td>Created By</td><td>&nbsp;</td></tr>
  <?
    $query = "select challenge_id, created_by, challenge_name, start_date, end_date from ugomo_workout_challenge";
    $result = mysql_query($query);
    while($row = mysql_fetch_assoc($result)) {
      echo '<tr>';
      echo '<td>' . $row["challenge_name"] . '</td>';
      echo '<td>' . $row["start_date"] . '</td>';
      echo '<td>' . $row["end_date"] . '</td>';
      echo '<td>' . $row["created_by"] . '</td>';
      echo '<td><a href="?action=delete&id=' . $row["challenge_id"] . '" onclick="return confirm(\'Are you sure you want to delete ' . $row["challenge_name"] . '\')">Delete</a></td>';
      echo '</tr>' . "\n";
    }
  ?>
</table>
