@extends('layouts.master')

@push('css')
<style>
    .aging-wrap {
        direction: rtl;
        font-family: 'Noto Nastaliq Urdu', 'Cairo', Arial, serif;
    }
    .aging-header { text-align: center; margin-bottom: 8px; }
    .aging-header .co-name  { font-size: 24px; font-weight: 700; letter-spacing: 2px; }
    .aging-header .co-sub   { font-size: 15px; font-weight: 600; color: #333; }
    .aging-header .co-range { font-size: 13px; color: #555; margin-top: 3px; }

    .aging-table {
        width: 100%;
        border-collapse: collapse;
        direction: rtl;
        font-size: 13.5px;
        margin-top: 12px;
    }
    .aging-table thead th {
        border: 1px solid #888;
        padding: 9px 14px;
        text-align: center;
        background: #fff;
        font-weight: 700;
        font-size: 14px;
        white-space: nowrap;
    }
    .aging-table tbody td {
        border-left:   1px solid #aaa;
        border-right:  1px solid #aaa;
        border-top:    none;
        border-bottom: 1px dashed #aaa;
        padding: 8px 14px;
        vertical-align: middle;
    }
    .aging-table td.col-party  { text-align: right;  font-weight: 600; font-size: 14px; }
    .aging-table td.col-num    { text-align: center; white-space: nowrap; width: 130px; }
    .aging-table td.col-haliah { text-align: center; font-weight: 700; white-space: nowrap; width: 140px; }
    .aging-table td.neg { color: #c00; }
    .aging-table td.pos { color: #060; }
    .aging-table tr.total-row td {
        border-top:    2px solid #555 !important;
        border-bottom: 2px solid #555 !important;
        background: #f0f0f0;
        font-weight: 700;
        text-align: center;
        font-size: 13px;
        padding: 9px 14px;
    }
</style>
@endpush

@section('content')

<div class="card">
    <div class="card-header">
        <h3>Aging Payables — ادائیگی ادھاری</h3>
    </div>
    <div class="card-body">

        {{-- ── Filter Form ── --}}
        <form class="row" method="GET" action="{{ route('aging-payables.index') }}">
            <div class="col-md-3">
                <div class="form-group mt-3">
                    <label>از تاریخ (From Date)</label>
                    <input type="date" name="from_date" class="form-control" value="{{ $fromDate }}">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group mt-3">
                    <label>تا تاریخ (To Date)</label>
                    <input type="date" name="to_date" class="form-control" value="{{ $toDate }}">
                </div>
            </div>
            <div class="col-md-6 d-flex align-items-end mt-3 gap-2">
                <button type="submit" class="btn btn-primary">
                    <i class="fa fa-search"></i> دیکھیں
                </button>
                @if($reportData && count($reportData['rows']))
                    <a href="{{ route('aging-payables.print', ['from_date' => $fromDate, 'to_date' => $toDate]) }}"
                       target="_blank" class="btn btn-warning">
                        <i class="fa fa-print"></i> پرنٹ
                    </a>
                @endif
            </div>
        </form>

        <hr />

        @if($reportData !== null)
            @if(empty($reportData['rows']))
                <div class="alert alert-info text-center">کوئی اندراج نہیں ملا۔</div>
            @else

                @php
                    // Negative → parentheses like the image, zero → $z param
                    $fmt = fn($v, $z = '-') => $v == 0
                        ? $z
                        : ($v < 0
                            ? '(' . number_format(abs($v), 0) . ')'
                            : 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">سابقہ بیلنس</small>
                                <strong>{{ $fmt($reportData['grandSabiqah'], '0') }}</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">خرید وغیرہ</small>
                                <strong class="text-danger">{{ number_format($reportData['grandKharid'], 0) }}</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">ادائیگی</small>
                                <strong class="text-success">{{ number_format($reportData['grandAdaegi'], 0) }}</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">حالیہ بیلنس</small>
                                <strong class="text-primary">{{ $fmt($reportData['grandHaliah'], '0') }}</strong>
                            </div>
                        </div>
                    </div>
                </div>

                {{-- Aging Table --}}
                <div class="aging-wrap">

                    <div class="aging-header">
                        <div class="co-name">M.D.S</div>
                        <div class="co-sub">ادائیگی ادھاری &nbsp;&nbsp; Aging Payables</div>
                        <div class="co-range">
                            مورخہ: {{ \Carbon\Carbon::parse($fromDate)->format('d/m/Y') }}
                            تا {{ \Carbon\Carbon::parse($toDate)->format('d/m/Y') }}
                        </div>
                    </div>

                    <table class="aging-table">
                        <thead>
                            <tr>
                                <th>پارٹی</th>
                                <th>سابقہ بیلنس</th>
                                <th>خرید وغیرہ</th>
                                <th>ادائیگی</th>
                                <th>حالیہ بیلنس</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($reportData['rows'] as $row)
                                <tr>
                                    <td class="col-party">{{ $row['supplier_name'] }}</td>

                                    <td class="col-num {{ $row['sabiqah'] < 0 ? 'neg' : '' }}">
                                        {{ $fmt($row['sabiqah']) }}
                                    </td>

                                    <td class="col-num">
                                        {{ $row['kharid'] == 0 ? '-' : number_format($row['kharid'], 0) }}
                                    </td>

                                    <td class="col-num">
                                        {{ $row['adaegi'] == 0 ? '0' : number_format($row['adaegi'], 0) }}
                                    </td>

                                    <td class="col-haliah {{ $row['haliah'] < 0 ? 'neg' : 'pos' }}">
                                        {{ $fmt($row['haliah'], '0') }}
                                    </td>
                                </tr>
                            @endforeach

                            <tr class="total-row">
                                <td>کُل مجموعہ</td>
                                <td>{{ $fmt($reportData['grandSabiqah'], '0') }}</td>
                                <td>{{ number_format($reportData['grandKharid'], 0) }}</td>
                                <td>{{ number_format($reportData['grandAdaegi'], 0) }}</td>
                                <td>{{ $fmt($reportData['grandHaliah'], '0') }}</td>
                            </tr>
                        </tbody>
                    </table>

                </div>{{-- .aging-wrap --}}
            @endif
        @endif

    </div>
</div>

@endsection