<?php
include_once '../includes/constants.php';
include_once '../includes/functions.php';

// Connect to the database
$partners = new partners;
$partners->connection($host, $user, $pass, $db);
$conn = $partners->conn;

// Get and sanitize input
$name    = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_STRING);
$msg     = filter_input(INPUT_GET, 'msg', FILTER_SANITIZE_STRING);
$status  = filter_input(INPUT_GET, 'status', FILTER_SANITIZE_STRING);

$active = $inactive = "";
if ($status === "active") {
    $active = "checked";
} else {
    $inactive = "checked";
}

// Fetch event names for dropdown
$events = [];
$result = $conn->query("SELECT event_name FROM partners_event");
if ($result) {
    while ($row = $result->fetch_assoc()) {
        $events[] = $row['event_name'];
    }
}
?>

<form method="POST" action="eventvalidate.php" id="f1">
  <p class="textred"><?php echo htmlspecialchars($msg); ?></p>
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="373" class="tablebdr">
    <tr>
      <th colspan="4" height="20" class="tdhead" align="right">Add Event and Configure E-mail for It</th>
    </tr>
    <tr>
      <td width="12" height="30">&nbsp;</td>
      <td width="125">Event Name</td>
      <td width="257"><input type="text" name="nametxt" size="23" value="<?php echo htmlspecialchars($name); ?>"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>Status</td>
      <td>
        Active <input type="radio" value="active" <?php echo $active; ?> name="status">
        Inactive <input type="radio" name="status" value="inactive" <?php echo $inactive; ?>>
      </td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td colspan="4">
        <input type="submit" value="Add Event" name="B1" style="float: left">
      </td>
    </tr>
    <tr>
      <th colspan="4" class="tdhead"><p align="center">&nbsp;</p></th>
    </tr>
  </table>
  <p>&nbsp;</p>
</form>

<form name="FormName" action="event_remove.php" method="post">
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="373" class="tablebdr">
    <tr>
      <th colspan="4" height="20" class="tdhead">Remove An Event</th>
    </tr>
    <tr>
      <td width="12" height="30">&nbsp;</td>
      <td width="125">Event Name</td>
      <td width="257">
        <select name="eventcompo" id="select1">
          <option selected>ChooseEvent</option>
          <?php foreach ($events as $event_name): ?>
            <option><?php echo htmlspecialchars($event_name); ?></option>
          <?php endforeach; ?>
        </select>
      </td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td colspan="4" align="center">
        <input type="submit" value="REMOVE">
      </td>
    </tr>
    <tr>
      <th colspan="4" class="tdhead"><p align="center">&nbsp;</p></th>
    </tr>
  </table>
</form>
