<?php
require '../db.php';
require '../functions.php';

header('Content-Type: application/json');
$villager = requireVillagerToken($pdo);

try {
    $stmt = $pdo->prepare("SELECT v.status, v.rejectreason, v.paymentstatus, v.passactive, v.passstartdate, v.passenddate,
        p.name AS panchayatname
        FROM villager v LEFT JOIN panchayat p ON p.id = v.panchayatid
        WHERE v.id = ? AND v.isdeleted = 0");
    $stmt->execute([$villager['id']]);
    $v = $stmt->fetch();

    if (!$v) {
        jsonOut(false, 'Registration not found');
    }

    jsonOut(true, '', [
        'status' => $v['status'],
        'rejectreason' => $v['rejectreason'],
        'paymentstatus' => $v['paymentstatus'],
        'passactive' => (int)$v['passactive'],
        'passstartdate' => $v['passstartdate'],
        'passenddate' => $v['passenddate'],
        'panchayatname' => $v['panchayatname'],
    ]);
} catch (Exception $e) {
    logDebug('[api_getstatus] ' . $e->getMessage());
    jsonOut(false, 'Something went wrong. Please try again.');
}
