@extends('layouts.app')

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

@section('content')
<div class="card mb-4">
    <div class="card-header">
        <h4 class="card-title mb-0">Trial Balance</h4>
    </div>
    <div class="card-body">
        <form method="GET" action="{{ route('accounting.reports.trial-balance') }}">
            <div class="row g-3 align-items-end">
                <div class="col-md-3">
                    <label class="form-label">As of Date</label>
                    <input type="date" name="as_of_date" class="form-control" value="{{ $asOfDate }}">
                </div>
                <div class="col-md-2 d-flex gap-2">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="fas fa-filter me-2"></i>Filter
                    </button>
                    <a href="{{ route('accounting.reports.trial-balance') }}" class="btn btn-secondary w-100">
                        <i class="fas fa-times me-2"></i>Clear
                    </a>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-header bg-light text-center py-3">
        <h5 class="mb-0">Habib Cables</h5>
        <h6 class="text-muted mb-0">Trial Balance{{ $asOfDate ? ' as of ' . \Carbon\Carbon::parse($asOfDate)->format('F d, Y') : ' (All Time)' }}</h6>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover table-bordered mb-0">
                <thead class="table-light">
                    <tr>
                        <th>Account Code</th>
                        <th>Account Name</th>
                        <th class="text-end" width="20%">Debit</th>
                        <th class="text-end" width="20%">Credit</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($balances as $account)
                        <tr>
                            <td>{{ $account->code }}</td>
                            <td>{{ $account->name }}</td>
                            <td class="text-end">{{ $account->calculated_debit > 0 ? number_format($account->calculated_debit, 2) : '-' }}</td>
                            <td class="text-end">{{ $account->calculated_credit > 0 ? number_format($account->calculated_credit, 2) : '-' }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="4" class="text-center py-4">No data found.</td>
                        </tr>
                    @endforelse
                </tbody>
                <tfoot class="table-light fw-bold fs-5">
                    <tr>
                        <td colspan="2" class="text-end">Totals:</td>
                        <td class="text-end text-primary">Rs. {{ number_format($totalDebit, 2) }}</td>
                        <td class="text-end text-success">Rs. {{ number_format($totalCredit, 2) }}</td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>
@endsection
