<script setup>
import { ref, onMounted } from 'vue'
import { usePageTitle } from '../composables/Core/usePageTitle'
import { useNavigate } from '../composables/Core/useNavigate'
import BackButton from '../Components/Core/BackButton.vue'
import axios from 'axios'

usePageTitle('Property Referrals')
const { navigate } = useNavigate()

const referrals = ref([])
const isLoading = ref(true)

onMounted(async () => {
    await fetchReferrals()
})

const fetchReferrals = async () => {
    isLoading.value = true
    try {
        const response = await axios.post('/admin/properties/referrals')
        referrals.value = response.data.referrals || []
    } catch (error) {
        console.error('Error fetching referrals:', error)
    } finally {
        isLoading.value = false
    }
}
</script>

<template>
    <div class="list-referrals-page pb-5">
        <br><br>
        <div class="tf-container">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <BackButton to="Home" />
                <h4 class="fw_6 mb-0">Referral Tracking</h4>
                <button class="btn btn_primary btn-sm rounded-pill px-3 shadow-sm">
                    <i class="fas fa-plus me-1"></i> New Referral
                </button>
            </div>

            <div v-if="isLoading" class="text-center py-5">
                <i class="fas fa-spinner fa-spin fa-3x text-muted"></i>
                <p class="mt-3">Loading referrals...</p>
            </div>

            <div v-else-if="referrals.length === 0" class="card shadow-sm border-0 rounded-3">
                <div class="card-body text-center py-5">
                    <i class="fas fa-users-cog fa-4x text-muted mb-3"></i>
                    <h5>No Referrals Yet</h5>
                    <p class="text-muted">Track property leads and referrals here.</p>
                </div>
            </div>

            <div v-else class="card shadow-sm border-0 rounded-3 overflow-hidden">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Referral ID</th>
                                <th>Property</th>
                                <th>Referrer</th>
                                <th>Lead Name</th>
                                <th>Status</th>
                                <th>Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="referral in referrals" :key="referral.id">
                                <td><span class="fw-bold">#{{ referral.hashkey?.substring(0, 8) }}</span></td>
                                <td>{{ referral.property?.name }}</td>
                                <td>{{ referral.referrer?.name }}</td>
                                <td>{{ referral.referred_name || referral.referred?.name }}</td>
                                <td>
                                    <span class="badge bg-warning text-dark">{{ referral.status }}</span>
                                </td>
                                <td>{{ new Date(referral.created_at).toLocaleDateString() }}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped>
.tf-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 15px;
}
</style>
