<?php
$host = "localhost";
$username = "job_portal";
$password = "job_portal";
$dbname = "job_portal";

// DB connection
$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Ensure ID is passed for update
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
    die("❌ Invalid ID for update.");
}

function get_post($key) {
    return isset($_POST[$key]) ? trim($_POST[$key]) : null;
}

$entry_date         = get_post('entry_date');
$member_return_date = get_post('member_return_date');
$applicant_name     = get_post('applicant_name');
$applicant_title    = get_post('applicant_title');
$address            = get_post('address');
$country            = get_post('country');
$post_code          = get_post('post_code');
$phone_home         = get_post('phone_home');
$phone_work         = get_post('phone_work');
$mobile_no          = get_post('mobile_no');
$fax                = get_post('fax');
$email              = get_post('email');
$unit_type          = get_post('unit_type');
$season             = get_post('season');
$no_weeks           = (int)get_post('no_weeks');
$occupancy_years    = (int)get_post('occupancy_years');
$purchase_price     = (float)get_post('purchase_price');
$membership_type    = get_post('membership_type');
$tenure             = get_post('tenure');
$deposit            = (float)get_post('deposit');
$payment_mode       = get_post('payment_mode');
$balance            = (float)get_post('balance');
$hotel_place        = get_post('hotel_place');
$room_no            = get_post('room_no');
$remarks            = get_post('remarks');
$quota_applied      = isset($_POST['quota_applied']) ? 1 : 0;
$client_approval    = isset($_POST['client_approval']) ? 1 : 0;
$manager_approval   = isset($_POST['manager_approval']) ? 1 : 0;
$password           = get_post('Password');
$consultant_name    = get_post('consultant_name');
$manager_name       = get_post('manager_name');

$hashed_password = password_hash($password, PASSWORD_DEFAULT);

// Update SQL
$sql = "UPDATE memberships SET
  entry_date = ?, member_return_date = ?, applicant_name = ?, applicant_title = ?, address = ?,
  country = ?, post_code = ?, phone_home = ?, phone_work = ?, mobile_no = ?, fax = ?, email = ?,
  unit_type = ?, season = ?, no_weeks = ?, occupancy_years = ?, purchase_price = ?,
  membership_type = ?, tenure = ?, deposit = ?, payment_mode = ?, balance = ?,
  hotel_place = ?, room_no = ?, remarks = ?, quota_applied = ?, client_approval = ?,
  manager_approval = ?, password = ?, consultant_name = ?, manager_name = ?
WHERE id = ?";

$stmt = $conn->prepare($sql);

$stmt->bind_param(
    "ssssssssssssssiidssdsdsssiiisssi",
    $entry_date, $member_return_date, $applicant_name, $applicant_title, $address,
    $country, $post_code, $phone_home, $phone_work, $mobile_no, $fax, $email,
    $unit_type, $season, $no_weeks, $occupancy_years, $purchase_price,
    $membership_type, $tenure, $deposit, $payment_mode, $balance,
    $hotel_place, $room_no, $remarks, $quota_applied, $client_approval,
    $manager_approval, $hashed_password, $consultant_name, $manager_name,
    $id
);

if ($stmt->execute()) {
    echo "<h3>✅ Membership updated successfully!</h3>";
} else {
    echo "❌ Error updating record: " . $stmt->error;
}

$stmt->close();
$conn->close();
?>
