@extends('layouts.app')

@section('content')
<div class="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 py-8 px-4 md:px-8">
    <div class="max-w-7xl mx-auto">
        <div class="mb-8">
            <div class="flex items-center justify-between mb-6">
                <div>
                    <h1 class="text-3xl font-bold text-gray-900 mb-2">📉 {{ \App\Helpers\TranslationHelper::transFromDatabase('Expense Analysis', __('Expense Analysis')) }}</h1>
                    <p class="text-gray-600">{{ \App\Helpers\TranslationHelper::transFromDatabase('Distribution of expenses by account type', __('Distribution of expenses by account type')) }}</p>
                </div>
                <a href="{{ route('reports.cost-center') }}" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-blue-600 to-blue-700 text-white font-semibold rounded-lg hover:from-blue-700 hover:to-blue-800 transition-all duration-200 shadow-lg">
                    <i class="fas fa-arrow-right ml-2"></i>
                    <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Return', __('Return')) }}</span>
                </a>
            </div>
        </div>

        @if($report->isEmpty())
        <div class="bg-white rounded-xl shadow-lg p-12 text-center border border-gray-200">
            <i class="fas fa-inbox text-gray-400 text-5xl mb-4"></i>
            <p class="text-gray-600 font-medium text-lg">{{ \App\Helpers\TranslationHelper::transFromDatabase('No data to display', __('No data to display')) }}</p>
        </div>
        @else
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-8">
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200 lg:col-span-2">
                <h3 class="text-lg font-semibold text-gray-900 mb-6">{{ \App\Helpers\TranslationHelper::transFromDatabase('Distribution of Expenses', __('Distribution of Expenses')) }}</h3>
                <div id="expenseChart" class="w-full h-96"></div>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <h3 class="text-lg font-semibold text-gray-900 mb-4">{{ \App\Helpers\TranslationHelper::transFromDatabase('Summary of Expenses', __('Summary of Expenses')) }}</h3>
                <div class="space-y-3">
                    @foreach($report as $item)
                    <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                        <div>
                            <p class="text-sm font-medium text-gray-900">{{ $item['account_type'] ?? \App\Helpers\TranslationHelper::transFromDatabase('Other', __('Other')) }}</p>
                            <p class="text-xs text-gray-500">{{ $item['percentage'] }}%</p>
                        </div>
                        <p class="font-bold text-gray-900">{{ number_format($item['amount'], 2) }}</p>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                        <tr>
                            <th class="px-6 py-4 text-right font-semibold text-sm">#</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Account Type', __('Account Type')) }}</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Amount', __('Amount')) }}</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Percentage %', __('Percentage %')) }}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @foreach($report as $item)
                        <tr class="hover:bg-blue-50 transition-colors duration-150">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>
                            <td class="px-6 py-4">
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-semibold bg-blue-100 text-blue-800">
                                    {{ $item['account_type'] ?? \App\Helpers\TranslationHelper::transFromDatabase('Not Specified', __('Not Specified')) }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-center font-bold text-gray-900">{{ number_format($item['amount'], 2) }}</td>
                            <td class="px-6 py-4 text-center">
                                <div class="w-full bg-gray-200 rounded-full h-2">
                                    <div class="bg-blue-600 h-2 rounded-full" style="width: {{ $item['percentage'] }}%"></div>
                                </div>
                                <p class="text-sm font-semibold text-gray-700 mt-2">{{ $item['percentage'] }}%</p>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @endif
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
@if(!$report->isEmpty())
const ctx = document.getElementById('expenseChart').getContext('2d');
const chartData = {
    labels: @json($report->pluck('account_type')),
    datasets: [{
        label: '{{ \App\Helpers\TranslationHelper::transFromDatabase('Expenses', __('Expenses')) }}',
        data: @json($report->pluck('amount')),
        backgroundColor: [
            '#FF6384',
            '#36A2EB',
            '#FFCE56',
            '#4BC0C0',
            '#9966FF',
            '#FF9F40',
            '#FF6384',
            '#C9CBCF',
        ],
        borderColor: '#fff',
        borderWidth: 2,
    }]
};

new Chart(ctx, {
    type: 'doughnut',
    data: chartData,
    options: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
            legend: {
                position: 'bottom',
            }
        }
    }
});
@endif
</script>
@endsection
