<?php
// public/admin/verify_visit.php
// Admin page to verify (approve/reject) visit details

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

require_admin();

$visit_id = isset($_GET['id']) ? trim($_GET['id']) : '';
$action = isset($_POST['action']) ? $_POST['action'] : '';
$remark = isset($_POST['admin_remark']) ? trim($_POST['admin_remark']) : '';

$error = '';
$success = '';
$visit = null;

if ($visit_id !== '') {
    // Fetch visit details
    $st = $pdo->prepare('
        SELECT sv.*, s.name AS school_name, s.address, s.category, 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
        FROM school_visits sv
        JOIN schools s ON s.id = sv.school_id
        JOIN gps g ON g.id = s.gp_id
        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 = sv.representative_id
        WHERE sv.id = ?
    ');
    $st->execute([$visit_id]);
    $visit = $st->fetch(PDO::FETCH_ASSOC);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && $visit) {
    if ($action === 'approve' || $action === 'reject') {
        $status = $action === 'approve' ? 'APPROVED' : 'REJECTED';
        $st = $pdo->prepare('UPDATE school_verification SET status = ?, admin_remark = ?, verified_at = NOW() WHERE school_id = ? AND representative_id = ? AND status = "PENDING"');
        $st->execute([$status, $remark, $visit['school_id'], $visit['representative_id']]);
        $success = "Verification status updated to $status.";
        // Optionally, you can also update a status in school_visits if needed
    } else {
        $error = 'Invalid action.';
    }
}

$page_title = 'Verify Visit Details';
require_once __DIR__ . '/../partials/header.php';
?>

<div class="container mt-4">
    <h2>Verify Visit Details</h2>
    <?php if ($error): ?>
        <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
    <?php elseif ($success): ?>
        <div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
    <?php endif; ?>

    <?php if ($visit): ?>
        <div class="card mb-4">
            <div class="card-header bg-primary text-white">
                Visit ID: <?= htmlspecialchars($visit['id']) ?> | School: <?= htmlspecialchars($visit['school_name']) ?>
            </div>
            <div class="card-body">
                <p><strong>School:</strong> <?= htmlspecialchars($visit['school_name']) ?> (<?= htmlspecialchars($visit['category']) ?>)</p>
                <p><strong>Address:</strong> <?= htmlspecialchars($visit['address']) ?></p>
                <p><strong>Location:</strong> <?= htmlspecialchars($visit['gp_name']) ?>, <?= htmlspecialchars($visit['block_name']) ?>, <?= htmlspecialchars($visit['district_name']) ?></p>
                <p><strong>Representative:</strong> <?= htmlspecialchars($visit['rep_name']) ?> (<?= htmlspecialchars($visit['rep_email']) ?>)</p>
                <p><strong>Visit Date:</strong> <?= htmlspecialchars($visit['visited_at']) ?></p>
                <p><strong>Permission Status:</strong> <?= htmlspecialchars($visit['permission_status']) ?></p>
                <p><strong>Interest Status:</strong> <?= htmlspecialchars($visit['interest_status']) ?></p>
                <p><strong>Authority Person:</strong> <?= htmlspecialchars($visit['authority_person']) ?></p>
                <p><strong>Letter Received:</strong> <?= $visit['letter_received'] ? 'Yes' : 'No' ?></p>
                <p><strong>Self Declaration:</strong> <?= $visit['self_declaration'] ? 'Yes' : 'No' ?></p>
                <p><strong>Photo:</strong><br>
                    <?php if ($visit['photo1_path']): ?>
                        <img src="<?= htmlspecialchars($visit['photo1_path']) ?>" alt="Visit Photo" style="max-width:300px;">
                    <?php endif; ?>
                </p>
                <form method="post">
                    <div class="mb-3">
                        <label for="admin_remark" class="form-label">Admin Remark (optional):</label>
                        <textarea name="admin_remark" id="admin_remark" class="form-control" rows="2"></textarea>
                    </div>
                    <button type="submit" name="action" value="approve" class="btn btn-success">Approve</button>
                    <button type="submit" name="action" value="reject" class="btn btn-danger ms-2">Reject</button>
                </form>
            </div>
        </div>
    <?php else: ?>
        <div class="alert alert-warning">Visit not found.</div>
    <?php endif; ?>
</div>

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