@extends('layouts.app')
@section('title', 'Trial Balance')

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
    <li class="breadcrumb-item active">Trial Balance</li>
@endsection

@section('content')
@php
    $companyId = auth()->user()->company_id;
    $fmt = fn($amount) => abs((float) $amount) < 0.005 ? '—' : \App\Helpers\NumberHelper::formatAmount((float) $amount, $companyId);
    $balanced = abs($totals['closing_debit'] - $totals['closing_credit']) < 0.01
        && abs($totals['period_debit'] - $totals['period_credit']) < 0.01;
@endphp

<div class="page-header">
    <div>
        <h1 class="page-title">Trial Balance</h1>
        <p class="page-subtitle">Opening balance, current period movement, and closing balance</p>
    </div>
    <span class="badge {{ $balanced ? 'bg-success' : 'bg-danger' }} px-3 py-2">
        {{ $balanced ? 'Balanced' : 'Out of Balance' }}
    </span>
</div>

<div class="card mb-3">
    <div class="card-body">
        <form method="GET" action="{{ route('reports.trial-balance') }}">
            <div class="row g-3 align-items-end">
                <div class="col-12 col-md-3">
                    <label class="form-label fw-semibold">Fiscal Year</label>
                    <select name="fiscal_year_id" class="form-select">
                        @foreach($fiscalYears as $fy)
                            <option value="{{ $fy->id }}" {{ (int) $selectedFiscalYearId === $fy->id ? 'selected' : '' }}>
                                {{ $fy->name }}{{ $fy->is_current ? ' (Current)' : '' }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-6 col-md-2">
                    <label class="form-label fw-semibold">Date From</label>
                    <input type="date" name="date_from" class="form-control" value="{{ $dateFrom }}">
                </div>
                <div class="col-6 col-md-2">
                    <label class="form-label fw-semibold">Date To</label>
                    <input type="date" name="date_to" class="form-control" value="{{ $dateTo }}">
                </div>
                <div class="col-12 col-md-2">
                    <div class="form-check mb-2">
                        <input class="form-check-input" type="checkbox" name="include_zero" value="1" id="includeZero" {{ $includeZero ? 'checked' : '' }}>
                        <label class="form-check-label" for="includeZero">Include zero balances</label>
                    </div>
                </div>
                <div class="col-6 col-md-1">
                    <button type="submit" class="btn btn-primary w-100"><i class="bi bi-search"></i></button>
                </div>
                <div class="col-6 col-md-1">
                    <a href="{{ route('reports.trial-balance') }}" class="btn btn-outline-secondary w-100"><i class="bi bi-x-lg"></i></a>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-header d-flex flex-wrap justify-content-between align-items-center gap-2">
        <div>
            <strong>Trial Balance</strong>
            <span class="text-muted ms-2">{{ \Carbon\Carbon::parse($dateFrom)->format('d M Y') }} to {{ \Carbon\Carbon::parse($dateTo)->format('d M Y') }}</span>
        </div>
        <small class="text-muted">Click an account to drill down to General Ledger.</small>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-bordered table-hover mb-0 align-middle" style="min-width:1100px">
                <thead class="table-light">
                    <tr class="text-center">
                        <th rowspan="2" class="text-start" style="width:110px">Account No.</th>
                        <th rowspan="2" class="text-start">Account Name</th>
                        <th colspan="2">Opening Balance</th>
                        <th colspan="2">Current Period</th>
                        <th colspan="2">Closing Balance</th>
                    </tr>
                    <tr class="text-center">
                        <th>Debit</th>
                        <th>Credit</th>
                        <th>Debit</th>
                        <th>Credit</th>
                        <th>Debit</th>
                        <th>Credit</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($rows as $row)
                    <tr>
                        <td><code>{{ $row->code ?: '—' }}</code></td>
                        <td>
                            <a href="{{ route('reports.general-ledger', ['account_id' => $row->id, 'date_from' => $dateFrom, 'date_to' => $dateTo]) }}" class="fw-semibold text-decoration-none">
                                {{ $row->name }}
                            </a>
                            <div class="text-muted" style="font-size:11px">{{ ucfirst($row->type) }}</div>
                        </td>
                        <td class="text-end">{{ $fmt($row->opening_debit) }}</td>
                        <td class="text-end">{{ $fmt($row->opening_credit) }}</td>
                        <td class="text-end">{{ $fmt($row->period_debit) }}</td>
                        <td class="text-end">{{ $fmt($row->period_credit) }}</td>
                        <td class="text-end fw-semibold">{{ $fmt($row->closing_debit) }}</td>
                        <td class="text-end fw-semibold">{{ $fmt($row->closing_credit) }}</td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="8" class="text-center py-5 text-muted">
                            No ledger balances found for the selected period.
                        </td>
                    </tr>
                    @endforelse
                </tbody>
                <tfoot class="table-light">
                    <tr>
                        <th colspan="2" class="text-end">Total</th>
                        <th class="text-end">{{ $fmt($totals['opening_debit']) }}</th>
                        <th class="text-end">{{ $fmt($totals['opening_credit']) }}</th>
                        <th class="text-end">{{ $fmt($totals['period_debit']) }}</th>
                        <th class="text-end">{{ $fmt($totals['period_credit']) }}</th>
                        <th class="text-end">{{ $fmt($totals['closing_debit']) }}</th>
                        <th class="text-end">{{ $fmt($totals['closing_credit']) }}</th>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>
@endsection
