@extends('layouts.app')
@section('title', 'Bank Reconciliation')

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

@section('content')
@if(session('success'))
<div class="alert alert-success alert-dismissible d-flex gap-2 mb-3" style="font-size:13.5px">
    <i class="bi bi-check-circle flex-shrink-0"></i><div>{{ session('success') }}</div>
    <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
</div>
@endif
@if($errors->any())
<div class="alert alert-danger alert-dismissible mb-3">
    @foreach($errors->all() as $error)<div>{{ $error }}</div>@endforeach
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
@endif

<div class="page-header flex-wrap gap-3">
    <div>
        <p class="text-uppercase text-primary fw-semibold mb-1" style="font-size:11px;letter-spacing:.08em">Banking</p>
        <h1 class="page-title mb-1">Bank Reconciliation</h1>
        <p class="page-subtitle mb-0">Match your bank statement with posted transactions and clear the difference to zero.</p>
    </div>
</div>

@if($accounts->isEmpty())
<div class="alert alert-warning">
    No bank account ledgers found. Tag a chart of accounts ledger as <strong>Bank Account</strong> (not cash) first.
</div>
@else
<div class="card mb-3">
    <div class="card-body">
        <form method="GET" action="{{ route('banking.reconciliation') }}" class="row g-3 align-items-end">
            <div class="col-12 col-md-6 col-lg-4">
                <label class="form-label fw-semibold">Bank account</label>
                <select name="account_id" class="form-select" onchange="this.form.submit()">
                    @foreach($accounts as $account)
                        <option value="{{ $account->id }}" {{ $selectedAccountId === $account->id ? 'selected' : '' }}>
                            {{ $account->code ? $account->code . ' — ' : '' }}{{ $account->name }}
                        </option>
                    @endforeach
                </select>
            </div>
        </form>
    </div>
</div>

@if($workspace && $workspace['account'])
<div class="card mb-3">
    <div class="card-header fw-semibold">Start reconciliation</div>
    <div class="card-body">
        <form method="POST" action="{{ route('banking.reconciliation.start', $workspace['account']->id) }}">
            @csrf
            <div class="row g-3">
                <div class="col-6 col-md-3">
                    <label class="form-label fw-semibold">Statement ending date</label>
                    <input type="date" name="statement_date" class="form-control" required value="{{ old('statement_date', now()->toDateString()) }}">
                </div>
                <div class="col-6 col-md-3">
                    <label class="form-label fw-semibold">Statement ending balance</label>
                    <input type="number" name="statement_balance" class="form-control" step="0.01" required value="{{ old('statement_balance') }}">
                </div>
                <div class="col-6 col-md-3">
                    <label class="form-label fw-semibold">Beginning balance</label>
                    <input type="number" name="beginning_balance" class="form-control" step="0.01"
                           value="{{ old('beginning_balance', number_format($workspace['beginning_balance'], 2, '.', '')) }}">
                    <div class="form-text">From last completed reconciliation or ledger opening.</div>
                </div>
                <div class="col-12 col-md-3 d-flex align-items-end">
                    @if(auth()->user()->hasPermission('banking', 'reconcile'))
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-play-circle me-1"></i>Start reconciliation
                    </button>
                    @endif
                </div>
            </div>
        </form>
    </div>
</div>

@if($workspace['history']->isNotEmpty())
<div class="card">
    <div class="card-header fw-semibold">Reconciliation history — {{ $workspace['account']->name }}</div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-light">
                    <tr>
                        <th>Statement date</th>
                        <th class="text-end">Statement balance</th>
                        <th>Status</th>
                        <th>Completed</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($workspace['history'] as $item)
                    <tr>
                        <td>{{ $item->statement_date?->format('d/m/Y') }}</td>
                        <td class="text-end">{{ number_format((float) $item->statement_balance, 2) }}</td>
                        <td>
                            @if($item->isReconciled())
                                <span class="badge" style="background:#d1e7dd;color:#0a3622">Completed</span>
                            @else
                                <span class="badge" style="background:#fff3cd;color:#664d03">In progress</span>
                            @endif
                        </td>
                        <td class="text-muted">{{ $item->reconciled_at?->format('d/m/Y H:i') ?: '—' }}</td>
                        <td class="text-end">
                            <a href="{{ route('banking.reconciliation.show', $item) }}" class="btn btn-outline-primary btn-sm">
                                {{ $item->isInProgress() ? 'Resume' : 'View' }}
                            </a>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
@endif
@endif
@endif
@endsection
