<?php
include "dbconnect.php";

// एजेंट की जानकारी प्राप्त करें
if (isset($_GET['id'])) {
    $id = $_GET['id'];
    $stmt = $con->prepare("SELECT * FROM agents WHERE id = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    $agent = $result->fetch_assoc();
    $stmt->close();
}

// एजेंट की जानकारी अपडेट करें
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $father_name = $_POST['father_name'];
    $qualification = $_POST['qualification'];
    $mobile_no = $_POST['mobile_no'];
    $email_id = $_POST['email_id'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $message = $_POST['message'];
    $adharno = $_POST['adharno'];  // ✅ आधार नंबर
    $pancard = $_POST['pancard'];  // ✅ पैनकार्ड नंबर

    $stmt = $con->prepare("UPDATE agents SET name=?, father_name=?, qualification=?, mobile_no=?, email_id=?, address=?, city=?, state=?, adharno=?, pancard=?, message=? WHERE id=?");
    $stmt->bind_param("sssssssssssi", $name, $father_name, $qualification, $mobile_no, $email_id, $address, $city, $state, $adharno, $pancard, $message, $id);
    
    if ($stmt->execute()) {
        header("Location: agentlist.php?success=updated");
        exit();
    } else {
        echo "<div class='alert alert-danger'>❌ Error updating agent!</div>";
    }
    $stmt->close();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Edit Agent</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <h3 class="text-center">Edit Agent</h3>
        <form method="POST">
            <input type="text" name="name" value="<?= htmlspecialchars($agent['name']) ?>" class="form-control" required>
            <input type="text" name="father_name" value="<?= htmlspecialchars($agent['father_name']) ?>" class="form-control">
            <input type="text" name="qualification" value="<?= htmlspecialchars($agent['qualification']) ?>" class="form-control">
            <input type="text" name="mobile_no" value="<?= htmlspecialchars($agent['mobile_no']) ?>" class="form-control" required>
            <input type="email" name="email_id" value="<?= htmlspecialchars($agent['email_id']) ?>" class="form-control" required>
            <input type="text" name="adharno" value="<?= htmlspecialchars($agent['adharno']) ?>" class="form-control" required>  <!-- ✅ आधार नंबर -->
            <input type="text" name="pancard" value="<?= htmlspecialchars($agent['pancard']) ?>" class="form-control" required>  <!-- ✅ पैनकार्ड नंबर -->
            <textarea name="address" class="form-control"><?= htmlspecialchars($agent['address']) ?></textarea>
            <input type="text" name="city" value="<?= htmlspecialchars($agent['city']) ?>" class="form-control">
            <input type="text" name="state" value="<?= htmlspecialchars($agent['state']) ?>" class="form-control">
            <textarea name="message" class="form-control"><?= htmlspecialchars($agent['message']) ?></textarea>
            <button type="submit" class="btn btn-primary mt-2">Update</button>
        </form>
    </div>
</body>
</html>
