@extends('layouts.admin')

@section('title', 'Trial Balance')

@section('page-title', 'Trial Balance')

@section('header-actions')
    <a href="{{ route('admin.reports.trial-balance.export', request()->all()) }}" class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-blue-600 transition-colors flex items-center gap-2">
        <span class="material-symbols-outlined text-lg">download</span>
        <span>Export CSV</span>
    </a>
@endsection

@section('content')
<div class="max-w-7xl mx-auto">
    <div class="bg-white rounded-xl shadow-sm border border-[#e5e7eb] p-6 mb-6">
        <form method="GET" action="{{ route('admin.reports.trial-balance') }}" class="grid grid-cols-1 md:grid-cols-3 gap-4">
            <div>
                <label class="block text-sm font-medium text-[#111418] text-gray-900 mb-2">From Date</label>
                <input type="date" name="date_from" value="{{ $dateFrom }}" class="w-full h-11 rounded-lg border border-[#dbe0e6] bg-white px-3 py-2 text-sm outline-none focus:border-primary focus:ring-1 focus:ring-primary text-gray-900">
            </div>
            <div>
                <label class="block text-sm font-medium text-[#111418] text-gray-900 mb-2">To Date</label>
                <input type="date" name="date_to" value="{{ $dateTo }}" class="w-full h-11 rounded-lg border border-[#dbe0e6] bg-white px-3 py-2 text-sm outline-none focus:border-primary focus:ring-1 focus:ring-primary text-gray-900">
            </div>
            <div class="flex items-end">
                <button type="submit" class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-blue-600 transition-colors w-full">Generate Report</button>
            </div>
        </form>
    </div>

    <div class="bg-white rounded-xl shadow-sm border border-[#e5e7eb] overflow-hidden">
        <div class="p-6 border-b border-[#e5e7eb]">
            <h3 class="text-lg font-bold text-[#111418] text-gray-900">Trial Balance</h3>
            <p class="text-sm text-[#617589]">Period: {{ \Carbon\Carbon::parse($dateFrom)->format('M d, Y') }} to {{ \Carbon\Carbon::parse($dateTo)->format('M d, Y') }}</p>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full text-left">
                <thead class="bg-background-light border-b border-[#e5e7eb]">
                    <tr>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Account Code</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Account Name</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Type</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Debit</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Credit</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Balance</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-[#e5e7eb]">
                    @php
                        $totalDebit = 0;
                        $totalCredit = 0;
                    @endphp
                    @foreach($trialBalance as $item)
                        @php
                            $totalDebit += $item['debit'];
                            $totalCredit += $item['credit'];
                        @endphp
                        <tr class="hover:bg-background-light/50">
                            <td class="py-4 px-4 font-mono text-sm text-[#111418] text-gray-900">{{ $item['account']->code }}</td>
                            <td class="py-4 px-4 text-sm text-[#111418] text-gray-900">{{ $item['account']->name }}</td>
                            <td class="py-4 px-4">
                                <span class="px-2 py-1 rounded text-xs font-medium bg-blue-100 text-blue-800">
                                    {{ ucfirst($item['account']->type) }}
                                </span>
                            </td>
                            <td class="py-4 px-4 text-sm text-right font-medium text-[#111418] text-gray-900">
                                {{ number_format($item['debit'], 2) }}
                            </td>
                            <td class="py-4 px-4 text-sm text-right font-medium text-[#111418] text-gray-900">
                                {{ number_format($item['credit'], 2) }}
                            </td>
                            <td class="py-4 px-4 text-sm text-right font-medium {{ $item['balance'] >= 0 ? 'text-green-600' : 'text-red-600' }}">
                                {{ number_format($item['balance'], 2) }}
                            </td>
                        </tr>
                    @endforeach
                </tbody>
                <tfoot class="bg-background-light border-t-2 border-[#e5e7eb]">
                    <tr>
                        <td colspan="3" class="py-4 px-4 text-sm font-bold text-[#111418] text-gray-900">Total</td>
                        <td class="py-4 px-4 text-sm font-bold text-right text-[#111418] text-gray-900">{{ number_format($totalDebit, 2) }}</td>
                        <td class="py-4 px-4 text-sm font-bold text-right text-[#111418] text-gray-900">{{ number_format($totalCredit, 2) }}</td>
                        <td class="py-4 px-4"></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>
@endsection



