<?php
require_once __DIR__ . '/../includes/functions.php';
requireLogin();

$cardCode = trim($_POST['card_code'] ?? '');
if (!$cardCode) jsonResponse(['success'=>false,'error'=>'No card code provided.']);

$member = getMemberByCard($cardCode);
if (!$member) jsonResponse(['success'=>false,'error'=>'Card not found.']);

$db         = getDB();
$newStatus  = ($member['status'] === 'active') ? 'inactive' : 'active';

$db->prepare('UPDATE members SET status = ? WHERE card_code = ?')
   ->execute([$newStatus, $cardCode]);

jsonResponse([
    'success'    => true,
    'new_status' => $newStatus,
    'is_active'  => $newStatus === 'active',
    'message'    => 'Card ' . ($newStatus === 'active' ? 'activated' : 'deactivated') . ' successfully.',
]);
