<template>
  <div class="py-6">
    <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
        <h3 class="text-2xl font-bold text-gray-900 mb-6">User Verification Queue</h3>
        
        <div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">User</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Role</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Documents</th>
                            <th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        <tr v-for="user in users" :key="user.id">
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="flex items-center">
                                    <div class="text-sm font-medium text-gray-900">{{ user.name }}</div>
                                </div>
                                <div class="text-sm text-gray-500">{{ user.email }}</div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full" 
                                    :class="{'bg-blue-100 text-blue-800': user.role === 'worker', 'bg-green-100 text-green-800': user.role === 'client'}">
                                    {{ user.role.charAt(0).toUpperCase() + user.role.slice(1) }}
                                </span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full"
                                    :class="{'bg-yellow-100 text-yellow-800': user.status === 'pending', 'bg-green-100 text-green-800': user.status === 'active', 'bg-red-100 text-red-800': user.status === 'suspended'}">
                                    {{ user.status.charAt(0).toUpperCase() + user.status.slice(1) }}
                                </span>
                            </td>
                            <td class="px-6 py-4">
                                <div v-if="user.documents.length > 0" class="space-y-2">
                                    <div v-for="doc in user.documents" :key="doc.id" class="text-xs flex items-center justify-between">
                                        <a :href="'/storage/' + doc.file_path" target="_blank" class="text-indigo-600 hover:text-indigo-900 underline truncate max-w-[150px] inline-block" :title="doc.type">
                                            {{ doc.type.replace('_', ' ') }}
                                        </a>
                                        <div class="flex items-center space-x-1 ml-2">
                                            <span :class="{'text-yellow-600': doc.status === 'pending', 'text-green-600': doc.status === 'approved', 'text-red-600': doc.status === 'rejected'}">
                                                {{ doc.status }}
                                            </span>
                                            <div v-if="doc.status === 'pending'" class="flex space-x-1">
                                                <button @click="verifyDocument(doc.id, 'approved')" class="text-green-600 hover:text-green-900" title="Approve">✓</button>
                                                <button @click="verifyDocument(doc.id, 'rejected')" class="text-red-600 hover:text-red-900" title="Reject">✗</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <span v-else class="text-xs text-gray-500">No docs</span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                                <button v-if="user.status !== 'active'" @click="verifyUser(user.id, 'active')" class="text-green-600 hover:text-green-900 mr-4">Activate</button>
                                <button v-if="user.status !== 'suspended'" @click="verifyUser(user.id, 'suspended')" class="text-red-600 hover:text-red-900">Suspend</button>
                            </td>
                        </tr>
                        <tr v-if="users.length === 0">
                            <td colspan="5" class="px-6 py-4 text-center text-sm text-gray-500">No users found.</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import axios from 'axios';

const users = ref([]);

const fetchUsers = async () => {
    try {
        const response = await axios.get('/api/admin/users');
        users.value = response.data;
    } catch (error) {
        console.error('Failed to fetch users', error);
    }
};

const verifyUser = async (userId, status) => {
    try {
        await axios.post(`/api/admin/users/${userId}/verify`, { status });
        fetchUsers();
    } catch (error) {
        alert('Action failed');
    }
};

const verifyDocument = async (docId, status) => {
    let reason = null;
    if (status === 'rejected') {
        reason = prompt('Enter rejection reason:');
        if (!reason) return;
    }

    try {
        await axios.post(`/api/admin/documents/${docId}/verify`, { status, rejection_reason: reason });
        fetchUsers();
    } catch (error) {
        alert('Action failed');
    }
};

onMounted(() => {
    fetchUsers();
});
</script>
