<?php
$pageTitle = "Profile";
require_once("includes/header.php");
require_once("../api/config/db.php");

$user_id = intval($user['id']);

$q = $conn->query("
    SELECT u.*, v.village_name, b.name AS block_name, d.name AS district_name
    FROM users u
    LEFT JOIN villages v ON u.village_id = v.id
    LEFT JOIN blocks b ON v.block_id = b.id
    LEFT JOIN districts d ON b.district_id = d.id
    WHERE u.id='$user_id'
    LIMIT 1
");

$data = $q->fetch_assoc();
?>

<div class="profile-card">
    <div class="profile-avatar">
        <?= strtoupper(substr($data['name'], 0, 1)) ?>
    </div>
    <h2><?= htmlspecialchars($data['name']) ?></h2>
    <p><?= htmlspecialchars($data['role']) ?></p>
</div>

<div class="section-card">
    <div class="info-row"><span>Mobile</span><b><?= htmlspecialchars($data['mobile']) ?></b></div>
    <div class="info-row"><span>Father Name</span><b><?= htmlspecialchars($data['father_name'] ?? '-') ?></b></div>
    <div class="info-row"><span>Village</span><b><?= htmlspecialchars($data['village_name'] ?? '-') ?></b></div>
    <div class="info-row"><span>Block</span><b><?= htmlspecialchars($data['block_name'] ?? '-') ?></b></div>
    <div class="info-row"><span>District</span><b><?= htmlspecialchars($data['district_name'] ?? '-') ?></b></div>
    <div class="info-row"><span>Status</span><b><?= htmlspecialchars($data['status']) ?></b></div>

    <a href="logout.php" class="logout-btn">Logout</a>
</div>

<?php require_once("includes/bottom-nav.php"); ?>