@extends('layouts.app')

@section('title', 'Balance Sheet')

@section('content')
<div class="card mb-4">
    <div class="card-header">
        <h4 class="card-title mb-0">Balance Sheet</h4>
    </div>
    <div class="card-body">
        <form method="GET" action="{{ route('accounting.reports.balance-sheet') }}">
            <div class="row g-3 align-items-end">
                <div class="col-md-3">
                    <label class="form-label">As of Date</label>
                    <input type="date" name="as_of_date" class="form-control" value="{{ $asOfDate }}">
                </div>
                <div class="col-md-2 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.balance-sheet') }}" 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 text-center py-3">
        <h5 class="mb-0">Habib Cables</h5>
        <h6 class="text-muted mb-0">Balance Sheet{{ $asOfDate ? ' as of ' . \Carbon\Carbon::parse($asOfDate)->format('F d, Y') : ' (All Time)' }}</h6>
    </div>
    
    <div class="card-body pt-4">
        <div class="row g-5">
            <!-- Assets Side -->
            <div class="col-md-6 border-end">
                <h4 class="text-primary border-bottom pb-2 mb-3">Assets</h4>
                <div class="table-responsive">
                    <table class="table table-borderless table-sm mb-4">
                        <tbody>
                            @forelse($assets as $asset)
                                <tr>
                                    <td>{{ $asset->name }}</td>
                                    <td class="text-end">{{ number_format($asset->calculated_balance, 2) }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="2" class="text-muted fst-italic">No assets to display.</td></tr>
                            @endforelse
                        </tbody>
                        <tfoot>
                            <tr class="fw-bold fs-5 border-top border-2 border-primary">
                                <td>Total Assets</td>
                                <td class="text-end">Rs. {{ number_format($totalAssets, 2) }}</td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
            
            <!-- Liabilities & Equity Side -->
            <div class="col-md-6">
                <h4 class="text-danger border-bottom pb-2 mb-3">Liabilities</h4>
                <div class="table-responsive mb-4">
                    <table class="table table-borderless table-sm mb-0">
                        <tbody>
                            @forelse($liabilities as $liability)
                                <tr>
                                    <td>{{ $liability->name }}</td>
                                    <td class="text-end">{{ number_format($liability->calculated_balance, 2) }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="2" class="text-muted fst-italic">No liabilities to display.</td></tr>
                            @endforelse
                        </tbody>
                        <tfoot>
                            <tr class="fw-bold border-top">
                                <td>Total Liabilities</td>
                                <td class="text-end">Rs. {{ number_format($liabilities->sum('calculated_balance') ?? 0, 2) }}</td>
                            </tr>
                        </tfoot>
                    </table>
                </div>

                <h4 class="text-info border-bottom pb-2 mb-3">Equity</h4>
                <div class="table-responsive">
                    <table class="table table-borderless table-sm mb-4">
                        <tbody>
                            @forelse($equity as $eq)
                                <tr>
                                    <td>{{ $eq->name }}</td>
                                    <td class="text-end">{{ number_format($eq->calculated_balance, 2) }}</td>
                                </tr>
                            @empty
                            @endforelse
                            <tr>
                                <td class="fw-bold">Current Period Profit/Loss</td>
                                <td class="text-end {{ $currentPeriodProfit >= 0 ? 'text-success' : 'text-danger' }}">
                                    {{ number_format($currentPeriodProfit, 2) }}
                                </td>
                            </tr>
                        </tbody>
                        <tfoot>
                            <tr class="fw-bold border-top">
                                <td>Total Equity</td>
                                <td class="text-end">Rs. {{ number_format($totalEquity, 2) }}</td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
                
                <table class="table table-borderless table-sm mt-5">
                    <tr class="fw-bold fs-5 border-top border-2 border-dark">
                        <td>Total Liabilities & Equity</td>
                        <td class="text-end">Rs. {{ number_format($totalLiabilitiesAndEquity, 2) }}</td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
