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

function generateUniquePin($connation) {
    do {
        $pin = mt_rand(1000, 9999); // Generate a random 4-digit number
        $stmt = $connation->prepare("SELECT cs_pin FROM customer_details WHERE cs_pin = ?");
        $stmt->bind_param("s", $pin);
        $stmt->execute();
        $result = $stmt->get_result();
    } while ($result->num_rows > 0);
    
    return $pin;
}

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

    // Photo upload logic
    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)) {
            $photo_path = $target_file;
        }
    }

    // ID Photo upload logic
    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)) {
            $id_photo_path = $target_file;
        }
    }

    // Generate unique PIN
    $cs_pin = generateUniquePin($connation);

    // Insert customer data
    $stmt = $connation->prepare("INSERT INTO customer_details (cs_name, cs_mobile, cs_address, cs_photo, cs_id_photo, cs_pin) VALUES (?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssssss", $cs_name, $cs_mobile, $cs_address, $photo_path, $id_photo_path, $cs_pin);
    if ($stmt->execute()) {
        echo "<div class='alert alert-success'>Customer Added Successfully! Customer PIN: " . $cs_pin . "</div>";
    } else {
        echo "<div class='alert alert-danger'>Error adding customer: " . $stmt->error . "</div>";
    }
}
?>

<?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>Customer Management</title>
</head>
<body>
<?php include 'navbar.php'; ?>
<div class="container mt-5">
    <h2>Customer Management</h2>
    <form action="add_customers.php" method="POST" enctype="multipart/form-data">
        <div class="form-group">
            <label for="name">Full Name:</label>
            <input type="text" class="form-control" id="name" name="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" required>
        </div>
        <div class="form-group">
            <label for="address">Address:</label>
            <textarea class="form-control" id="address" name="cs_address" required></textarea>
        </div>
        <div class="form-group">
            <label for="photo">Upload Photo:</label>
            <input type="file" class="form-control-file" id="photo" name="cs_photo">
        </div>
        <div class="form-group">
            <label for="id_photo">Upload 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">Add Customer</button>
    </form>

    <div class="mt-5">
        <h3>Customer Actions</h3>
        <a href="view_customers.php" class="btn btn-info">View Customers</a>

    </div>
</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>