@extends('layouts.app')

@section('title', 'Profit & Loss Statement')

@section('content')
<div class="card mb-4">
    <div class="card-header">
        <h4 class="card-title mb-0">Profit & Loss Statement</h4>
    </div>
    <div class="card-body">
        <form method="GET" action="{{ route('accounting.reports.profit-loss') }}">
            <div class="row g-3 align-items-end">
                <div class="col-md-3">
                    <label class="form-label">Start Date</label>
                    <input type="date" name="start_date" class="form-control" value="{{ $startDate }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label">End Date</label>
                    <input type="date" name="end_date" class="form-control" value="{{ $endDate }}">
                </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.profit-loss') }}" class="btn btn-secondary w-100">
                        <i class="fas fa-times me-2"></i>Clear
                    </a>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="row justify-content-center">
    <div class="col-lg-10">
        <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">Profit & Loss Statement</h6>
                <small class="text-muted">For the period {{ $startDate ? \Carbon\Carbon::parse($startDate)->format('M d, Y') : 'Beginning' }} to {{ $endDate ? \Carbon\Carbon::parse($endDate)->format('M d, Y') : 'Present' }}</small>
            </div>
            
            <div class="card-body">
                <h4 class="text-primary border-bottom pb-2 mb-3">Revenues</h4>
                <div class="table-responsive mb-4">
                    <table class="table table-borderless table-sm mb-0">
                        <tbody>
                            @forelse($revenues as $rev)
                                <tr>
                                    <td>{{ $rev->name }}</td>
                                    <td class="text-end text-success">{{ number_format($rev->amount, 2) }}</td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="2" class="text-muted fst-italic ps-3">No revenue recorded in this period.</td>
                                </tr>
                            @endforelse
                        </tbody>
                        <tfoot>
                            <tr class="fw-bold fs-5 border-top">
                                <td>Total Revenue:</td>
                                <td class="text-end text-success">Rs. {{ number_format($totalRevenue, 2) }}</td>
                            </tr>
                        </tfoot>
                    </table>
                </div>

                <div class="table-responsive mb-4">
                    <table class="table table-borderless table-sm mb-0">
                        <tbody>
                            <tr>
                                <td class="text-danger ps-0">Less: Cost of Goods Sold (COGS)</td>
                                <td class="text-end text-danger">-{{ number_format($cogsAmount, 2) }}</td>
                            </tr>
                        </tbody>
                        <tfoot>
                            <tr class="fw-bold fs-4 border-top border-bottom">
                                <td>Gross Profit:</td>
                                <td class="text-end @if($grossProfit >= 0) text-success @else text-danger @endif">Rs. {{ number_format($grossProfit, 2) }}</td>
                            </tr>
                        </tfoot>
                    </table>
                </div>

                <h4 class="text-danger border-bottom pb-2 mb-3 mt-4">Expenses & Costs</h4>
                <div class="table-responsive mb-4">
                    <table class="table table-borderless table-sm mb-0">
                        <tbody>
                            @forelse($expenses as $exp)
                                <tr>
                                    <td>{{ $exp->name }}</td>
                                    <td class="text-end text-danger">{{ number_format($exp->amount, 2) }}</td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="2" class="text-muted fst-italic ps-3">No expenses recorded in this period.</td>
                                </tr>
                            @endforelse
                        </tbody>
                        <tfoot>
                            <tr class="fw-bold fs-5 border-top">
                                <td>Total Expenses:</td>
                                <td class="text-end text-danger">Rs. {{ number_format($totalExpense, 2) }}</td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>

            <div class="card-footer @if($netProfit >= 0) bg-success-subtle @else bg-danger-subtle @endif pt-4 pb-4">
                <div class="d-flex justify-content-between align-items-center">
                    <h3 class="mb-0 @if($netProfit >= 0) text-success @else text-danger @endif">Net Profit/Loss</h3>
                    <h2 class="mb-0 fw-bold @if($netProfit >= 0) text-success @else text-danger @endif">
                        Rs. {{ number_format($netProfit, 2) }}
                    </h2>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
