<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 Listings')
const { navigate } = useNavigate()

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

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

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

const formatCurrency = (amount) => {
  return new Intl.NumberFormat('en-PH', { 
    style: 'currency', 
    currency: 'PHP' 
  }).format(amount)
}
</script>

<template>
    <div class="list-properties-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">Property Management</h4>
                <button class="btn btn-primary btn-sm rounded-pill px-3 shadow-sm">
                    <i class="fas fa-plus me-1"></i> Add Property
                </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 properties...</p>
            </div>

            <div v-else-if="properties.length === 0" class="card shadow-sm border-0 rounded-3">
                <div class="card-body text-center py-5">
                    <i class="fas fa-home fa-4x text-muted mb-3"></i>
                    <h5>No Properties Found</h5>
                    <p class="text-muted">Register your real estate listings here.</p>
                </div>
            </div>

            <div v-else class="row">
                <div v-for="property in properties" :key="property.id" class="col-md-6 col-lg-4 mb-4">
                    <div class="property-card card h-100 shadow-sm border-0 rounded-3">
                        <div class="card-body">
                            <h5 class="fw_6">{{ property.name }}</h5>
                            <p class="text-muted small mb-2"><i class="fas fa-map-marker-alt me-1"></i> {{ property.location }}</p>
                            <div class="d-flex justify-content-between align-items-center mt-3">
                                <span class="fw_7 text-primary">{{ formatCurrency(property.price) }}</span>
                                <span class="badge bg-success-subtle text-success">{{ property.status }}</span>
                            </div>
                        </div>
                        <div class="card-footer bg-transparent border-0 pt-0 pb-3">
                            <button class="btn btn-sm btn-outline-primary w-100 rounded-pill">View Details</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped>
.tf-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 15px;
}
.property-card {
    transition: transform 0.2s;
}
.property-card:hover {
    transform: translateY(-5px);
}
</style>
