<?php
if (!isset($_GET['id'])) {
    header('Location: 404');
    exit;
}

include_once "global.php";

if (isset($_GET["id"])) {
    $ticketId = $functions->decode($_GET["id"]);

    $sql = "SELECT t.*, p.project_title FROM tickets t JOIN projects p ON p.id = t.project_id WHERE t.id = ?";
    $ticketsData = $dbF->getRow($sql, [$ticketId]);
}

if ($dbF->rowCount == 0) {
    header('Location: 404');
    exit;
}

$pageTitle = 'Edit Ticket - ';
include_once 'header.php';
?>

<div class="card">
    <div class="card-body p-4">
        <h5 class="mb-4">Edit Ticket</h5>
        <form id="ticketEditForm" class="row g-3" method="post">
            <input type="hidden" name="ticketId" value="<?php echo $ticketId; ?>">
            <?php if ($webUserType === 1) { ?>
                <div class="col-md-6">
                    <label for="clientId" class="form-label">Client <span>*</span></label>
                    <select id="clientId" disabled name="clientId" class="form-select" required>
                        <option selected value="<?php echo $ticketsData["client_id"]; ?>">
                            <?php echo $functions->webUserName($ticketsData["client_id"], 'acc_name'); ?>
                        </option>
                    </select>
                </div>
            <?php } else { ?>
                <input type="hidden" name="clientId" value="<?php echo $ticketsData["client_id"]; ?>">
            <?php } ?>
            <div class="col-md-6">
                <label for="projectId" class="form-label">Project <span>*</span></label>
                <select class="form-select" disabled name="projectId" id="projectId">
                    <option selected value="<?php echo $ticketsData["project_id"]; ?>">
                        <?php echo $ticketsData["project_title"]; ?>
                    </option>
                </select>
            </div>
            <div class="col-md-6">
                <label for="category" class="form-label">Category <span>*</span></label>
                <select id="category" name="category" class="form-select" required>
                    <option selected="" value="">Select...</option>
                    <?php
                    $ticketCategories = $productClass->getTicketCategories();
                    foreach ($ticketCategories as $ticketCategory) {
                        $categoryId = $ticketCategory["id"];
                        $name = $ticketCategory["category"];
                        $selected = ($categoryId == $ticketsData["category"]) ? "selected" : "";
                        echo "<option value='$categoryId' $selected>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="deadline" class="form-label">Deadline <span>*</span></label>
                <input type="date" name="deadline" id="deadline" class="form-control"
                    value="<?php echo $ticketsData["deadline"]; ?>" required>
            </div>
            <?php if ($webUserType === 1): ?>
                <div class="col-md-6">
                    <label for="assignto" class="form-label">Assign To</label>
                    <select id="assignto" name="assignto" class="form-select">
                        <option value="" disabled selected>Select...</option>
                        <?php
                        $employees = $productClass->AllEmployeesArray();
                        foreach ($employees as $employee) {
                            $employeeId = $employee["acc_id"];
                            $employeeName = $employee["acc_name"];
                            $selected = ($employeeId == $ticketsData["assigned_to"]) ? "selected" : "";
                            echo "<option value='$employeeId' $selected>$employeeName</option>";
                        }
                        ?>
                    </select>
                </div>
            <?php endif; ?>
            <div class="col-md-3">
                <label class="form-label d-block">Status</label>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="radio" name="status" id="statusOpen" value="Open" <?php if ($ticketsData["status"] == "Open")
                                                                                                                echo "checked"; ?>>
                    <label class="form-check-label" for="statusOpen">Open</label>
                </div>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="radio" name="status" id="statusClosed" value="Closed" <?php if ($ticketsData["status"] == "Closed")
                                                                                                                    echo "checked"; ?>>
                    <label class="form-check-label" for="statusClosed">Closed</label>
                </div>
            </div>
            <div class="col-md-3">
                <label class="form-label d-block">Priority <span>*</span></label>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="radio" name="priority" id="priorityNormal" value="Normal"
                        <?php if ($ticketsData["priority"] == "Normal")
                            echo "checked"; ?>>
                    <label class="form-check-label" for="priorityNormal">Normal</label>
                </div>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="radio" name="priority" id="priorityUrgent" value="Urgent"
                        <?php if ($ticketsData["priority"] == "Urgent")
                            echo "checked"; ?>>
                    <label class="form-check-label" for="priorityUrgent">Urgent</label>
                </div>
            </div>
            <div class="col-md-6">
                <label for="complaint" class="form-label">Complaint</label>
                <input type="text" name="complaint" id="complaint" class="form-control"
                    value="<?php echo $ticketsData["complaint"]; ?>" required>
            </div>
            <div class="col-md-12">
                <label for="description" class="form-label">Description</label>
                <textarea class="form-control" id="description" name="description"
                    rows="3"><?php echo $ticketsData["description"]; ?></textarea>
            </div>
            <div class="col-md-12">
                <button type="submit" class="btn btn-grd-primary px-4">Submit</button>
            </div>
        </form>
    </div>
</div>

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