<?php
session_start();

if (!isset($_SESSION['user_id'])) {
    header("Location: /beauty_pos/login.php");
    exit;
}

include_once __DIR__ . "/../config/database.php";

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    $_SESSION['error'] = "Invalid customer.";
    header("Location: /beauty_pos/customers/index.php");
    exit;
}

$id = (int)$_GET['id'];

$getCustomer = mysqli_query(
    $conn,
    "SELECT *
     FROM customers
     WHERE id='$id'
     LIMIT 1"
);

if (mysqli_num_rows($getCustomer) == 0) {
    $_SESSION['error'] = "Customer not found.";
    header("Location: /beauty_pos/customers/index.php");
    exit;
}

$data = mysqli_fetch_assoc($getCustomer);

$customer_name = $data['customer_name'];
$phone = $data['phone'];
$email = $data['email'];
$address = $data['address'];
$loyalty_points = $data['loyalty_points'];
$balance = $data['balance'];
$notes = $data['notes'];

$errors = [];

if ($_SERVER['REQUEST_METHOD'] == "POST") {

    $customer_name = trim(mysqli_real_escape_string($conn, $_POST['customer_name']));
    $phone = trim(mysqli_real_escape_string($conn, $_POST['phone']));
    $email = trim(mysqli_real_escape_string($conn, $_POST['email']));
    $address = trim(mysqli_real_escape_string($conn, $_POST['address']));
    $loyalty_points = (int)$_POST['loyalty_points'];
    $balance = (float)$_POST['balance'];
    $notes = trim(mysqli_real_escape_string($conn, $_POST['notes']));

    if ($customer_name == "") {
        $errors[] = "Customer name is required.";
    }

    if ($phone != "") {

        $checkPhone = mysqli_query(
            $conn,
            "SELECT id
             FROM customers
             WHERE phone='$phone'
             AND id!='{$id}'
             LIMIT 1"
        );

        if (mysqli_num_rows($checkPhone) > 0) {
            $errors[] = "Phone number already exists.";
        }

    }

    if ($email != "") {

        $checkEmail = mysqli_query(
            $conn,
            "SELECT id
             FROM customers
             WHERE email='$email'
             AND id!='{$id}'
             LIMIT 1"
        );

        if (mysqli_num_rows($checkEmail) > 0) {
            $errors[] = "Email already exists.";
        }

    }

    if (empty($errors)) {

        $update = mysqli_query(
            $conn,
            "UPDATE customers SET
                customer_name='$customer_name',
                phone='$phone',
                email='$email',
                address='$address',
                loyalty_points='$loyalty_points',
                balance='$balance',
                notes='$notes'
            WHERE id='$id'"
        );

        if ($update) {

            $_SESSION['success'] = "Customer updated successfully.";

            header("Location: /beauty_pos/customers/index.php");
            exit;

        } else {

            $errors[] = "Something went wrong.";

        }

    }

}

include_once __DIR__ . "/../includes/header.php";
include_once __DIR__ . "/../includes/sidebar.php";
?>

<div class="overlay" id="overlay"></div>

<div class="main-content">

<?php include_once __DIR__ . "/../includes/topbar.php"; ?>

<div class="container-fluid py-4">

<div class="d-flex justify-content-between align-items-center flex-wrap mb-4">

<div>

<h3 class="fw-bold mb-1">

Edit Customer

</h3>

<p class="text-muted mb-0">

Update customer information

</p>

</div>

<a href="/beauty_pos/customers/index.php"
class="btn btn-secondary">

<i class="fa fa-arrow-left me-2"></i>

Back

</a>

</div>

<?php if(!empty($errors)){ ?>

<div class="alert alert-danger">

<ul class="mb-0">

<?php foreach($errors as $error){ ?>

<li><?php echo $error; ?></li>

<?php } ?>

</ul>

</div>

<?php } ?>

<div class="card rounded-4 shadow-sm border-0">

<div class="card-body">

<form method="POST">

<div class="row g-3">

<div class="col-lg-6 col-md-6 col-12">

<label class="form-label">

Customer Name <span class="text-danger">*</span>

</label>

<input
type="text"
name="customer_name"
class="form-control"
required
value="<?php echo htmlspecialchars($customer_name); ?>">

</div>

<div class="col-lg-6 col-md-6 col-12">

<label class="form-label">

Phone

</label>

<input
type="text"
name="phone"
class="form-control"
value="<?php echo htmlspecialchars($phone); ?>">
</div>

<div class="col-lg-6 col-md-6 col-12">

<label class="form-label">

Email

</label>

<input
type="email"
name="email"
class="form-control"
value="<?php echo htmlspecialchars($email); ?>">
</div>

<div class="col-lg-6 col-md-6 col-12">

<label class="form-label">

Opening Balance

</label>

<input
type="number"
step="0.01"
min="0"
name="balance"
class="form-control"
value="<?php echo htmlspecialchars($balance); ?>">
</div>
<div class="col-lg-6 col-md-6 col-12">

    <label class="form-label">

        Loyalty Points

    </label>

    <input
        type="number"
        name="loyalty_points"
        min="0"
        class="form-control"
        value="<?php echo htmlspecialchars($loyalty_points); ?>">

</div>

<div class="col-lg-6 col-md-6 col-12">

    <label class="form-label">

        Address

    </label>

    <textarea
        name="address"
        rows="4"
        class="form-control"><?php echo htmlspecialchars($address); ?></textarea>

</div>

<div class="col-12">

    <label class="form-label">

        Notes

    </label>

    <textarea
        name="notes"
        rows="4"
        class="form-control"><?php echo htmlspecialchars($notes); ?></textarea>

</div>

<div class="col-12">

    <hr>

</div>

<div class="col-lg-3 col-md-4 col-sm-6 col-12">

    <button
        type="submit"
        class="btn btn-primary w-100">

        <i class="fa fa-save me-2"></i>

        Update Customer

    </button>

</div>

<div class="col-lg-3 col-md-4 col-sm-6 col-12">

    <a
        href="/beauty_pos/customers/index.php"
        class="btn btn-secondary w-100">

        Cancel

    </a>

</div>

</div>

</form>

</div>

</div>

</div>

<?php

include_once __DIR__ . "/../includes/footer.php";

?>