@extends('layouts.master')

@push('css')
<style>
    .lar-wrap { font-family: Arial, sans-serif; font-size: 13px; }

    /* Header */
    .lar-header { text-align: center; margin-bottom: 10px; line-height: 1.6; }
    .lar-header .co-name  { font-size: 22px; font-weight: 700; letter-spacing: 1px; }
    .lar-header .co-sub   { font-size: 15px; font-weight: 600; }
    .lar-header .co-range { font-size: 12px; color: #444; margin-top: 3px; }

    /* Table */
    .lar-table {
        width: 100%;
        border-collapse: collapse;
        font-size: 13px;
        margin-top: 12px;
    }
    .lar-table thead th {
        border-top:    2px solid #000;
        border-bottom: 2px solid #000;
        border-left:   none;
        border-right:  none;
        padding: 8px 10px;
        font-weight: 700;
        font-size: 13px;
        background: #fff;
        white-space: nowrap;
    }
    .lar-table thead th:first-child { text-align: left; }
    .lar-table thead th.num         { text-align: right; }
    .lar-table thead th.ctr         { text-align: center; }

    /* Data rows — solid side borders, dashed bottom */
    .lar-table tbody td {
        border-left:   none;
        border-right:  none;
        border-top:    none;
        border-bottom: 1px dashed #aaa;
        padding: 7px 10px;
        vertical-align: middle;
    }

    /* Department header row */
    .lar-table tr.dept-row td {
        font-weight: 700;
        font-size: 13.5px;
        color: #cc0000;
        border-bottom: none;
        padding: 10px 10px 2px;
        letter-spacing: 0.3px;
    }

    /* Dept totals row */
    .lar-table tr.dept-total-row td {
        border-top:    1px dashed #888;
        border-bottom: 1px dashed #888;
        font-size: 12.5px;
        padding: 6px 10px;
    }

    /* Grand total row */
    .lar-table tr.grand-row td {
        border-top:    2px solid #000 !important;
        border-bottom: 2px solid #000 !important;
        font-weight: 700;
        font-size: 13px;
        background: #f5f5f5;
        padding: 8px 10px;
    }

    .lar-table td.num { text-align: right;  white-space: nowrap; }
    .lar-table td.ctr { text-align: center; white-space: nowrap; }
    .lar-table td.lft { text-align: left; }
    .text-red { color: #cc0000; }
</style>
@endpush

@section('content')

<div class="card">
    <div class="card-header">
        <h3>Loan / Advance Summary Report</h3>
    </div>
    <div class="card-body">

        {{-- ── Filter Form ── --}}
        <form class="row g-3" method="GET" action="{{ route('loan-advance-report.index') }}">

            <div class="col-md-3">
                <label>From Date</label>
                <input type="date" name="from_date" class="form-control" value="{{ $fromDate }}">
            </div>

            <div class="col-md-3">
                <label>To Date</label>
                <input type="date" name="to_date" class="form-control" value="{{ $toDate }}">
            </div>

            <div class="col-md-3">
                <label>Department</label>
                <select name="department_id" class="form-control">
                    <option value="">— All Departments —</option>
                    @foreach($departments as $dept)
                        <option value="{{ $dept->id }}"
                            {{ $departmentId == $dept->id ? 'selected' : '' }}>
                            {{ $dept->name }}
                        </option>
                    @endforeach
                </select>
            </div>

            <div class="col-md-3">
                <label>Employee</label>
                <select name="employee_id" class="form-control">
                    <option value="">— All Employees —</option>
                    @foreach($employees as $emp)
                        <option value="{{ $emp->id }}"
                            {{ $employeeId == $emp->id ? 'selected' : '' }}>
                            {{ $emp->first_name }} {{ $emp->last_name }}
                        </option>
                    @endforeach
                </select>
            </div>

            <div class="col-12 d-flex gap-2">
                <button type="submit" class="btn btn-primary">
                    <i class="fa fa-search"></i> View
                </button>
                @if($reportData && count($reportData['departments']))
                    <a href="{{ route('loan-advance-report.print', [
                            'from_date'     => $fromDate,
                            'to_date'       => $toDate,
                            'department_id' => $departmentId,
                            'employee_id'   => $employeeId,
                        ]) }}"
                       target="_blank" class="btn btn-warning">
                        <i class="fa fa-print"></i> Print
                    </a>
                @endif
            </div>
        </form>

        <hr />

        @if($reportData !== null)
            @if(empty($reportData['departments']))
                <div class="alert alert-info text-center">No loan/advance records found for the selected period.</div>
            @else

                @php
                    $n  = fn($v) => $v  ? number_format($v, 0) : '-';
                    $n0 = fn($v) => number_format($v, 0);
                @endphp

                {{-- Summary Cards --}}
                <div class="row mb-3">
                    <div class="col-md-3">
                        <div class="card border-secondary">
                            <div class="card-body py-2 text-center">
                                <small class="text-muted d-block">Total Opening</small>
                                <strong>{{ $n($reportData['grand']['opening']) }}</strong>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card border-primary">
                            <div class="card-body py-2 text-center">
                                <small class="text-muted d-block">Total Payments</small>
                                <strong class="text-primary">{{ $n0($reportData['grand']['payments']) }}</strong>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card border-danger">
                            <div class="card-body py-2 text-center">
                                <small class="text-muted d-block">Total Deductions</small>
                                <strong class="text-danger">{{ $n0($reportData['grand']['deductions']) }}</strong>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card border-success">
                            <div class="card-body py-2 text-center">
                                <small class="text-muted d-block">Total Balance</small>
                                <strong class="text-success">{{ $n0($reportData['grand']['balance']) }}</strong>
                            </div>
                        </div>
                    </div>
                </div>

                {{-- ── Report Table ── --}}
                <div class="lar-wrap">

                    <div class="lar-header">
                        <div class="co-name">M.D.S</div>
                        <div class="co-sub">Loan / Advance Summary Report</div>
                        <div class="co-range">
                            From: {{ \Carbon\Carbon::parse($fromDate)->format('d/m/Y') }}
                            &nbsp;To:&nbsp; {{ \Carbon\Carbon::parse($toDate)->format('d/m/Y') }}
                            @if($selectedEmployee)
                                &nbsp;|&nbsp; Employee: {{ $selectedEmployee->first_name }} {{ $selectedEmployee->last_name }}
                            @endif
                            @if($selectedDepartment)
                                &nbsp;|&nbsp; Dept: {{ $selectedDepartment->name }}
                            @endif
                        </div>
                    </div>

                    <table class="lar-table">
                        <thead>
                            <tr>
                                <th style="width:25%">Name</th>
                                <th class="ctr" style="width:15%">Designation</th>
                                <th class="ctr" style="width:12%">Card No</th>
                                <th class="num" style="width:12%">Opening</th>
                                <th class="num" style="width:12%">Payments</th>
                                <th class="num" style="width:12%">Deductions</th>
                                <th class="num" style="width:12%">Balance</th>
                            </tr>
                        </thead>
                        <tbody>

                            @foreach($reportData['departments'] as $deptName => $deptData)

                                {{-- Department row --}}
                                <tr class="dept-row">
                                    <td colspan="7">{{ $deptName }}</td>
                                </tr>

                                @foreach($deptData['rows'] as $row)
                                    <tr>
                                        <td class="lft">{{ $row['employee_name'] }}</td>
                                        <td class="ctr">{{ $row['designation'] }}</td>
                                        <td class="ctr">{{ $row['card_no'] }}</td>
                                        <td class="num">{{ $n($row['opening']) }}</td>
                                        <td class="num">{{ $n($row['payments']) }}</td>
                                        <td class="num">{{ $n($row['deductions']) }}</td>
                                        <td class="num">{{ $n($row['balance']) }}</td>
                                    </tr>
                                @endforeach

                                {{-- Dept Totals --}}
                                <tr class="dept-total-row">
                                    <td class="lft">Dept. Totals ==&gt;</td>
                                    <td></td>
                                    <td></td>
                                    <td class="num">{{ $n($deptData['totals']['opening']) }}</td>
                                    <td class="num">{{ $n0($deptData['totals']['payments']) }}</td>
                                    <td class="num">{{ $n0($deptData['totals']['deductions']) }}</td>
                                    <td class="num">{{ $n0($deptData['totals']['balance']) }}</td>
                                </tr>

                            @endforeach

                            {{-- Grand Totals --}}
                            <tr class="grand-row">
                                <td class="lft">GRAND TOTALS ==&gt;</td>
                                <td></td>
                                <td></td>
                                <td class="num">{{ $n($reportData['grand']['opening']) }}</td>
                                <td class="num">{{ $n0($reportData['grand']['payments']) }}</td>
                                <td class="num">{{ $n0($reportData['grand']['deductions']) }}</td>
                                <td class="num">{{ $n0($reportData['grand']['balance']) }}</td>
                            </tr>

                        </tbody>
                    </table>

                </div>{{-- .lar-wrap --}}

            @endif
        @endif

    </div>
</div>

@endsection