@extends('layouts.master')
@section('title', 'Cashbook Report')

@section('styles')
<style>
.btn-back {
    text-decoration: none;
    color: #1AB9D9;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    padding: 5px 15px;
    border-radius: 6px;
    transition: all 0.3s ease;
}
.btn-back:hover { background-color: rgba(26, 185, 217, 0.1); }
.summary-card {
    border-radius: 12px;
    background: #fff;
    box-shadow: 0 4px 12px rgba(0,0,0,0.06);
    padding: 1rem;
    height: 100%;
}
.summary-title {
    font-size: 0.78rem;
    text-transform: uppercase;
    color: #6b7280;
    letter-spacing: 0.04em;
    font-weight: 700;
}
.summary-value {
    font-size: 1.35rem;
    font-weight: 700;
    color: #111827;
}
.txn-meta {
    color: #6b7280;
    font-size: 0.78rem;
    margin-top: 0.2rem;
}
.report-shell {
    border: 1px solid #dee2e6;
    border-radius: 10px;
    padding: 1rem;
    background: #fff;
}
@media print {
    .no-print, .btn-back, form { display: none !important; }
    .card, .summary-card, .report-shell { box-shadow: none !important; }
}
</style>
@endsection

@section('content')
<div class="container-fluid py-3">
    <div class="d-flex justify-content-end mb-3">
        <a href="{{ route('modules.reports') }}" class="btn-back">
            <i class="fas fa-arrow-left me-2"></i> Back to Reports
        </a>
    </div>

    <div class="card shadow-sm mb-4">
        <div class="card-header bg-primary text-white py-3">
            <h5 class="mb-0"><i class="fas fa-book me-2"></i> Cashbook Report</h5>
        </div>
        <div class="card-body">
            @if($errors->any())
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        @foreach($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <form action="{{ route('cashbookDailyTransaction.generate') }}" method="POST" class="row g-3 align-items-end no-print">
                @csrf
                <div class="col-md-3">
                    <label class="form-label"><strong>Cashbook</strong></label>
                    <select class="form-control" name="cashbook_account" required>
                        <option value="" disabled {{ $cashbookId ? '' : 'selected' }}>Select Cashbook</option>
                        @foreach($cashbooks as $cashbook)
                            <option value="{{ $cashbook->id }}" {{ (string)$cashbookId === (string)$cashbook->id ? 'selected' : '' }}>
                                {{ $cashbook->name }} - {{ $cashbook->account_number }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label"><strong>From</strong></label>
                    <input type="date" class="form-control" name="date_from" value="{{ $dateFrom }}" required>
                </div>
                <div class="col-md-2">
                    <label class="form-label"><strong>To</strong></label>
                    <input type="date" class="form-control" name="date_to" value="{{ $dateTo }}" required>
                </div>
                <div class="col-md-2 d-grid">
                    <button type="submit" class="btn btn-primary">Generate Report</button>
                </div>
            </form>
        </div>
    </div>

    @if($reportData)
        <div class="d-flex justify-content-end gap-2 mb-3 no-print">
            <button type="button" class="btn btn-outline-primary" onclick="window.print()">
                <i class="fas fa-print me-2"></i>Print
            </button>
            <form action="{{ route('cashbookDailyTransaction.downloadPdf') }}" method="POST">
                @csrf
                <input type="hidden" name="cashbook_account" value="{{ $cashbookId }}">
                <input type="hidden" name="date_from" value="{{ $dateFrom }}">
                <input type="hidden" name="date_to" value="{{ $dateTo }}">
                <button type="submit" class="btn btn-success">
                    <i class="fas fa-file-pdf me-2"></i>Download PDF
                </button>
            </form>
        </div>

        <div class="report-shell">
            <div class="text-center mb-2">
                <h4 class="mb-1">{{ $reportData['company_name'] }}</h4>
                <h5 class="mb-0">Cashbook Report</h5>
            </div>
            <hr>
            <div class="row mb-3">
                <div class="col-md-4"><strong>Cashbook:</strong> {{ $reportData['cashbook']->name }} ({{ $reportData['cashbook']->account_number }})</div>
                <div class="col-md-4"><strong>Period:</strong> {{ $reportData['date_from'] }} - {{ $reportData['date_to'] }}</div>
                <div class="col-md-4"><strong>Generated:</strong> {{ $reportData['generated_at'] }}</div>
                <div class="col-md-4 mt-2"><strong>Currency:</strong> {{ $reportData['currency']->currency_code ?? $reportData['cashbook']->currency ?? 'N/A' }}</div>
            </div>

            <div class="row g-3 mb-4">
                <div class="col-md-3"><div class="summary-card"><div class="summary-title">Opening Balance</div><div class="summary-value">{{ number_format($reportData['opening_balance'], 2) }}</div></div></div>
                <div class="col-md-3"><div class="summary-card"><div class="summary-title">Total Receipts</div><div class="summary-value text-success">{{ number_format($reportData['total_receipts'], 2) }}</div></div></div>
                <div class="col-md-3"><div class="summary-card"><div class="summary-title">Total Payments/Transfers</div><div class="summary-value text-danger">{{ number_format($reportData['total_payments'], 2) }}</div></div></div>
                <div class="col-md-3"><div class="summary-card"><div class="summary-title">Closing Balance</div><div class="summary-value">{{ number_format($reportData['closing_balance'], 2) }}</div></div></div>
            </div>

            <h6 class="fw-bold mb-2">Transactions</h6>
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead class="table-light">
                        <tr>
                            <th>Date</th>
                            <th>Description</th>
                            <th>Ref</th>
                            <th class="text-end">In</th>
                            <th class="text-end">Out</th>
                            <th class="text-end">Balance</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($reportData['transactions'] as $txn)
                            <tr>
                                <td>{{ $txn['date'] }}</td>
                                <td>
                                    <div>{{ $txn['description'] }}</div>
                                    <div class="txn-meta">Type: {{ ucfirst($txn['transaction_type']) }} | Client: {{ $txn['client'] }} | Matter: {{ $txn['matter'] }}</div>
                                </td>
                                <td>{{ $txn['reference'] }}</td>
                                <td class="text-end">{{ $txn['in'] > 0 ? number_format($txn['in'], 2) : '' }}</td>
                                <td class="text-end">{{ $txn['out'] > 0 ? number_format($txn['out'], 2) : '' }}</td>
                                <td class="text-end fw-bold">{{ number_format($txn['balance'], 2) }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center text-muted">No cashbook transactions found for the selected period.</td>
                            </tr>
                        @endforelse
                    </tbody>
                    <tfoot class="table-secondary">
                        <tr>
                            <td colspan="3" class="text-end"><strong>Period Totals</strong></td>
                            <td class="text-end"><strong>{{ number_format($reportData['period_totals']['in'], 2) }}</strong></td>
                            <td class="text-end"><strong>{{ number_format($reportData['period_totals']['out'], 2) }}</strong></td>
                            <td></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    @endif
</div>
@endsection
