<?php
require_once 'db_functions.php';
require_once 'account_functions.php';
require_once 'article_functions.php';

function admin_article_view()
{
  $display = '
    <div class="view_card">
      <div class="row">
        <h3>Articles</h3>
        <a href="admin.php?view=users">View Users</a>
      </div>
      <div class="line"></div>
      <form method="post">
        <input type="hidden" name="form_id" value="articles">
        <div class="row">
          <label for="delete" class="form_checkbox_container">Delete
            <input type="radio" class="button" name="action" id="delete" value="delete">
            <span class="checkmark"></span>
          </label>
          <input type="submit" class="button narrow" value="Commit">
        </div>

        <table>
          <thead>
            <tr>
              <th>Select</th>
              <th>Article ID</th>
              <th>Author</th>
              <th>Title</th>
              <th>Created</th>
              <th>Updated</th>
              <th>Published</th>
              <th>Read Count</th>
            </tr>
          </thead>
          <tbody>
  ';
  $query = 'SELECT article_id, username, title, read_count, article.created_at, updated_at, published_at, excerpt FROM article INNER JOIN user ON article.author_id = user.user_id;';

  $results = query_one_or_more_results($query);

  foreach ($results as $key => $result) {
    $display .= '
            <tr>
              <td>
                <label class="form_checkbox_container">
                  <input name="selected_articles[]" value="' . $result['article_id'] . '" type="checkbox">
                  <span class="checkmark"></span>
                </label>
              </td>
              <td>' . $result['article_id'] . '</td>
              <td>' . $result['username'] . '</td>
              <td>' . $result['title'] . '</td>
              <td>' . $result['created_at'] . '</td>
              <td>' . $result['updated_at'] . '</td>
              <td>' . $result['published_at'] . '</td>
              <td>' . $result['read_count'] . '</td>
            </tr>
    ';
  }

  $display .= '
          </tbody>
        </table>

      </form>
    </div>
  ';

  return $display;
}
function admin_user_view()
{
  $display = '
    <div class="view_card">
      <div class="row">
        <h3>Users</h3>
        <a href="admin.php?view=articles">View Articles</a>
      </div>
      <div class="line"></div>
      <form method="post">
        <input type="hidden" name="form_id" value="users">
        <div class="row">
          <label for="delete" class="form_checkbox_container">Delete
            <input type="radio" class="button" name="action" id="delete" value="delete">
            <span class="checkmark"></span>
          </label>
          <input type="submit" class="button narrow" value="Commit">
        </div>

        <table>
          <tr>
            <th>Select</th>
            <th>User ID</th>
            <th>Username</th>
            <th>Email</th>
            <th>Role</th>
            <th>Created</th>
            <th>Last Active</th>
            <th>Active</th>
            <th>Profile Picture</th>
          </tr>
  ';
  $query = 'SELECT user_id, username, email, last_active, role, created_at, is_active, profile_picture FROM user INNER JOIN roles ON user.role_id = roles.role_id;';

  $results = query_one_or_more_results($query);

  foreach ($results as $key => $result) {
    $display .= '
          <tr>
            <td>
              <label class="form_checkbox_container">
                <input name="selected_users[]" value="' . $result['user_id'] . '" type="checkbox">
                <span class="checkmark"></span>
              </label>
            </td>
            <td>' . $result['user_id'] . '</td>
            <td>' . $result['username'] . '</td>
            <td>' . $result['email'] . '</td>
            <td>' . ucwords($result['role']) . '</td>
            <td>' . $result['created_at'] . '</td>
            <td>' . $result['last_active'] . '</td>
            <td>' . $result['is_active'] . '</td>
            <td><img src="' . $_ENV['PROFILE_IMAGES_PQ_PATH'] . $result['profile_picture'] . '" class="card_profile_picture"></td>
          </tr>
    ';
  }

  $display .= '
        </table>

      </form>
    </div>
  ';

  return $display;
}

function delete_users($ids)
{
  foreach ($ids as $id) {
    delete_account($id);
  }
}
