<?php
session_start();
require_once "config.php";

// Sanitize input function
function sanitizeInput($input) {
    return htmlspecialchars(strip_tags(trim($input)));
}

// Check if a client ID is provided
if (!isset($_GET['id']) || empty($_GET['id'])) {
    die("Client ID is required");
}

$client_id = sanitizeInput($_GET['id']);

// Fetch client details
$sql = "SELECT * FROM clients WHERE id = $client_id";
$result = mysqli_query($conn, $sql);

if (!$result) {
    die("Error fetching client data: " . mysqli_error($conn));
}

$client = mysqli_fetch_assoc($result);

$assigned_to = $client['assigned_to'];

if (!$client) {
    die("Client not found");
}

?>
<?php include_once("include/head.php"); ?>

<div class="main-panel">
    <div class="content-wrapper">
        <div class="card">
            <div class="card-body">
                <div class="row">
                    <div class="col-md-3">
                        <h4 class="card-title">EDIT CLIENT</h4>
                    </div>
                </div>

                <div class="row">
                    <div class="col-12">
                        <form class="cmxform" id="commentForm" method="post" action="process_client.php?action=edit" enctype="multipart/form-data">
                            <input type="hidden" name="client_id" value="<?php echo $client['id']; ?>">
                            <fieldset>
                                <div class="row">
                                    <div class="form-group col-md-6">
                                        <label for="client_name">Company Name</label>
                                        <input class="form-control" type="text" name="Compname" placeholder="Company name" value="<?php echo $client['compname']; ?>" required>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="client_name">Company Conatct Number</label>
                                        <input class="form-control" type="text" name="Compnumber" placeholder="Company Conatct Number" value="<?php echo $client['compnum']; ?>" required>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="client_name">Contact Person</label>
                                        <input class="form-control" type="text" name="client_name" value="<?php echo $client['name']; ?>" required>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="phone">Contact Person Phone</label>
                                        <input class="form-control" type="text" name="phone" value="<?php echo $client['phone']; ?>" required>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="email">Email</label>
                                        <input class="form-control" type="email" name="email" value="<?php echo $client['email']; ?>" required>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="designation">Designation</label>
                                        <input class="form-control" type="text" name="designation" value="<?php echo $client['designation']; ?>" required>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="client_type">Type</label>
                                        <select id="client_type" class="form-control" name="client_type" required>
                                            <option value="">Choose Type</option>
                                            <option value="A" <?php if($client['type'] == 'A') echo 'selected'; ?>>A</option>
                                            <option value="B" <?php if($client['type'] == 'B') echo 'selected'; ?>>B</option>
                                            <option value="C" <?php if($client['type'] == 'C') echo 'selected'; ?>>C</option>
                                        </select>
                                    </div>
                                    <div class="form-group col-md-4">
                                        <label for="documents">Documents</label>
                                        <input class="form-control" type="file" id="documents" name="documents">
                                        <input type="hidden" name="current_documents" value="<?php echo $client['documents']; ?>">
                                        <?php if (!empty($client['documents'])): ?>
                                            <p>Current file: <a target="_blank" href="assets/images/clients/<?php echo $client['documents']; ?>"> <?php echo $client['documents']; ?></a></p>
                                        <?php endif; ?>
                                    </div>
                                    <div class="form-group col-md-2 bg-red">
                                        <img src="assets/images/clients/<?php echo $client['documents']; ?>" width="100%">
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="state">State</label>
                                        <select id="state" class="form-control" name="state" required>
                                            <option value="">Choose State</option>
                                            <option value="A" <?php if($client['type'] == 'A') echo 'selected'; ?>>A</option>
                                            <option value="B" <?php if($client['type'] == 'B') echo 'selected'; ?>>B</option>
                                            <option value="C" <?php if($client['type'] == 'C') echo 'selected'; ?>>C</option>
                                        </select>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="city">City</label>
                                         <select id="city" class="form-control" name="state" required>
                                            <option value="">Choose City</option>
                                            <option value="A" <?php if($client['type'] == 'A') echo 'selected'; ?>>A</option>
                                            <option value="B" <?php if($client['type'] == 'B') echo 'selected'; ?>>B</option>
                                            <option value="C" <?php if($client['type'] == 'C') echo 'selected'; ?>>C</option>
                                        </select>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="pin">Pin Code</label>
                                        <input class="form-control" type="text" name="pin" value="<?php echo $client['phone']; ?>" required>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="address">Full Address</label>
                                        <input type="text" class="form-control" name="address" value="<?php echo $client['address']; ?>" required>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="remarks">Remarks</label>
                                        <input type="text" class="form-control" name="remarks" value="<?php echo $client['remarks']; ?>" required>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="website">Website</label>
                                        <input class="form-control" type="text" name="website" value="<?php echo $client['website']; ?>">
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="status">Status</label>
                                        <select id="status" class="form-control" name="status" required>
                                            <option value="Contacted" <?php if($client['status'] == 'Contacted') echo 'selected'; ?>>Contacted</option>
                                            <option value="Visited" <?php if($client['status'] == 'Visited') echo 'selected'; ?>>Visited</option>
                                            <option value="Closing" <?php if($client['status'] == 'Closing') echo 'selected'; ?>>Closing</option>
                                            <option value="Standby" <?php if($client['status'] == 'Standby') echo 'selected'; ?>>Standby</option>
                                            <option value="Won" <?php if($client['status'] == 'Won') echo 'selected'; ?>>Won</option>
                                            <option value="Lost" <?php if($client['status'] == 'Lost') echo 'selected'; ?>>Lost</option>
                                            <option value="Cancelled" <?php if($client['status'] == 'Cancelled') echo 'selected'; ?>>Cancelled</option>
                                        </select>
                                    </div>
                                <div class="form-group col-md-6">
                                        <label for="phone">GST Number</label>
                                        <input class="form-control" type="text" name="gst" placeholder="GST Number" value= "<?php echo $client['gst']; ?>" required>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label for="assigned_to">Assign To</label>
                                        <select id="assigned_to" class="form-control" name="assigned_to">
                                            <option value="">None</option>
                                           <?php
                                           
                                            $sql = "SELECT employee_id, first_name, last_name FROM employees";
                                            $result = mysqli_query($conn, $sql);
                                            while ($row = mysqli_fetch_assoc($result)) {
                                                $selected = ($assigned_to == $row['employee_id']) ? 'selected' : '';
                                                echo '<option value="' . $row['employee_id'] . '" ' . $selected . '>' . $row['first_name'] . ' ' . $row['last_name'] . '</option>';
                                            }
                                            ?>
                                            
                                        </select>
                                    </div>
                                </div>
                                <input class="btn btn-primary" type="submit" value="Update" name="submit">
                            </fieldset>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include_once("include/footer.php"); ?>
