<?php
include_once "global.php";

$eventId = $_GET["id"];

$sql = "SELECT * FROM `events` WHERE `id` = ?";
$eventData = $dbF->getRow($sql, [$eventId]);
?>
<div class="form-body">
    <form id="eventEditForm" class="row g-3" method="post">
        <input type="hidden" name="eventId" value="<?php echo $eventId; ?>">
        <div class="col-md-6">
            <label for="eventTitle" class="form-label">Title <span>*</span></label>
            <input type="text" name="eventTitle" id="eventTitle" class="form-control"
                value="<?php echo $eventData["title"]; ?>" required>
        </div>
        <div class="col-md-6">
            <label for="eventCategory" class="form-label">Category <span>*</span></label>
            <select name="eventCategory" id="eventCategory" class="form-select" required>
                <option value="" disabled selected>Select...</option>
                <option value="Event" <?php if ($eventData["category"] == "Event")
                                            echo "selected"; ?>>Event</option>
                <option value="Task" <?php if ($eventData["category"] == "Task")
                                            echo "selected"; ?>>Task</option>
                <option value="Reminder" <?php if ($eventData["category"] == "Reminder")
                                                echo "selected"; ?>>Reminder
                </option>
            </select>
        </div>
        <div class="col-md-6">
            <label for="eventDueDate" class="form-label">Due Date <span>*</span></label>
            <input type="date" name="eventDueDate" id="eventDueDate" class="form-control"
                value="<?php echo $eventData["due_date"]; ?>" required>
        </div>
        <div class="col-md-6">
            <label for="eventClientId" class="form-label">Client <span>*</span></label>
            <select id="eventClientId" name="eventClientId" class="form-select" required>
                <option value="" disabled selected>Select...</option>
                <?php
                $clients = $productClass->AllClientsArray();
                foreach ($clients as $client) {
                    $clientId = $client["acc_id"];
                    $name = $client["acc_name"];
                    $selected = ($clientId == $eventData["client_id"]) ? "selected" : "";
                    echo "<option value='$clientId' $selected>$name</option>";
                }
                ?>
            </select>
        </div>
        <div class="col-md-6">
            <label for="eventAssignTo" class="form-label">Delegate Task <span>*</span></label>
            <select id="eventAssignTo" name="eventAssignTo" class="form-select" required>
                <option value="" disabled selected>Select...</option>
                <?php
                $users = $productClass->getStaffArray();
                foreach ($users as $user) {
                    $userId = $user["acc_id"];
                    $name = $user["acc_name"];
                    $selected = ($userId == $eventData["assign_to"]) ? "selected" : "";
                    echo "<option value='$userId' $selected>$name</option>";
                }
                ?>
            </select>
        </div>
        <div class="col-md-6">
            <label for="eventStatus" class="form-label">Approved Status <span>*</span></label>
            <select name="eventStatus" id="eventStatus" class="form-select" required>
                <option value="" disabled selected>Select...</option>
                <option value="Complete" <?php if ($eventData["status"] == "Complete")
                                                echo "selected"; ?>>Complete
                </option>
                <option value="Pending" <?php if ($eventData["status"] == "Pending")
                                            echo "selected"; ?>>Pending</option>
            </select>
        </div>
        <div class="col-md-12">
            <label for="eventDetails" class="form-label">Detail <span>*</span></label>
            <textarea class="form-control" id="eventDetails" name="eventDetails" rows="3"
                required><?php echo $eventData["detail"]; ?></textarea>
        </div>
        <div class="col-md-12">
            <button type="submit" class="btn btn-grd-primary px-4">Update</button>
        </div>
    </form>
</div>