<?php
session_start();
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
    header("Location: login.php");
    exit;
}
include 'config.php';

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

$cid = mysqli_real_escape_string($conn, $_GET['id']);

if (isset($_POST['update_customer'])) {
    // নতুন ইনপুটগুলো গ্রহণ করা হচ্ছে
    $new_customer_id = mysqli_real_escape_string($conn, $_POST['customer_id']);
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $phone = mysqli_real_escape_string($conn, $_POST['phone']);
    $address = mysqli_real_escape_string($conn, $_POST['address']);
    $nid_no = mysqli_real_escape_string($conn, $_POST['nid_no']);
    $bill = mysqli_real_escape_string($conn, $_POST['bill_amount']);
    $expiry = mysqli_real_escape_string($conn, $_POST['expiry_date']);
    $status = mysqli_real_escape_string($conn, $_POST['status']);
    $connection_date = mysqli_real_escape_string($conn, $_POST['connection_date']);
    $balance = mysqli_real_escape_string($conn, $_POST['balance']);

    // আপডেট কুয়েরি
    $sql = "UPDATE customers SET 
            customer_id='$new_customer_id',
            name='$name', 
            phone='$phone', 
            address='$address', 
            nid_no='$nid_no', 
            bill_amount='$bill', 
            expiry_date='$expiry', 
            status='$status',
            connection_date='$connection_date',
            balance='$balance'
            WHERE customer_id = '$cid'";

    if (mysqli_query($conn, $sql)) {
        header("Location: index.php?success=1");
        exit;
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}

$result = mysqli_query($conn, "SELECT * FROM customers WHERE customer_id = '$cid'");
$row = mysqli_fetch_assoc($result);

if (!$row) {
    echo "কাস্টমার খুঁজে পাওয়া যায়নি।";
    exit;
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Customer</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
    <style>
        body { font-family: 'Inter', sans-serif; background: #f9fafb; margin: 0; padding: 20px; }
        .container { max-width: 500px; margin: 0 auto; background: white; padding: 20px; border-radius: 12px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
        .form-group { margin-bottom: 15px; }
        .form-group label { display: block; margin-bottom: 5px; font-weight: 600; font-size: 14px; }
        .form-group input, .form-group select { width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 8px; box-sizing: border-box; font-size: 16px; }
        .btn { width: 100%; padding: 14px; border: none; border-radius: 8px; cursor: pointer; font-weight: 600; margin-top: 10px; font-size: 16px; }
        .btn-save { background: #4f46e5; color: white; }
        .btn-back { background: #e5e7eb; color: #374151; text-align: center; display: block; text-decoration: none; padding: 14px; border-radius: 8px; margin-top: 10px; }
        @media (max-width: 600px) {
            body { padding: 0; }
            .container { max-width: 100%; border-radius: 0; min-height: 100vh; box-shadow: none; }
        }
    </style>
</head>
<body>

<div class="container">
    <h2>কাস্টমার সকল তথ্য এডিট করুন</h2>
    <form method="POST">
        <div class="form-group">
            <label>Customer ID</label>
            <input type="text" name="customer_id" value="<?php echo htmlspecialchars($row['customer_id']); ?>" required>
        </div>
        <div class="form-group">
            <label>নাম</label>
            <input type="text" name="name" value="<?php echo htmlspecialchars($row['name']); ?>" required>
        </div>
        <div class="form-group">
            <label>ফোন নম্বর</label>
            <input type="number" name="phone" value="<?php echo htmlspecialchars($row['phone']); ?>">
        </div>
        <div class="form-group">
            <label>NID নম্বর</label>
            <input type="number" name="nid_no" value="<?php echo htmlspecialchars($row['nid_no'] ?? ''); ?>">
        </div>
        <div class="form-group">
            <label>ঠিকানা</label>
            <input type="text" name="address" value="<?php echo htmlspecialchars($row['address']); ?>">
        </div>
        <div class="form-group">
            <label>বিল এর পরিমাণ</label>
            <input type="number" name="bill_amount" value="<?php echo htmlspecialchars($row['bill_amount']); ?>" required>
        </div>
        <div class="form-group">
            <label>Connection Date</label>
            <input type="date" name="connection_date" value="<?php echo htmlspecialchars($row['connection_date'] ?? ''); ?>">
        </div>
        <div class="form-group">
            <label>Balance</label>
            <input type="number" step="0.01" name="balance" value="<?php echo htmlspecialchars($row['balance'] ?? '0'); ?>">
        </div>
        <div class="form-group">
            <label>মেয়াদ (Expiry Date)</label>
            <input type="date" name="expiry_date" value="<?php echo htmlspecialchars($row['expiry_date']); ?>" required>
        </div>
        <div class="form-group">
            <label>স্ট্যাটাস</label>
            <select name="status">
                <option value="active" <?php if($row['status']=='active') echo 'selected'; ?>>Active</option>
                <option value="inactive" <?php if($row['status']=='inactive') echo 'selected'; ?>>Inactive</option>
                <option value="deactivated" <?php if($row['status']=='deactivated') echo 'selected'; ?>>Deactivated</option>
            </select>
        </div>
        <button type="submit" name="update_customer" class="btn btn-save">আপডেট করুন</button>
        <a href="index.php" class="btn-back">ফিরে যান</a>
    </form>
</div>

</body>
</html>
