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

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

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

const fetchReports = async () => {
    isLoading.value = true
    try {
        const response = await axios.post('/admin/accounting/reports', {})
        transactions.value = response.data.transactions || []
    } catch (error) {
        console.error('Error fetching reports:', 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-reports-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">Accounting Reports</h4>
                <div class="d-flex gap-2">
                    <button class="btn btn-outline-secondary btn-sm rounded-pill px-3">
                        <i class="fas fa-download me-1"></i> Export PDF
                    </button>
                    <button class="btn btn-primary btn-sm rounded-pill px-3 shadow-sm">
                        <i class="fas fa-filter me-1"></i> Filters
                    </button>
                </div>
            </div>

            <div v-if="isLoading" class="text-center py-5">
                <i class="fas fa-chart-line fa-spin fa-3x text-muted"></i>
                <p class="mt-3">Generating reports...</p>
            </div>

            <div v-else-if="transactions.length === 0" class="card shadow-sm border-0 rounded-3">
                <div class="card-body text-center py-5">
                    <i class="fas fa-file-invoice-dollar fa-4x text-muted mb-3"></i>
                    <h5>No Accounting Data</h5>
                    <p class="text-muted">Financial reports will appear here once transactions are recorded.</p>
                </div>
            </div>

            <div v-else class="card shadow-sm border-0 rounded-3 overflow-hidden">
                <div class="card-header bg-white border-bottom py-3">
                    <h5 class="mb-0 fw_6">Recent Transactions</h5>
                </div>
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Date</th>
                                <th>Description</th>
                                <th>Type</th>
                                <th>Amount</th>
                                <th>Recorded By</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="tx in transactions" :key="tx.id">
                                <td>{{ new Date(tx.transaction_date).toLocaleDateString() }}</td>
                                <td>{{ tx.item }}</td>
                                <td><span class="badge bg-info-subtle text-info">{{ tx.account_type?.name }}</span></td>
                                <td class="fw-bold" :class="tx.amount > 0 ? 'text-success' : 'text-danger'">
                                    {{ formatCurrency(tx.amount) }}
                                </td>
                                <td>{{ tx.creator?.name }}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</template>

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