@extends('layouts.app')

@section('title', 'Day Book / Cash Book')

@section('content')
<div class="card mb-4">
    <div class="card-header border-bottom">
        <h4 class="card-title mb-0">Day Book / Cash Book</h4>
    </div>
    <div class="card-body">
        <form method="GET" action="{{ route('accounting.reports.day-book') }}">
            <div class="row g-3 align-items-end">
                <div class="col-md-3">
                    <label class="form-label">Date</label>
                    <input type="date" name="date" class="form-control" value="{{ $date }}">
                </div>
                <div class="col-md-3 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.day-book') }}" 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 d-flex justify-content-between align-items-center">
        <h5 class="mb-0">Transactions{{ $date ? ' on ' . \Carbon\Carbon::parse($date)->format('F d, Y') : ' (All Dates)' }}</h5>
    </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 width="15%">Time</th>
                        <th width="20%">Account</th>
                        <th width="15%">Reference</th>
                        <th width="20%">Remarks</th>
                        <th width="15%" class="text-end">Debit In (Rs)</th>
                        <th width="15%" class="text-end">Credit Out (Rs)</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($transactions as $txn)
                        <tr>
                            <td>{{ $txn->created_at->format('H:i') }}</td>
                            <td>
                                <strong>{{ $txn->account->name ?? 'N/A' }}</strong>
                                @if(isset($txn->chartOfAccount))
                                    <div class="small text-muted">{{ $txn->chartOfAccount->name }}</div>
                                @endif
                            </td>
                            <td>
                                @if($txn->reference_type)
                                    <span class="badge bg-light text-dark border">
                                        {{ class_basename($txn->reference_type) }} #{{ $txn->reference_id }}
                                    </span>
                                @else
                                    -
                                @endif
                            </td>
                            <td>{{ $txn->remarks }}</td>
                            <td class="text-end text-danger fw-bold">
                                {{ $txn->transaction_type == 'debit' ? number_format($txn->amount, 2) : '-' }}
                            </td>
                            <td class="text-end text-success fw-bold">
                                {{ $txn->transaction_type == 'credit' ? number_format($txn->amount, 2) : '-' }}
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="text-center py-4 text-muted">No transactions recorded on this date.</td>
                        </tr>
                    @endforelse
                </tbody>
                <tfoot class="table-light fs-5 fw-bold">
                    <tr>
                        <td colspan="4" class="text-end">Daily Totals:</td>
                        <td class="text-end text-danger">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
