<?php
// admin/verify-users.php
session_start();

// Check if admin is logged in
if (!isset($_SESSION['admin_id'])) {
    header("Location: login.php");
    exit();
}

// Include database
require_once "../config/db.php";

// Handle verification actions
if (isset($_POST['action'])) {
    $proof_id = mysqli_real_escape_string($conn, $_POST['proof_id']);
    $action = mysqli_real_escape_string($conn, $_POST['action']);
    
    if ($action == 'approve') {
        // Get user_id from proof
        $user_query = mysqli_query($conn, "SELECT user_id FROM user_id_proofs WHERE id = '$proof_id'");
        $user_data = mysqli_fetch_assoc($user_query);
        $user_id = $user_data['user_id'];
        
        // Update proof status
        mysqli_query($conn, "UPDATE user_id_proofs SET verification_status = 'verified', verified_at = NOW() WHERE id = '$proof_id'");
        
        // Update user status
        mysqli_query($conn, "UPDATE users SET id_verified = 1, verification_level = 'full' WHERE id = '$user_id'");
        
        $_SESSION['success'] = "User verified successfully!";
    } 
    elseif ($action == 'reject') {
        $reason = mysqli_real_escape_string($conn, $_POST['rejection_reason']);
        mysqli_query($conn, "UPDATE user_id_proofs SET verification_status = 'rejected', rejection_reason = '$reason' WHERE id = '$proof_id'");
        $_SESSION['success'] = "User verification rejected!";
    }
    
    header("Location: verify-users.php");
    exit();
}

// Get pending verifications
$pending_query = mysqli_query($conn, 
    "SELECT p.*, u.name, u.email, u.phone 
     FROM user_id_proofs p
     JOIN users u ON p.user_id = u.id
     WHERE p.verification_status = 'pending'
     ORDER BY p.created_at DESC"
);

// Get verified/rejected history
$history_query = mysqli_query($conn, 
    "SELECT p.*, u.name, u.email 
     FROM user_id_proofs p
     JOIN users u ON p.user_id = u.id
     WHERE p.verification_status IN ('verified', 'rejected')
     ORDER BY p.verified_at DESC"
);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Verify Users - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
    <?php include "includes/sidebar.php"; ?>
    
    <div class="main-content" style="margin-left: 220px; padding: 20px;">
        <h2><i class="fas fa-id-card me-2"></i>Verify Users ID</h2>
        
        <?php if(isset($_SESSION['success'])): ?>
            <div class="alert alert-success alert-dismissible fade show mt-3">
                <?php echo $_SESSION['success']; ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
            <?php unset($_SESSION['success']); ?>
        <?php endif; ?>
        
        <!-- Pending Verifications -->
        <div class="card mt-4">
            <div class="card-header bg-warning">
                <h4 class="mb-0"><i class="fas fa-clock me-2"></i>Pending Verifications</h4>
            </div>
            <div class="card-body">
                <?php if(mysqli_num_rows($pending_query) > 0): ?>
                    <?php while($row = mysqli_fetch_assoc($pending_query)): ?>
                    <div class="card mb-3">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-8">
                                    <h5><?php echo htmlspecialchars($row['full_name']); ?></h5>
                                    <p><strong>User:</strong> <?php echo htmlspecialchars($row['name']); ?> (<?php echo htmlspecialchars($row['email']); ?>)</p>
                                    <p><strong>Document:</strong> <?php echo ucfirst(str_replace('_', ' ', $row['proof_type'])); ?></p>
                                    <p><strong>Document No:</strong> <?php echo htmlspecialchars($row['proof_number']); ?></p>
                                    
                                    <!-- Display Images -->
                                    <div class="mt-3">
                                        <p><strong>Front Side:</strong></p>
                                        <img src="../assets/images/id_proofs/<?php echo $row['front_image']; ?>" 
                                             style="max-width: 300px; border: 1px solid #ddd; border-radius: 5px;">
                                             
                                        <?php if(!empty($row['back_image'])): ?>
                                        <p class="mt-3"><strong>Back Side:</strong></p>
                                        <img src="../assets/images/id_proofs/<?php echo $row['back_image']; ?>" 
                                             style="max-width: 300px; border: 1px solid #ddd; border-radius: 5px;">
                                        <?php endif; ?>
                                    </div>
                                </div>
                                
                                <div class="col-md-4">
                                    <div class="card">
                                        <div class="card-body">
                                            <h6>Actions</h6>
                                            
                                            <!-- Approve Button -->
                                            <form method="POST" class="mb-3">
                                                <input type="hidden" name="proof_id" value="<?php echo $row['id']; ?>">
                                                <input type="hidden" name="action" value="approve">
                                                <button type="submit" class="btn btn-success w-100">
                                                    <i class="fas fa-check me-1"></i> Approve
                                                </button>
                                            </form>
                                            
                                            <!-- Reject Button -->
                                            <form method="POST">
                                                <input type="hidden" name="proof_id" value="<?php echo $row['id']; ?>">
                                                <input type="hidden" name="action" value="reject">
                                                <div class="mb-2">
                                                    <label>Rejection Reason:</label>
                                                    <textarea name="rejection_reason" class="form-control" rows="2" required></textarea>
                                                </div>
                                                <button type="submit" class="btn btn-danger w-100">
                                                    <i class="fas fa-times me-1"></i> Reject
                                                </button>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endwhile; ?>
                <?php else: ?>
                    <div class="text-center py-4">
                        <i class="fas fa-check-circle fa-3x text-success mb-3"></i>
                        <h4>No pending verifications!</h4>
                        <p class="text-muted">All users are verified.</p>
                    </div>
                <?php endif; ?>
            </div>
        </div>
        
        <!-- Verification History -->
        <div class="card mt-4">
            <div class="card-header bg-secondary text-white">
                <h4 class="mb-0"><i class="fas fa-history me-2"></i>Verification History</h4>
            </div>
            <div class="card-body">
                <?php if(mysqli_num_rows($history_query) > 0): ?>
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>User</th>
                                <th>Document</th>
                                <th>Status</th>
                                <th>Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php while($history = mysqli_fetch_assoc($history_query)): ?>
                            <tr>
                                <td>
                                    <strong><?php echo htmlspecialchars($history['name']); ?></strong><br>
                                    <small><?php echo htmlspecialchars($history['email']); ?></small>
                                </td>
                                <td><?php echo ucfirst(str_replace('_', ' ', $history['proof_type'])); ?></td>
                                <td>
                                    <?php if($history['verification_status'] == 'verified'): ?>
                                        <span class="badge bg-success">Verified</span>
                                    <?php else: ?>
                                        <span class="badge bg-danger">Rejected</span>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo $history['verified_at'] ? date('d/m/Y', strtotime($history['verified_at'])) : 'N/A'; ?></td>
                            </tr>
                            <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
                <?php else: ?>
                    <p class="text-muted text-center py-3">No verification history yet.</p>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>