<?php
function admin_article_view()
{
  $display = '
    <div class="view_card">
      <div class="row_space_between">
        <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_space_between">
          <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 class="">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>
  ';
  $conn = get_connection();
  $results = $conn->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;');

  foreach ($results as $key => $result) {
    $display .= '
            <tr data-href="articles.php?view=read&article_id=' . $result['article_id'] . '">
              <td class="excluded_cell">
                <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;
}
