@extends('layouts.master')

@push('css')
<style>
    /* ── Report Wrapper ── */
    .purchase-wrap {
        font-family: Arial, sans-serif;
        font-size: 13px;
        direction: ltr;
    }

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

    /* ── Main Table ── */
    .purchase-table {
        width: 100%;
        border-collapse: collapse;
        font-size: 12.5px;
        margin-top: 14px;
        direction: rtl;
    }
    .purchase-table thead th {
        border: 1px solid #000;
        padding: 7px 8px;
        text-align: center;
        font-weight: 700;
        font-size: 12px;
        background: #fff;
        white-space: nowrap;
        vertical-align: bottom;
    }

    /* Solid side borders, dashed bottom */
    .purchase-table tbody td {
        border-left:   1px solid #000;
        border-right:  1px solid #000;
        border-top:    none;
        border-bottom: 1px dashed #888;
        padding: 6px 8px;
        vertical-align: middle;
    }

    /* Purchaser group header */
    .purchase-table tr.purchaser-row td {
        font-weight: 700;
        font-size: 13px;
        color: #cc0000;
        border-left:   1px solid #000;
        border-right:  1px solid #000;
        border-bottom: none;
        padding: 8px 8px 2px;
        background: #fff;
        letter-spacing: 0.5px;
        text-align: right;
    }

    /* Sub-total row */
    .purchase-table tr.subtotal-row td {
        border-top:    1px dashed #888;
        border-bottom: 1px dashed #888;
        font-size: 12px;
        color: #cc0000;
        font-weight: 600;
        padding: 5px 8px;
    }

    /* Grand Total */
    .purchase-table tr.grand-row td {
        border-top:    2px solid #000 !important;
        border-bottom: 2px solid #000 !important;
        font-weight: 700;
        font-size: 12px;
        background: #f0f0f0;
        padding: 7px 8px;
    }

    /* Summary box */
    .summary-box {
        margin-top: 20px;
        border: 1px solid #000;
        display: inline-block;
        min-width: 280px;
        direction: rtl;
    }
    .summary-box table {
        width: 100%;
        border-collapse: collapse;
    }
    .summary-box table td {
        padding: 5px 12px;
        font-size: 12.5px;
        border-bottom: 1px solid #ccc;
    }
    .summary-box table td:first-child {
        font-weight: 700;
        text-align: right;
    }
    .summary-box table td:last-child {
        text-align: left;
        font-weight: 600;
    }
    .summary-box .summary-title {
        font-weight: 700;
        font-size: 13px;
        padding: 5px 12px;
        border-bottom: 1px solid #000;
        text-align: right;
    }

    /* Number alignment */
    .purchase-table td.num { text-align: left;  white-space: nowrap; }
    .purchase-table th.num { text-align: center; }
    .purchase-table td.ctr { text-align: center; }
    .text-red { color: #cc0000; }
</style>
@endpush

@section('content')

<div class="card">
    <div class="card-header">
        <h3>Purchase Register — خرید رجسٹر</h3>
    </div>
    <div class="card-body">

        {{-- ── Filter Form ── --}}
        <form class="row" method="GET" action="{{ route('purchase-register.index') }}">
            <div class="col-md-3">
                <div class="form-group mt-2">
                    <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-2">
                    <label>Till Date</label>
                    <input type="date" name="to_date" class="form-control"
                           value="{{ $toDate }}">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group mt-2">
                    <label>Purchaser / Client</label>
                    <select name="purchaser_id" class="form-control">
                        <option value="">— All Purchasers —</option>
                        @foreach($purchasers as $p)
                            <option value="{{ $p->id_purchaser }}"
                                {{ $purchaserId == $p->id_purchaser ? 'selected' : '' }}>
                                {{ $p->purchaser_name }}
                                @if($p->purchaser_name_urdu)
                                    — {{ $p->purchaser_name_urdu }}
                                @endif
                            </option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group mt-2">
                    <label>&nbsp;</label>
                    <div class="d-flex gap-2 mt-1">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-search"></i> View
                        </button>
                        @if($reportData && count($reportData['purchasers']))
                            <a href="{{ route('purchase-register.print', [
                                    'from_date'    => $fromDate,
                                    'to_date'      => $toDate,
                                    'purchaser_id' => $purchaserId,
                                ]) }}"
                               target="_blank" class="btn btn-warning">
                                <i class="fa fa-print"></i> Print
                            </a>
                        @endif
                    </div>
                </div>
            </div>
        </form>

        <hr />

        @if($reportData !== null)
            @if(empty($reportData['purchasers']))
                <div class="alert alert-info text-center">No records found.</div>
            @else

                {{-- ── Summary Cards ── --}}
                <div class="row mb-3">
                    <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 Bori (بوری)</small>
                                <strong class="text-primary">{{ number_format($reportData['grandBori'], 0) }}</strong>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card border-info">
                            <div class="card-body py-2 text-center">
                                <small class="text-muted d-block">Total Weight (وزن)</small>
                                <strong class="text-info">{{ number_format($reportData['grandWeight'], 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">Total Amount (قیمت)</small>
                                <strong class="text-success">{{ number_format($reportData['grandAmount'], 0) }}</strong>
                            </div>
                        </div>
                    </div>
                    <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 Orders (گاڑی)</small>
                                <strong>{{ $reportData['grandOrders'] }}</strong>
                            </div>
                        </div>
                    </div>
                </div>

                {{-- ── Purchase Register Table ── --}}
                <div class="purchase-wrap">

                    {{-- Report Header --}}
                    <div class="purchase-header">
                        <div class="co-name">M.D.S</div>
                        <div class="co-sub">Purchase Register &nbsp; خرید رجسٹر</div>
                        <div class="co-range">
                            From: {{ \Carbon\Carbon::parse($fromDate)->format('d/m/y') }},
                            To: {{ \Carbon\Carbon::parse($toDate)->format('d/m/y') }}
                            @if($selectedPurchaser)
                                , Client: {{ $selectedPurchaser->purchaser_name }}
                            @endif
                        </div>
                    </div>

                    <table class="purchase-table">
                        <thead>
                            <tr>
                                <th>تاریخ</th>
                                <th>گاڑی نمبر</th>
                                <th>مال</th>
                                <th>بوری</th>
                                <th>وزن</th>
                                <th class="num">ریٹ</th>
                                <th class="num">قیمت</th>
                            </tr>
                        </thead>
                        <tbody>

                            @foreach($reportData['purchasers'] as $purchaserName => $purchaserData)

                                {{-- Purchaser Group Header --}}
                                <tr class="purchaser-row">
                                    <td colspan="7">{{ $purchaserData['urdu_name'] ?: $purchaserName }}</td>
                                </tr>

                                @foreach($purchaserData['orders'] as $order)
                                    {{-- Items within this order --}}
                                    @foreach($order['details'] as $i => $detail)
                                        <tr>
                                            <td class="ctr">{{ $i === 0 ? $order['order_date'] : '' }}</td>
                                            <td class="ctr">{{ $i === 0 ? $order['po_number'] : '' }}</td>
                                            <td>{{ $detail['name'] }}</td>
                                            <td class="ctr">{{ $detail['bori'] ?: '' }}</td>
                                            <td class="num">{{ $detail['weight'] ? number_format($detail['weight'], 0) : '' }}</td>
                                            <td class="num">{{ $detail['rate'] ? number_format($detail['rate'], 0) : '' }}</td>
                                            <td class="num">{{ $detail['amount'] ? number_format($detail['amount'], 0) : '' }}</td>
                                        </tr>
                                    @endforeach

                                    {{-- Order sub-line (labour/mzdoori if exists) --}}
                                    @if($order['labour_charges'] > 0)
                                        <tr>
                                            <td class="ctr">{{ $order['order_date'] }}</td>
                                            <td class="ctr"></td>
                                            <td>مزدوری</td>
                                            <td class="ctr"></td>
                                            <td class="num"></td>
                                            <td class="num"></td>
                                            <td class="num">{{ number_format($order['labour_charges'], 0) }}</td>
                                        </tr>
                                    @endif

                                    {{-- Order total line --}}
                                    <tr>
                                        <td colspan="2" class="ctr"></td>
                                        <td></td>
                                        <td class="ctr"></td>
                                        <td class="num">{{ number_format($order['total_weight'], 0) }}</td>
                                        <td class="num">{{ $order['total_bori'] }}</td>
                                        <td class="num" style="border-top:1px solid #000;">
                                            {{ number_format($order['grand_total'], 0) }}
                                        </td>
                                    </tr>
                                @endforeach

                                {{-- Sub Total (dashed line) --}}
                                <tr class="subtotal-row">
                                    <td colspan="3" class="text-red" style="text-align:right;">کُل</td>
                                    <td class="ctr text-red"></td>
                                    <td class="num text-red">{{ number_format($purchaserData['subWeight'], 0) }}</td>
                                    <td class="num text-red">{{ $purchaserData['subBori'] }}</td>
                                    <td class="num text-red">{{ number_format($purchaserData['subAmount'], 0) }}</td>
                                </tr>

                            @endforeach

                            {{-- Grand Total --}}
                            <tr class="grand-row">
                                <td colspan="3" style="text-align:right;">Grand Total =&gt;</td>
                                <td class="ctr"></td>
                                <td class="num">{{ number_format($reportData['grandWeight'], 0) }}</td>
                                <td class="num">{{ $reportData['grandBori'] }}</td>
                                <td class="num">{{ number_format($reportData['grandAmount'], 0) }}</td>
                            </tr>

                        </tbody>
                    </table>

                    {{-- ── Summary Box (bottom right, like in image) ── --}}
                    <div class="d-flex justify-content-end mt-3">
                        <div class="summary-box">
                            <div class="summary-title">کُل</div>
                            <table>
                                <tr>
                                    <td>بوری</td>
                                    <td>{{ number_format($reportData['grandBori'], 0) }}</td>
                                </tr>
                                <tr>
                                    <td>وزن</td>
                                    <td>{{ number_format($reportData['grandWeight'], 0) }}</td>
                                </tr>
                                <tr>
                                    <td>قیمت</td>
                                    <td>{{ number_format($reportData['grandAmount'], 0) }}</td>
                                </tr>
                                <tr>
                                    <td>گاڑی</td>
                                    <td>{{ $reportData['grandOrders'] }}</td>
                                </tr>
                            </table>
                        </div>
                    </div>

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

            @endif
        @endif

    </div>
</div>

@endsection