<?php
include 'includes/db.php';

$uid = $_POST['uid'] ?? '';
$member_id = $_POST['member_id'] ?? 0;

if (!$uid || !$member_id) {
    header("Location: members_active.php?error=missing_data");
    exit;
}

// Check if UID is already in use by another member
$check = mysqli_query($conn, "SELECT id FROM members WHERE nfc_uid = '$uid' AND id != $member_id");
if (mysqli_num_rows($check) > 0) {
    header("Location: members_active.php?error=duplicate_uid");
    exit;
}

// Update the member with the new UID
mysqli_query($conn, "UPDATE members SET nfc_uid = '$uid' WHERE id = $member_id");

header("Location: members_active.php?success=card_assigned");
exit;
