<?php

require_once('../common.php');
require_once('../config.php'); // Include configuration for PDO connection
require_once('checklogin.php');

include_once('header.php');
include_once('editors-menu.php');

// Initialize variables
$showipaddress = "no"; // Default value if not configured

// Define submission statuses as a static array
$submission_status = [
    0 => 'Pending',
    1 => 'Approved',
    2 => 'Rejected',
    3 => 'In Review',
    4 => 'Published',
    // Add more statuses as needed
];

// Retrieve the showipaddress setting
$stmt = $pdo->prepare("SELECT value FROM sm_settings WHERE id = :id"); // Assuming 'id' is the key column
$stmt->execute(['id' => 1]); // Replace '1' with the correct ID for the showipaddress setting
$setting = $stmt->fetch(PDO::FETCH_ASSOC);
if ($setting) {
    $showipaddress = htmlspecialchars($setting['value'], ENT_QUOTES, 'UTF-8');
}

?>

<style>
    .search-container {
        width: 60%;
        margin: 0 auto;
        padding: 20px;
        border: 1px solid #ccc;
        border-radius: 10px;
        background-color: #f9f9f9;
    }
    .search-container h2 {
        text-align: center;
    }
    .search-container form {
        display: flex;
        flex-direction: column;
        gap: 15px;
    }
    .search-container form input[type="text"], .search-container form input[type="date"], .search-container form input[type="submit"] {
        padding: 10px;
        font-size: 16px;
        border: 1px solid #ccc;
        border-radius: 5px;
    }
    .search-container form label {
        font-weight: bold;
    }
    .results-container {
        width: 80%;
        margin: 20px auto;
        text-align: center;
    }
    table {
        width: 100%;
        border-collapse: collapse;
    }
    table th, table td {
        padding: 10px;
        border: 1px solid #ccc;
    }
    table th {
        background-color: #007bff;
        color: white;
    }
</style>

<div class="search-container">
    <h2>Search Submissions</h2>
    <form method="post" action="editors-search.php">
        <label for="from">Date Submitted From:</label>
        <input type="date" id="from" name="from" value="<?php if (isset($_POST['from'])) { echo htmlspecialchars($_POST['from'], ENT_QUOTES, 'UTF-8'); } ?>">

        <label for="to">Date Submitted To:</label>
        <input type="date" id="to" name="to" value="<?php if (isset($_POST['to'])) { echo htmlspecialchars($_POST['to'], ENT_QUOTES, 'UTF-8'); } ?>">

        <label for="author">Contributor Name:</label>
        <input id="author" name="author" value="<?php if (isset($_POST['author'])) { echo htmlspecialchars($_POST['author'], ENT_QUOTES, 'UTF-8'); } ?>" type="text" placeholder="Enter contributor name">

        <label for="title">Submission Title:</label>
        <input id="title" name="title" value="<?php if (isset($_POST['title'])) { echo htmlspecialchars($_POST['title'], ENT_QUOTES, 'UTF-8'); } ?>" type="text" placeholder="Enter submission title">

        <input name="submit" type="submit" value="Search">
    </form>
</div>

<?php if (isset($_POST['submit'])): ?>
<div class="results-container">
    <h2>Search Results</h2>
    <table>
        <tr>
            <th>Date Submitted</th>
            <th>Title</th>
            <th>Author</th>
            <th>Email</th>
            <th>Edition</th>
            <th>Status</th>
            <?php if ($showipaddress == "yes"): ?><th>IP Address</th><?php endif ?>
        </tr>
    <?php
        // Build query conditions safely
        $where = [];
        $params = [];

        if (!empty($_POST['from'])) {
            $where[] = 'a.date_submitted >= :from_date';
            $params['from_date'] = $_POST['from'];
        }

        if (!empty($_POST['to'])) {
            $where[] = 'a.date_submitted <= :to_date';
            $params['to_date'] = $_POST['to'];
        }

        if (!empty(trim($_POST['author']))) {
            $where[] = "CONCAT(`author_name`, ' ', `author_lastname`) LIKE :author";
            $params['author'] = '%' . $_POST['author'] . '%';
        }

        if (!empty(trim($_POST['title']))) {
            $where[] = 'title LIKE :title';
            $params['title'] = '%' . $_POST['title'] . '%';
        }

        $query = "SELECT *, a.id AS sid, b.id AS poetid
                  FROM sm_submissions AS a
                  INNER JOIN sm_poets AS b ON a.poetid = b.id
                  WHERE " . (count($where) ? implode(' AND ', $where) : '1') . "
                  ORDER BY a.date_submitted DESC";

        // Prepare statement
        $stmt = $pdo->prepare($query);
        $stmt->execute($params);
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

        foreach ($results as $row) {
            $date_submitted = htmlspecialchars($row['date_submitted'] ?? '', ENT_QUOTES, 'UTF-8');
            $sid = (int) ($row['sid'] ?? 0);
            $title = htmlspecialchars($row['title'] ?? '', ENT_QUOTES, 'UTF-8');
            $author_name = htmlspecialchars($row['author_name'] ?? '', ENT_QUOTES, 'UTF-8');
            $author_lastname = htmlspecialchars($row['author_lastname'] ?? '', ENT_QUOTES, 'UTF-8');
            $email = htmlspecialchars($row['email'] ?? '', ENT_QUOTES, 'UTF-8');
            $edition_name = htmlspecialchars(getEditionName($row['editionid'] ?? 0), ENT_QUOTES, 'UTF-8');
            $submission_status_value = htmlspecialchars($submission_status[$row['status']] ?? 'Unknown (ID: ' . htmlspecialchars($row['status'], ENT_QUOTES, 'UTF-8') . ')', ENT_QUOTES, 'UTF-8');
    ?>
        <tr>
            <td><?= $date_submitted ?></td>
            <td><a href="view-submission.php?id=<?= $sid ?>"><?= $title ?></a></td>
            <td><a href="user_history.php?poetid=<?= $row['poetid'] ?? 0 ?>"><?= $author_name ?> <?= $author_lastname ?></a></td>
            <td><a href="mailto:<?= $email ?>"><?= $email ?></a></td>        
            <td><?= $edition_name ?></td>
            <td><?= $submission_status_value ?></td>
            <?php if ($showipaddress == "yes"): ?><td><?= htmlspecialchars($row['ip_address'] ?? '', ENT_QUOTES, 'UTF-8') ?></td><?php endif ?>
        </tr>    
    <?php
        }
    ?>
    </table>
</div>
<?php endif; ?>

<?php include_once('footer.php'); ?>
