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

include_once "global.php";

if (isset($_GET["id"])) {
    $userId = $functions->decode($_GET["id"]);
    $userData = $productClass->getUserById($userId);
}

if (empty($userData)) {
    header('Location: 404');
    exit;
}


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

$userDetail = $productClass->getUserDetailsById($userId);

$emails = $productClass->getUserEmailsById($userId);
$phones = $productClass->getUserPhonesById($userId);

$country = $webClass->webUserInfoArray($userDetail, 'country');
$clientStatus = $webClass->webUserInfoArray($userDetail, 'clientStatus');
?>

<div class="card">
    <div class="card-body p-4">
        <h5 class="mb-4">Edit Client</h5>
        <form id="userEditForm" class="row g-3" method="post" enctype="multipart/form-data">
            <input type="hidden" name="userId" value="<?php echo $userId; ?>">
            <input type="hidden" name="type" value="4">
            <div class="col-md-6">
                <label for="name" class="form-label">Full Name <span>*</span></label>
                <input type="text" class="form-control" id="name" name="name"
                    value="<?php echo $userData["acc_name"]; ?>">
            </div>
            <div class="col-md-6">
                <label for="status" class="form-label">Status</label>
                <select id="status" name="signUp[clientStatus]" class="form-select">
                    <option value="">Select...</option>
                    <option value="Cold Lead" <?php if ($clientStatus == "Cold Lead")
                                                    echo 'selected'; ?>>Cold Lead
                    </option>
                    <option value="Hot Lead" <?php if ($clientStatus == "Hot Lead")
                                                    echo 'selected'; ?>>Hot Lead</option>
                    <option value="Work Assigned" <?php if ($clientStatus == "Work Assigned")
                                                        echo 'selected'; ?>>Work
                        Assigned</option>
                    <option value="Client Left" <?php if ($clientStatus == "Client Left")
                                                    echo 'selected'; ?>>Client Left
                    </option>
                </select>
            </div>
            <div class="col-md-6">
                <label for="email" class="form-label">Email <span>*</span></label>
                <input type="email" class="form-control" id="email" value="<?php echo $userData["acc_email"]; ?>"
                    readonly>
                <div id="extraEmails">
                    <?php foreach ($emails as $email): ?>
                        <input type="email" class="form-control" name="extra_emails[]" value="<?php echo $email; ?>">
                    <?php endforeach; ?>
                </div>
                <button type="button" class="btn btn-sm btn-success mt-2" onclick="addEmailField()">Add More
                    Email</button>
            </div>
            <div class="col-md-6">
                <label for="phone" class="form-label">Phone <span>*</span></label>
                <input type="tel" class="form-control intl-tel-input" id="phone" name="phone"
                    value="<?php echo $userData["acc_phone"]; ?>">
                <div id="extraPhones">
                    <?php foreach ($phones as $phone): ?>
                        <input type="tel" class="form-control intl-tel-input" name="extra_phones[]"
                            value="<?php echo $phone; ?>">
                    <?php endforeach; ?>
                </div>
                <button type="button" class="btn btn-sm btn-success mt-2" onclick="addPhoneField()">Add More
                    Phone</button>
            </div>
            <div class="col-md-6">
                <label for="country" class="form-label">Country <span>*</span></label>
                <select id="country" name="signUp[country]" class="single-select-field"
                    data-placeholder="Select a country" required>
                    <option selected="" value="">Select...</option>
                    <?php echo $functions->countrySelectOption($country); ?>
                </select>
            </div>
            <div class="col-md-12">
                <div class="d-flex align-items-center">
                    <?php
                    $image = $productClass->getUserImageById($userId);
                    $src = empty($image) ? "" : WEB_URL . '/images/' . $image;
                    $alt = empty($image) ? "No Photo" : $userData["acc_name"];
                    ?>
                    <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="oldImg" value="<?php echo $image; ?>">
                        <label for="image" class="form-label">Change Photo</label>
                        <input type="file" class="form-control" id="image" name="image">
                    </div>
                </div>
            </div>
            <div class="col-md-12">
                <button type="submit" class="btn btn-grd-primary px-4">Save</button>
            </div>
        </form>
    </div>
</div>

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