<?php
// -------------------- DB Connection --------------------
$host = "localhost";
$username = "job_portal";
$password = "job_portal";
$dbname = "job_portal";

$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
    die("? Connection failed: " . $conn->connect_error);
}

// -------------------- Get ID from URL --------------------
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
    die("? Invalid ID.");
}

// -------------------- Fetch Membership Data --------------------
$sql = "SELECT * FROM memberships WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    die("? No membership found.");
}

$row = $result->fetch_assoc();
$stmt->close();
$conn->close();

// Helper Function
function value($key, $default = '') {
    global $row;
    return isset($row[$key]) ? htmlspecialchars($row[$key]) : $default;
}
?>
<!DOCTYPE html>
<html>
<head>
  <title>Edit Membership</title>
  <style>
    .form-container { max-width: 960px; margin: auto; padding: 25px; border: 1px solid #ccc; font-family: Arial; }
    .form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; }
    .form-group { display: flex; flex-direction: column; }
    .form-group textarea { resize: vertical; }
    .checkbox { flex-direction: row; align-items: center; }
  </style>
</head>
<body>
  <div class="form-container">
    <h2>Edit Membership Details (ID: <?= $id ?>)</h2>
    <form action="update_membership.php?id=<?= $id ?>" method="POST">
      <div class="form-grid">

        <!-- Row 1 -->
        <div class="form-group"><label>Entry Date</label><input type="date" name="entry_date" value="<?= value('entry_date') ?>"></div>
        <div class="form-group"><label>Return Date</label><input type="date" name="member_return_date" value="<?= value('member_return_date') ?>"></div>

        <!-- Row 2 -->
        <div class="form-group"><label>Applicant Name</label><input type="text" name="applicant_name" value="<?= value('applicant_name') ?>"></div>
        <div class="form-group"><label>Title</label>
          <select name="applicant_title">
            <option <?= value('applicant_title') == 'Mr' ? 'selected' : '' ?>>Mr</option>
            <option <?= value('applicant_title') == 'Ms' ? 'selected' : '' ?>>Ms</option>
            <option <?= value('applicant_title') == 'Mrs' ? 'selected' : '' ?>>Mrs</option>
          </select>
        </div>

        <!-- Contact Info -->
        <div class="form-group"><label>Address</label><textarea name="address"><?= value('address') ?></textarea></div>
        <div class="form-group"><label>Country</label><input type="text" name="country" value="<?= value('country') ?>"></div>
        <div class="form-group"><label>Post Code</label><input type="text" name="post_code" value="<?= value('post_code') ?>"></div>
        <div class="form-group"><label>Phone (Home)</label><input type="text" name="phone_home" value="<?= value('phone_home') ?>"></div>
        <div class="form-group"><label>Phone (Work)</label><input type="text" name="phone_work" value="<?= value('phone_work') ?>"></div>
        <div class="form-group"><label>Mobile No</label><input type="text" name="mobile_no" value="<?= value('mobile_no') ?>"></div>
        <div class="form-group"><label>Fax</label><input type="text" name="fax" value="<?= value('fax') ?>"></div>
        <div class="form-group"><label>Email</label><input type="email" name="email" value="<?= value('email') ?>"></div>

        <!-- Membership Info -->
        <div class="form-group"><label>Unit Type</label><input type="text" name="unit_type" value="<?= value('unit_type') ?>"></div>
        <div class="form-group"><label>Season</label><input type="text" name="season" value="<?= value('season') ?>"></div>
        <div class="form-group"><label>No. of Weeks</label><input type="number" name="no_weeks" value="<?= value('no_weeks') ?>"></div>
        <div class="form-group"><label>Occupancy Years</label><input type="number" name="occupancy_years" value="<?= value('occupancy_years') ?>"></div>
        <div class="form-group"><label>Purchase Price</label><input type="number" step="0.01" name="purchase_price" value="<?= value('purchase_price') ?>"></div>
        <div class="form-group"><label>Membership Type</label><input type="text" name="membership_type" value="<?= value('membership_type') ?>"></div>
        <div class="form-group"><label>Tenure</label><input type="text" name="tenure" value="<?= value('tenure') ?>"></div>
        <div class="form-group"><label>Deposit</label><input type="number" step="0.01" name="deposit" value="<?= value('deposit') ?>"></div>

        <div class="form-group"><label>Payment Mode</label>
          <select name="payment_mode">
            <option <?= value('payment_mode') == 'Credit Card' ? 'selected' : '' ?>>Credit Card</option>
            <option <?= value('payment_mode') == 'Cash' ? 'selected' : '' ?>>Cash</option>
            <option <?= value('payment_mode') == 'Debit Card' ? 'selected' : '' ?>>Debit Card</option>
          </select>
        </div>

        <div class="form-group"><label>Balance</label><input type="number" step="0.01" name="balance" value="<?= value('balance') ?>"></div>
        <div class="form-group"><label>Hotel Place</label><input type="text" name="hotel_place" value="<?= value('hotel_place') ?>"></div>
        <div class="form-group"><label>Room No</label><input type="text" name="room_no" value="<?= value('room_no') ?>"></div>
        <div class="form-group"><label>Remarks</label><textarea name="remarks"><?= value('remarks') ?></textarea></div>

        <!-- Checkboxes -->
        <div class="form-group checkbox"><label><input type="checkbox" name="quota_applied" value="1" <?= $row['quota_applied'] ? 'checked' : '' ?>> Quota Applied</label></div>
        <div class="form-group checkbox"><label><input type="checkbox" name="client_approval" value="1" <?= $row['client_approval'] ? 'checked' : '' ?>> Client Approval</label></div>
        <div class="form-group checkbox"><label><input type="checkbox" name="manager_approval" value="1" <?= $row['manager_approval'] ? 'checked' : '' ?>> Manager Approval</label></div>

        <!-- Password -->
        <div class="form-group"><label>Password (leave blank to keep unchanged)</label><input type="text" name="Password" value=""></div>
        <div class="form-group"><label>Consultant Name</label><input type="text" name="consultant_name" value="<?= value('consultant_name') ?>"></div>
        <div class="form-group"><label>Manager Name</label><input type="text" name="manager_name" value="<?= value('manager_name') ?>"></div>

      </div>

      <div style="text-align:center; margin-top: 20px;">
        <button type="submit">Update Membership</button>
      </div>
    </form>
  </div>
</body>
</html>
