@extends('layouts.app')
@section('title', 'Journal Report')

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

@push('styles')
<style>
.journal-report-title{text-align:center;margin:14px 0 18px}
.journal-report-title h2{font-size:20px;font-weight:700;margin:0;color:#111827}
.journal-report-title div{font-size:13px;color:#6b7280;margin-top:2px}
.jr-table th{font-size:11px;text-transform:uppercase;letter-spacing:.04em;color:#4b5563}
.jr-table td{font-size:12.5px;vertical-align:top}
.jr-transaction-start td{border-top:2px solid #d1d5db}
.jr-amount{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace}
@media print{
    #sidebar,#topbar,.page-header,#filterCard,.no-print{display:none!important}
    #main{margin-left:0!important;padding-top:0!important}
    .page-content{padding:0!important}
    body{background:#fff!important}
    .card{border:0!important;box-shadow:none!important}
    .card-body{padding:0!important}
    .jr-table th,.jr-table td{font-size:10px;padding:4px 5px!important}
}
</style>
@endpush

@section('content')
@php
    $companyId = auth()->user()->company_id;
    $fmt = fn($amount) => abs((float) $amount) < 0.005 ? '' : \App\Helpers\NumberHelper::formatAmount((float) $amount, $companyId);
@endphp

<div class="page-header">
    <div>
        <h1 class="page-title">Journal Report</h1>
        <p class="page-subtitle">All posted accounting transactions shown as journal entries</p>
    </div>
    <button class="btn btn-outline-secondary no-print" onclick="window.print()">
        <i class="bi bi-printer me-1"></i>Print
    </button>
</div>

<div class="card mb-3" id="filterCard">
    <div class="card-body">
        <form method="GET" action="{{ route('reports.journal') }}">
            <div class="row g-3 align-items-end">
                <div class="col-12 col-lg-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-lg-2">
                    <label class="form-label fw-semibold">From</label>
                    <input type="date" name="date_from" class="form-control" value="{{ $dateFrom }}">
                </div>
                <div class="col-6 col-lg-2">
                    <label class="form-label fw-semibold">To</label>
                    <input type="date" name="date_to" class="form-control" value="{{ $dateTo }}">
                </div>
                <div class="col-12 col-lg-2">
                    <label class="form-label fw-semibold">Transaction Type</label>
                    <select name="voucher_type" class="form-select">
                        <option value="">All Types</option>
                        @foreach($voucherTypes as $type => $label)
                            <option value="{{ $type }}" {{ $voucherType === $type ? 'selected' : '' }}>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-12 col-lg-2">
                    <label class="form-label fw-semibold">Look For</label>
                    <input type="text" name="search" class="form-control" value="{{ $search }}" placeholder="Voucher, memo, account">
                </div>
                <div class="col-6 col-lg-1">
                    <button type="submit" class="btn btn-primary w-100"><i class="bi bi-search"></i></button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <div class="journal-report-title">
            <div class="fw-semibold">{{ auth()->user()->company?->name ?? config('app.name') }}</div>
            <h2>Journal</h2>
            <div>{{ \Carbon\Carbon::parse($dateFrom)->format('d M Y') }} - {{ \Carbon\Carbon::parse($dateTo)->format('d M Y') }}</div>
        </div>

        <div class="table-responsive">
            <table class="table table-sm jr-table mb-0" style="min-width:1050px">
                <thead>
                    <tr>
                        <th style="width:70px">Trans #</th>
                        <th style="width:110px">Type</th>
                        <th style="width:95px">Date</th>
                        <th style="width:140px">Num</th>
                        <th style="width:150px">Name</th>
                        <th>Memo</th>
                        <th style="width:190px">Account</th>
                        <th class="text-end" style="width:120px">Debit</th>
                        <th class="text-end" style="width:120px">Credit</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($groups as $voucherId => $group)
                        @foreach($group as $index => $entry)
                            @php
                                $voucher = $entry->voucher;
                                $isFirst = $index === 0;
                            @endphp
                            <tr class="{{ $isFirst ? 'jr-transaction-start' : '' }}">
                                <td>{{ $isFirst ? $voucherId : '' }}</td>
                                <td>
                                    @if($isFirst && $voucher)
                                        {{ $voucher->getTypeLabel() }}
                                    @endif
                                </td>
                                <td>{{ $isFirst ? $entry->entry_date->format('d-m-Y') : '' }}</td>
                                <td>
                                    @if($isFirst && $voucher)
                                        <a href="{{ route('vouchers.show', [$voucher->voucher_type, $voucher->id]) }}" class="text-decoration-none fw-semibold">
                                            {{ $voucher->voucher_number }}
                                        </a>
                                    @endif
                                </td>
                                <td>{{ $isFirst ? ($entry->journal_party_name ?: '') : '' }}</td>
                                <td>{{ $entry->narration ?: ($voucher?->narration ?? '') }}</td>
                                <td>
                                    <a href="{{ route('reports.general-ledger', ['account_id' => $entry->account_id, 'date_from' => $dateFrom, 'date_to' => $dateTo]) }}" class="text-decoration-none">
                                        {{ $entry->account?->name ?? '—' }}
                                    </a>
                                    @if($entry->account?->code)<div class="text-muted" style="font-size:11px">{{ $entry->account->code }}</div>@endif
                                </td>
                                <td class="text-end jr-amount">{{ $fmt($entry->debit) }}</td>
                                <td class="text-end jr-amount">{{ $fmt($entry->credit) }}</td>
                            </tr>
                        @endforeach
                    @empty
                        <tr>
                            <td colspan="9" class="text-center py-5 text-muted">No posted journal transactions found for this period.</td>
                        </tr>
                    @endforelse
                </tbody>
                <tfoot>
                    <tr class="table-light">
                        <th colspan="7" class="text-end">TOTAL</th>
                        <th class="text-end jr-amount">{{ \App\Helpers\NumberHelper::formatAmount((float) $totals['debit'], $companyId) }}</th>
                        <th class="text-end jr-amount">{{ \App\Helpers\NumberHelper::formatAmount((float) $totals['credit'], $companyId) }}</th>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>
@endsection
