<?php
if (!isset($_GET['id'])) {
    header('Location: 505');
    exit;
}
include_once "global.php";

$pageTitle = 'Edit Project - ';
include_once 'header.php';

if (isset($_GET["id"])) {
    $projectId = $_GET["id"];

    $sql = "SELECT * FROM `projects` WHERE `id` = ?";
    $projectData = $dbF->getRow($sql, [$projectId]);

    $sqlAssignments = "SELECT * FROM `project_assignments` WHERE `project_id` = ?";
    $assignments = $dbF->getRows($sqlAssignments, [$projectId]);

    $assignedUsers = [
        'Designer' => [],
        'Developer' => [],
        'Worker' => [],
    ];

    foreach ($assignments as $assign) {
        $role = $assign['role'];
        $uid = $assign['user_id'];
        if (isset($assignedUsers[$role])) {
            $assignedUsers[$role][] = $uid;
        }
    }
}
?>

<div class="card">
    <div class="card-body p-4">
        <h5 class="mb-4">Edit Project</h5>
        <form id="projectEditForm" class="row g-3" method="post" enctype="multipart/form-data">
            <input type="hidden" name="projectId" value="<?php echo $projectId; ?>">
            <div class="col-md-6">
                <label for="projTitle" class="form-label">Title <span>*</span></label>
                <input type="text" class="form-control" id="projTitle" name="projTitle"
                    value="<?php echo $projectData["project_title"]; ?>" required>
            </div>
            <div class="col-md-6">
                <label for="projClientId" class="form-label">Client Name <span>*</span></label>
                <select id="projClientId" name="projClientId" 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 == $projectData["client_id"]) ? "selected" : "";
                        echo "<option value='$clientId' $selected>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="projCompany" class="form-label">Company Name <span>*</span></label>
                <input type="text" class="form-control" id="projCompany" name="projCompany"
                    value="<?php echo $projectData["company_name"]; ?>" required>
            </div>
            <div class="col-md-6">
                <label for="projCountry" class="form-label">Country <span>*</span></label>
                <select id="projCountry" name="projCountry" class="single-select-field"
                    data-placeholder="Select a country" required>
                    <option value="" disabled selected>Select...</option>
                    <?php echo $functions->countrySelectOption($projectData["country"]); ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="projDomain" class="form-label">Domain <span>*</span></label>
                <input type="text" class="form-control" id="projDomain" name="projDomain"
                    value="<?php echo $projectData["domain"]; ?>" required>
            </div>
            <div class="col-md-6">
                <label for="projTypeId" class="form-label">Type <span>*</span></label>
                <select id="projTypeId" name="projTypeId" class="form-select" required>
                    <option value="" disabled selected>Select...</option>
                    <?php
                    $projectTypes = $productClass->getProjectTypes();
                    foreach ($projectTypes as $projectType) {
                        $typeId = $projectType["id"];
                        $name = $projectType["name"];
                        $selected = ($typeId == $projectData["type_id"]) ? "selected" : "";
                        echo "<option value='$typeId' $selected>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="projCategoryId" class="form-label">Category <span>*</span></label>
                <select id="projCategoryId" name="projCategoryId" class="form-select" required>
                    <option value="" disabled selected>Select...</option>
                    <?php
                    $projectCategories = $productClass->getProjectCategories();
                    foreach ($projectCategories as $projectCategory) {
                        $categoryId = $projectCategory["id"];
                        $name = $projectCategory["category"];
                        $selected = ($categoryId == $projectData["category_id"]) ? "selected" : "";
                        echo "<option value='$categoryId' $selected>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="projStatusId" class="form-label">Status <span>*</span></label>
                <select id="projStatusId" name="projStatusId" class="form-select" required>
                    <option value="" disabled selected>Select...</option>
                    <?php
                    $projectStatuses = $productClass->getProjectStatuses();
                    foreach ($projectStatuses as $projectStatus) {
                        $statusId = $projectStatus["id"];
                        $name = $projectStatus["name"];
                        $selected = ($statusId == $projectData["status_id"]) ? "selected" : "";
                        echo "<option value='$statusId' $selected>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-12">
                <label for="projDesc" class="form-label">Description</label>
                <textarea class="form-control" id="projDesc" name="projDesc"
                    rows="3"><?php echo $projectData["description"]; ?></textarea>
            </div>
            <div class="col-md-12">
                <div class="d-flex align-items-center">
                    <?php
                    $image = $projectData["logo"];
                    $src = empty($image) ? "" : WEB_URL . '/images/' . $image;
                    $alt = empty($image) ? "No Photo" : $projectData["project_title"];
                    $logoLabelText = empty($image) ? "Upload Logo" : "Change Logo";
                    ?>
                    <img src="<?php echo $src; ?>" class="rounded-circle p-1 border bg-grd-primary" width="90"
                        height="90" alt="<?php echo $alt; ?>">
                    <div class="flex-grow-1 ms-3">
                        <input type="hidden" name="oldLogo" value="<?php echo $image; ?>">
                        <label for="projLogo" class="form-label"><?php echo $logoLabelText; ?></label>
                        <input type="file" class="form-control" id="projLogo" name="projLogo" accept="image/*">
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <label for="projSalesExecId" class="form-label">Sales Executive <span>*</span></label>
                <select id="projSalesExecId" name="projSalesExecId" class="form-select" required>
                    <option value="" disabled selected>Select...</option>
                    <?php
                    $users = $productClass->AllEmployeesAndManagersArray();
                    foreach ($users as $user) {
                        $userId = $user["acc_id"];
                        $name = $user["acc_name"];
                        $selected = ($userId == $projectData["sales_executive_id"]) ? "selected" : "";
                        echo "<option value='$userId' $selected>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="projClientCoordinatorId" class="form-label">Client Coordinator <span>*</span></label>
                <select id="projClientCoordinatorId" name="projClientCoordinatorId" class="form-select" required>
                    <option value="" disabled selected>Select...</option>
                    <?php
                    $users = $productClass->AllEmployeesAndManagersArray();
                    foreach ($users as $user) {
                        $userId = $user["acc_id"];
                        $name = $user["acc_name"];
                        $selected = ($userId == $projectData["client_coordinator_id"]) ? "selected" : "";
                        echo "<option value='$userId' $selected>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="projManagerId" class="form-label">Project Manager <span>*</span></label>
                <select id="projManagerId" name="projManagerId" class="form-select" required>
                    <option value="" disabled selected>Select...</option>
                    <?php
                    $users = $productClass->AllEmployeesAndManagersArray();
                    foreach ($users as $user) {
                        $userId = $user["acc_id"];
                        $name = $user["acc_name"];
                        $selected = ($userId == $projectData["manager_id"]) ? "selected" : "";
                        echo "<option value='$userId' $selected>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="projdesigner" class="form-label">Designer <span>*</span></label>
                <select id="projdesigner" name="projdesigner[]" class="multiple-select-field"
                    data-placeholder="Select Designer" multiple>
                    <?php
                    $users = $productClass->AllEmployeesAndManagersArray();
                    $designerIds = $assignedUsers['Designer'] ?? [];
                    foreach ($users as $user) {
                        $userId = $user["acc_id"];
                        $name = $user["acc_name"];
                        $selected = in_array($userId, $designerIds) ? "selected" : "";
                        echo "<option value='$userId' $selected>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="projdeveloper" class="form-label">Developer <span>*</span></label>
                <select id="projdeveloper" name="projdeveloper[]" class="multiple-select-field"
                    data-placeholder="Select Developer" multiple>
                    <?php
                    $users = $productClass->AllEmployeesAndManagersArray();
                    $developerIds = $assignedUsers['Developer'] ?? [];
                    foreach ($users as $user) {
                        $userId = $user["acc_id"];
                        $name = $user["acc_name"];
                        $selected = in_array($userId, $developerIds) ? "selected" : "";
                        echo "<option value='$userId' $selected>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="projworker" class="form-label">Worker <span>*</span></label>
                <select id="projworker" name="projworker[]" class="multiple-select-field"
                    data-placeholder="Select Worker" multiple>
                    <?php
                    $users = $productClass->AllEmployeesAndManagersArray();
                    $workerIds = $assignedUsers['Worker'] ?? [];
                    foreach ($users as $user) {
                        $userId = $user["acc_id"];
                        $name = $user["acc_name"];
                        $selected = in_array($userId, $workerIds) ? "selected" : "";
                        echo "<option value='$userId' $selected>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="projPropDate" class="form-label">Proposal Date <span>*</span></label>
                <input type="date" class="form-control" name="projPropDate" id="projPropDate"
                    value="<?php echo $projectData["proposal_date"]; ?>" required>
            </div>
            <div class="col-md-6">
                <label for="projStartDate" class="form-label">Start Date <span>*</span></label>
                <input type="date" class="form-control" name="projStartDate" id="projStartDate"
                    value="<?php echo $projectData["start_date"]; ?>" required>
            </div>
            <div class="col-md-6">
                <label for="projDesignStartDate" class="form-label">Design Start Date <span>*</span></label>
                <input type="date" class="form-control" name="projDesignStartDate" id="projDesignStartDate"
                    value="<?php echo $projectData["design_start_date"]; ?>" required>
            </div>
            <div class="col-md-6">
                <label for="projDevelopmentStartDate" class="form-label">Development Start Date <span>*</span></label>
                <input type="date" class="form-control" name="projDevelopmentStartDate" id="projDevelopmentStartDate"
                    value="<?php echo $projectData["development_start_date"]; ?>" required>
            </div>
            <div class="col-md-6">
                <label for="projDevelopmentEndDate" class="form-label">Development End Date <span>*</span></label>
                <input type="date" class="form-control" name="projDevelopmentEndDate" id="projDevelopmentEndDate"
                    value="<?php echo $projectData["development_end_date"]; ?>" required>
            </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';
?>