<?php
session_start();
require("php_con.php");
if(!isset($_COOKIE['user_id'])){
    echo "<script>window.location='logout';</script>";
    exit();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $cs_id = $_POST['cs_id'];
    $cs_name = $_POST['cs_name'];
    $cs_mobile = $_POST['cs_mobile'];
    $cs_address = $_POST['cs_address'];

    $stmt = $connation->prepare("UPDATE customer_details SET cs_name = ?, cs_mobile = ?, cs_address = ? WHERE cs_id = ?");
    $stmt->bind_param("sssi", $cs_name, $cs_mobile, $cs_address, $cs_id);
    
    // Handle photo upload
    if (isset($_FILES['cs_photo']) && $_FILES['cs_photo']['error'] == 0) {
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["cs_photo"]["name"]);
        if (move_uploaded_file($_FILES["cs_photo"]["tmp_name"], $target_file)) {
            $stmt = $connation->prepare("UPDATE customer_details SET cs_photo = ? WHERE cs_id = ?");
            $stmt->bind_param("si", $target_file, $cs_id);
            $stmt->execute();
        }
    }

    // Handle ID photo upload
    if (isset($_FILES['cs_id_photo']) && $_FILES['cs_id_photo']['error'] == 0) {
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["cs_id_photo"]["name"]);
        if (move_uploaded_file($_FILES["cs_id_photo"]["tmp_name"], $target_file)) {
            $stmt = $connation->prepare("UPDATE customer_details SET cs_id_photo = ? WHERE cs_id = ?");
            $stmt->bind_param("si", $target_file, $cs_id);
            $stmt->execute();
        }
    }

    if ($stmt->execute()) {
        echo "<div class='alert alert-success'>Customer Updated Successfully!</div>";
    } else {
        echo "<div class='alert alert-danger'>Error updating customer: " . $stmt->error . "</div>";
    }
}

$cs_id = $_GET['id'] ?? null;
if ($cs_id) {
    $stmt = $connation->prepare("SELECT * FROM customer_details WHERE cs_id = ?");
    $stmt->bind_param("i", $cs_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $customer = $result->fetch_assoc();
} else {
    echo "<div class='alert alert-danger'>No customer ID provided.</div>";
    exit();
}
?>

<?php require("header.php"); ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <title>Edit Customer</title>
    <style>
        .customer-photo {
            width: 100px;
            height: 100px;
            object-fit: cover;
            border-radius: 50%;
        }
    </style>
</head>
<body>
<?php include 'navbar.php'; ?>
<div class="container mt-5">
    <h2>Edit Customer</h2>
    <form action="edit_customer.php" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="cs_id" value="<?php echo $customer['cs_id']; ?>">
        <div class="form-group">
            <label for="name">Full Name:</label>
            <input type="text" class="form-control" id="name" name="cs_name" value="<?php echo $customer['cs_name']; ?>" required>
        </div>
        <div class="form-group">
            <label for="mobile">Mobile Number:</label>
            <input type="text" class="form-control" id="mobile" name="cs_mobile" value="<?php echo $customer['cs_mobile']; ?>" required>
        </div>
        <div class="form-group">
            <label for="address">Address:</label>
            <textarea class="form-control" id="address" name="cs_address" required><?php echo $customer['cs_address']; ?></textarea>
        </div>
        <div class="form-group">
            <label>Current Photo:</label>
            <img src="<?php echo $customer['cs_photo'] ? $customer['cs_photo'] : 'uploads/default.jpg'; ?>" class="customer-photo" alt="Customer Photo">
        </div>
        <div class="form-group">
            <label for="photo">Update Photo:</label>
            <input type="file" class="form-control-file" id="photo" name="cs_photo">
        </div>
        <div class="form-group">
            <label>Current ID Photo:</label>
            <img src="<?php echo $customer['cs_id_photo'] ? $customer['cs_id_photo'] : 'uploads/default.jpg'; ?>" class="customer-photo" alt="Customer ID Photo">
        </div>
        <div class="form-group">
            <label for="id_photo">Update ID Photo:</label>
            <input type="file" class="form-control-file" id="id_photo" name="cs_id_photo">
        </div>
        <button type="submit" class="btn btn-primary">Update Customer</button>
    </form>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>