<?php
session_start();
require_once '../config/database.php';

if (!isset($_SESSION["user_id"]) || $_SESSION["role"] !== "admin") {
    header("Location: ../auth/login.php");
    exit;
}

if (!isset($_GET["id"])) {
    header("Location: customers.php");
    exit;
}

$id = intval($_GET["id"]);
$error = "";
$success = "";

$stmt = $pdo->prepare("SELECT * FROM customers WHERE id = ? LIMIT 1");
$stmt->execute([$id]);
$customer = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$customer) {
    die("Customer not found.");
}

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $full_name = trim($_POST["full_name"]);
    $phone = trim($_POST["phone"]);
    $email = trim($_POST["email"]);
    $status = $_POST["status"];

    if ($full_name === "" || $email === "") {
        $error = "Name and email are required.";
    } else {
        $stmt = $pdo->prepare("
            UPDATE customers
            SET full_name = ?, phone = ?, email = ?, status = ?
            WHERE id = ?
        ");
        $stmt->execute([$full_name, $phone, $email, $status, $id]);

        if (!empty($customer["user_id"])) {
            $stmt = $pdo->prepare("
                UPDATE users
                SET name = ?, phone = ?, email = ?, status = ?
                WHERE id = ?
            ");
            $stmt->execute([$full_name, $phone, $email, $status, $customer["user_id"]]);
        }

        $success = "Customer updated successfully.";

        $stmt = $pdo->prepare("SELECT * FROM customers WHERE id = ? LIMIT 1");
        $stmt->execute([$id]);
        $customer = $stmt->fetch(PDO::FETCH_ASSOC);
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Edit Customer</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f4f6f8;
            margin: 0;
            padding: 30px;
        }

        .container {
            max-width: 720px;
            margin: auto;
        }

        .card {
            background: #ffffff;
            border-radius: 14px;
            padding: 28px;
            box-shadow: 0 4px 14px rgba(0,0,0,0.08);
        }

        h1 {
            margin-top: 0;
            color: #111827;
        }

        label {
            font-weight: bold;
            color: #374151;
            display: block;
            margin-bottom: 6px;
        }

        input, select {
            width: 100%;
            padding: 12px;
            border: 1px solid #d1d5db;
            border-radius: 8px;
            font-size: 15px;
            box-sizing: border-box;
        }

        .form-group {
            margin-bottom: 18px;
        }

        .readonly {
            background: #f3f4f6;
            color: #6b7280;
        }

        .btn {
            padding: 11px 16px;
            border-radius: 8px;
            border: none;
            text-decoration: none;
            display: inline-block;
            font-size: 14px;
            cursor: pointer;
        }

        .btn-primary {
            background: #2563eb;
            color: white;
        }

        .btn-gray {
            background: #6b7280;
            color: white;
        }

        .success {
            color: #15803d;
            background: #dcfce7;
            padding: 12px;
            border-radius: 8px;
        }

        .error {
            color: #b91c1c;
            background: #fee2e2;
            padding: 12px;
            border-radius: 8px;
        }

        .actions {
            display: flex;
            gap: 10px;
            margin-top: 22px;
        }

        .info-box {
            background: #eff6ff;
            padding: 14px;
            border-radius: 10px;
            margin-bottom: 22px;
            color: #1e3a8a;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="card">
        <h1>Edit Customer</h1>

        <div class="info-box">
            Customer Code: <strong><?php echo htmlspecialchars($customer["customer_code"]); ?></strong><br>
            Current Balance: <strong><?php echo number_format($customer["balance"], 2); ?></strong>
        </div>

        <?php if ($error): ?>
            <p class="error"><?php echo htmlspecialchars($error); ?></p>
        <?php endif; ?>

        <?php if ($success): ?>
            <p class="success"><?php echo htmlspecialchars($success); ?></p>
        <?php endif; ?>

        <form method="POST">
            <div class="form-group">
                <label>Customer Code</label>
                <input class="readonly" type="text" value="<?php echo htmlspecialchars($customer["customer_code"]); ?>" disabled>
            </div>

            <div class="form-group">
                <label>Full Name</label>
                <input type="text" name="full_name" value="<?php echo htmlspecialchars($customer["full_name"]); ?>" required>
            </div>

            <div class="form-group">
                <label>Phone</label>
                <input type="text" name="phone" value="<?php echo htmlspecialchars($customer["phone"]); ?>">
            </div>

            <div class="form-group">
                <label>Email</label>
                <input type="email" name="email" value="<?php echo htmlspecialchars($customer["email"]); ?>" required>
            </div>

            <div class="form-group">
                <label>Status</label>
                <select name="status">
                    <option value="active" <?php if ($customer["status"] === "active") echo "selected"; ?>>Active</option>
                    <option value="inactive" <?php if ($customer["status"] === "inactive") echo "selected"; ?>>Inactive</option>
                </select>
            </div>

            <div class="actions">
                <button class="btn btn-primary" type="submit">Update Customer</button>
                <a class="btn btn-gray" href="customers.php">Back to Customers</a>
            </div>
        </form>
    </div>
</div>

<?php require __DIR__ . '/../config/auto_update.php'; ?>
</body>
</html>