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

function create_users_from_csv($csv)
{
  $csv = read_file($csv);
  while ($record = fgets($csv)) {
    $fields = explode(',', $record);

    $username = $fields[0];
    $email = $fields[1];
    $password = $fields[2];
    $role_id = $fields[3];

    create_user(
      $username,
      $email,
      $password,
      $password,
      $role_id,
    );
  }
}

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 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?action=view&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;
}

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" enctype="multipart/form-data">
        <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>

          <label id="create_users_from_csv_label" for="create_users_from_csv" class="upload_button">Upload a CSV</label>
          <input id="create_users_from_csv" class="file_input" name="csv" type="file" accept=".csv">
          <small class="file_input_feedback">No file selected.</small>

          <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>
  ';
  $conn = get_connection();
  $results = $conn->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;');

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