
<?php
// public/admin/verification_detail.php

require_once __DIR__ . "/../../config/db.php";
require_once __DIR__ . "/../../config/auth.php";

require_admin();

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if (!$id) {
    header("Location: verifications.php");
    exit;
}


// Fetch visit (verification) details from school_visits
$stmt = $pdo->prepare("
    SELECT v.*, 
           s.name AS school_name, s.address, s.category, s.contact_no, s.whatsapp_no,
           g.name AS gp_name, b.name AS block_name, d.name AS district_name,
           u.name AS rep_name, u.email AS rep_email,
           a.name AS verifier_name
    FROM school_visits v
    JOIN schools s ON s.id = v.school_id
    LEFT JOIN gps g ON g.id = s.gp_id
    LEFT JOIN blocks b ON b.id = g.block_id
    LEFT JOIN districts d ON d.id = b.district_id
    JOIN users u ON u.id = v.representative_id
    LEFT JOIN users a ON a.id = v.verification_verified_by
    WHERE v.id = ?
    LIMIT 1
");
$stmt->execute([$id]);
$ver = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$ver) {
    header("Location: verifications.php");
    exit;
}

$page_title = "Verification Detail";
require_once __DIR__ . "/../partials/header.php";
?>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<style>
.detail-container { max-width: 700px; margin: 30px auto; background: #fff; border-radius: 18px; box-shadow: 0 4px 24px rgba(67,97,238,0.08); padding: 32px; }
.detail-header { display: flex; align-items: center; gap: 16px; margin-bottom: 24px; }
.detail-header .icon { font-size: 2.5rem; color: #4361ee; }
.detail-title { font-size: 1.5rem; font-weight: 700; }
.detail-status { margin-left: auto; }
.status-badge { padding: 6px 16px; border-radius: 30px; font-size: 1rem; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; }
.badge-pending { background: rgba(255,183,3,0.1); color: #ffb703; }
.badge-approved { background: rgba(6,214,160,0.1); color: #06d6a0; }
.badge-rejected { background: rgba(239,71,111,0.1); color: #ef476f; }
.detail-section { margin-bottom: 28px; }
.section-title { font-size: 1.1rem; font-weight: 600; color: #4361ee; margin-bottom: 10px; }
.detail-list { list-style: none; padding: 0; margin: 0; }
.detail-list li { margin-bottom: 8px; font-size: 1rem; }
.detail-label { color: #6c757d; font-weight: 500; margin-right: 8px; }
.remarks-box { background: #f8f9fa; border-radius: 10px; padding: 16px; margin-top: 10px; color: #073b4c; }
.action-buttons { display: flex; gap: 12px; margin-top: 24px; }
.btn-action { padding: 10px 24px; border: none; border-radius: 10px; font-weight: 600; font-size: 1rem; cursor: pointer; transition: all 0.2s; }
.btn-approve { background: #06d6a0; color: #fff; }
.btn-approve:hover { background: #05b588; }
.btn-reject { background: #ef476f; color: #fff; }
.btn-reject:hover { background: #d43f62; }
</style>

<div class="detail-container">
    <div class="detail-header">
        <span class="icon"><i class="bi bi-clipboard-check"></i></span>
        <span class="detail-title">Verification Detail</span>
        <span class="detail-status">
            <?php if ($ver['verification_status'] === 'PENDING'): ?>
                <span class="status-badge badge-pending"><i class="bi bi-hourglass-split"></i> Pending</span>
            <?php elseif ($ver['verification_status'] === 'APPROVED'): ?>
                <span class="status-badge badge-approved"><i class="bi bi-check-circle"></i> Approved</span>
            <?php else: ?>
                <span class="status-badge badge-rejected"><i class="bi bi-x-circle"></i> Rejected</span>
            <?php endif; ?>
        </span>
    </div>

    <div class="detail-section">
        <div class="section-title"><i class="bi bi-building"></i> School Info</div>
        <ul class="detail-list">
            <li><span class="detail-label">Name:</span> <?= htmlspecialchars($ver['school_name'] ?? '') ?></li>
            <li><span class="detail-label">Address:</span> <?= htmlspecialchars($ver['address'] ?? '') ?></li>
            <li><span class="detail-label">Category:</span> <?= htmlspecialchars($ver['category'] ?? '') ?></li>
            <li><span class="detail-label">Contact:</span> <?= htmlspecialchars($ver['contact_no'] ?? '') ?></li>
            <li><span class="detail-label">WhatsApp:</span> <?= htmlspecialchars($ver['whatsapp_no'] ?? '') ?></li>
            <li><span class="detail-label">GP:</span> <?= htmlspecialchars($ver['gp_name'] ?? '') ?></li>
            <li><span class="detail-label">Block:</span> <?= htmlspecialchars($ver['block_name'] ?? '') ?></li>
            <li><span class="detail-label">District:</span> <?= htmlspecialchars($ver['district_name'] ?? '') ?></li>
        </ul>
    </div>

    <div class="detail-section">
        <div class="section-title"><i class="bi bi-person"></i> Representative</div>
        <ul class="detail-list">
            <li><span class="detail-label">Name:</span> <?= htmlspecialchars($ver['rep_name'] ?? '') ?></li>
            <li><span class="detail-label">Email:</span> <?= htmlspecialchars($ver['rep_email'] ?? '') ?></li>
        </ul>
    </div>

    <div class="detail-section">
        <div class="section-title"><i class="bi bi-info-circle"></i> Verification Info</div>
        <ul class="detail-list">
            <li><span class="detail-label">Visited At:</span> <?= htmlspecialchars($ver['visited_at']) ?></li>
            <li><span class="detail-label">Verification Status:</span> <?= htmlspecialchars($ver['verification_status']) ?></li>
            <?php if ($ver['verification_verified_at']): ?>
                <li><span class="detail-label">Verified At:</span> <?= htmlspecialchars($ver['verification_verified_at']) ?></li>
            <?php endif; ?>
            <?php if ($ver['verifier_name']): ?>
                <li><span class="detail-label">Verified By:</span> <?= htmlspecialchars($ver['verifier_name']) ?></li>
            <?php endif; ?>
        </ul>
        <?php if ($ver['verification_admin_remark']): ?>
            <div class="remarks-box">
                <strong>Admin Remark:</strong><br>
                <?= nl2br(htmlspecialchars($ver['verification_admin_remark'])) ?>
            </div>
        <?php endif; ?>
    </div>

    </ul>
    </div>

    <div class="detail-section">
        <div class="section-title"><i class="bi bi-geo-alt"></i> Visit Details</div>
        <ul class="detail-list">
            <li><span class="detail-label">Permission Status:</span> <?= htmlspecialchars($ver['permission_status'] ?? '') ?></li>
            <li><span class="detail-label">Interest Status:</span> <?= htmlspecialchars($ver['interest_status'] ?? '') ?></li>
            <li><span class="detail-label">Authority Person:</span> <?= htmlspecialchars($ver['authority_person'] ?? '') ?></li>
            <li><span class="detail-label">Phone:</span> <?= htmlspecialchars($ver['phone'] ?? '') ?></li>
            <li><span class="detail-label">Optional Phone:</span> <?= htmlspecialchars($ver['optional_phone'] ?? '') ?></li>
            <li><span class="detail-label">WhatsApp:</span> <?= htmlspecialchars($ver['whatsapp'] ?? '') ?></li>
            <li><span class="detail-label">Letter Received:</span> <?= ($ver['letter_received'] ?? 0) ? 'Yes' : 'No' ?></li>
            <li><span class="detail-label">Not Allowed Reason:</span> <?= htmlspecialchars($ver['not_allowed_reason'] ?? '') ?></li>
            <li><span class="detail-label">GPS Captured At:</span> <?= htmlspecialchars($ver['gps_captured_at'] ?? '') ?></li>
            <li><span class="detail-label">Latitude:</span> <?= htmlspecialchars($ver['latitude'] ?? '') ?></li>
            <li><span class="detail-label">Longitude:</span> <?= htmlspecialchars($ver['longitude'] ?? '') ?></li>
            <?php if (!empty($ver['latitude']) && !empty($ver['longitude'])): ?>
                <li><span class="detail-label">Map:</span> <a href="https://maps.google.com/?q=<?= urlencode($ver['latitude']) ?>,<?= urlencode($ver['longitude']) ?>" target="_blank">View on Google Maps</a></li>
            <?php endif; ?>
            <li><span class="detail-label">Self Declaration:</span> <?= ($ver['self_declaration'] ?? 0) ? 'Yes' : 'No' ?></li>
            <li><span class="detail-label">Created At:</span> <?= htmlspecialchars($ver['created_at'] ?? '') ?></li>
        </ul>
        <?php if (!empty($ver['photo1_path'])): ?>
            <div style="margin-top:16px;">
                <strong>Visit Photo:</strong><br>
                <?php
                $photoPath = $ver['photo1_path'];
                // If already starts with /school-crm, use as is
                if (strpos($photoPath, '/school-crm') === 0) {
                    $imgSrc = $photoPath;
                // If starts with /uploads or /public, prepend /school-crm
                } elseif (strpos($photoPath, '/uploads') === 0 || strpos($photoPath, '/public') === 0) {
                    $imgSrc = '/school-crm' . $photoPath;
                // If it's just a filename, prepend full path
                } elseif (strpos($photoPath, '/') === false) {
                    $imgSrc = '/school-crm/public/uploads/visits/' . $photoPath;
                } else {
                    $imgSrc = $photoPath;
                }
                ?>
                <img src="<?= htmlspecialchars($imgSrc) ?>" alt="Visit Photo" style="max-width:320px;max-height:320px;border-radius:10px;border:1px solid #e9ecef;">
            </div>
        <?php endif; ?>
    </div>

    <?php if ($ver['verification_status'] === 'PENDING'): ?>
    <div class="action-buttons">
        <form method="post" action="process_verification.php" style="display:inline;">
            <input type="hidden" name="visit_id" value="<?= $ver['id'] ?>">
            <input type="hidden" name="action" value="approve">
            <input type="hidden" name="csrf_token" value="<?= csrf_token() ?>">
            <button type="submit" class="btn-action btn-approve"><i class="bi bi-check-lg"></i> Approve</button>
        </form>
        <form method="post" action="process_verification.php" style="display:inline;">
            <input type="hidden" name="visit_id" value="<?= $ver['id'] ?>">
            <input type="hidden" name="action" value="reject">
            <input type="hidden" name="csrf_token" value="<?= csrf_token() ?>">
            <button type="submit" class="btn-action btn-reject"><i class="bi bi-x-lg"></i> Reject</button>
        </form>
    </div>
    <?php endif; ?>
</div>

<?php require_once __DIR__ . "/../partials/footer.php"; ?>
