@extends('layouts.master')

@push('css')
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <style>
        /* ── Trial Balance Styles ── */
        .tb-wrap {
            font-family: 'Segoe UI', Tahoma, sans-serif;
            font-size: 13px;
        }

        /* Company header */
        .tb-company {
            text-align: center;
            margin-bottom: 10px;
        }
        .tb-company .co-name {
            font-size: 22px;
            font-weight: 700;
            letter-spacing: 2px;
        }
        .tb-company .co-sub {
            font-size: 15px;
            color: #333;
        }

        /* Date / title row */
        .tb-header-row {
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            margin-bottom: 6px;
            font-size: 13px;
            border-bottom: 1px solid #ccc;
            padding-bottom: 4px;
        }
        .tb-header-row .tb-urdu-title {
            font-size: 18px;
            font-weight: 700;
            color: #1a1a1a;
            direction: rtl;
        }

        /* Account range row */
        .tb-range-row {
            font-size: 12px;
            color: #444;
            margin-bottom: 4px;
        }

        /* Main table */
        .tb-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 13px;
        }

        /* Outer border */
        .tb-table,
        .tb-table th,
        .tb-table td {
            border: 1px solid #999;
        }

        .tb-table thead tr th {
            background: #fff;
            font-weight: 700;
            text-align: center;
            padding: 7px 10px;
            white-space: nowrap;
        }
        .tb-table thead tr.th-group th {
            border-bottom: 1px solid #999;
        }
        .tb-table thead tr.th-sub th {
            font-size: 12px;
            padding: 5px 8px;
        }

        .tb-table tbody td {
            padding: 6px 10px;
            vertical-align: middle;
        }

        /* A/C # col */
        .tb-table td.col-ac,
        .tb-table th.col-ac {
            width: 130px;
            white-space: nowrap;
        }
        /* Title col */
        .tb-table td.col-title {
            text-align: right;
            direction: rtl;
            font-size: 13.5px;
        }
        /* Amount cols */
        .tb-table td.col-amt {
            text-align: right;
            white-space: nowrap;
            width: 110px;
            padding-right: 12px;
        }
        .tb-table td.col-amt-empty {
            width: 110px;
        }

        /* Category heading rows (ASSETS / DEBTORS etc.) */
        .tb-table tr.row-category td {
            font-weight: 700;
            font-size: 14px;
            color: #1565C0;
            text-align: right;
            direction: rtl;
            background: #fff;
            border-top: none;
            border-bottom: none;
            padding: 10px 10px 4px;
        }
        .tb-table tr.row-category td.col-ac {
            color: #1565C0;
            text-align: left;
            font-size: 13px;
        }

        /* Sub-category heading rows */
        .tb-table tr.row-subcategory td {
            font-weight: 700;
            font-size: 13.5px;
            color: #2E7D32;
            text-align: right;
            direction: rtl;
            background: #fff;
            border-top: none;
            border-bottom: none;
            padding: 8px 10px 3px;
        }
        .tb-table tr.row-subcategory td.col-ac {
            color: #1565C0;
            text-align: left;
        }

        /* Total rows */
        .tb-table tr.row-total td {
            font-weight: 700;
            background: #f5f5f5;
            border-top: 2px solid #666;
            text-align: right;
            padding-right: 12px;
        }
        .tb-table tr.row-total td.col-ac,
        .tb-table tr.row-total td.col-title {
            text-align: left;
            direction: ltr;
        }
        .tb-table tr.row-grand-total td {
            font-weight: 700;
            font-size: 13px;
            background: #e8e8e8;
            border-top: 2px solid #333;
            border-bottom: 2px solid #333;
            text-align: right;
            padding: 8px 12px;
        }
        .tb-table tr.row-grand-total td.col-ac,
        .tb-table tr.row-grand-total td.col-title {
            text-align: left;
            direction: ltr;
        }

        /* Summary cards */
        .summary-cards {
            display: flex;
            gap: 15px;
            margin-bottom: 18px;
        }
        .summary-card {
            flex: 1;
            border-radius: 6px;
            border: 1px solid #ddd;
            padding: 10px 14px;
            text-align: center;
        }
        .summary-card small { display: block; font-size: 11px; color: #888; margin-bottom: 3px; }
        .summary-card strong { font-size: 16px; }
    </style>
@endpush

@section('content')

    {{-- ════════ FILTER CARD ════════ --}}
    <div class="card">
        <div class="card-header">
            <h3>Trial Balance &nbsp; <span style="font-size:14px;font-weight:normal;color:#666;">کُل کھاتہ میزان</span></h3>
        </div>
        <div class="card-body">

            @if ($errors->any())
                <div style="border:1px solid red;padding:10px;margin-bottom:20px;background:#fdd;">
                    <strong>Errors:</strong>
                    <ul>@foreach($errors->all() as $e)<li>{{ $e }}</li>@endforeach</ul>
                </div>
            @endif

            <form id="filterForm" class="row" method="GET" action="{{ route('trialBalance.report') }}">

                <div class="row">

                    {{-- From Date --}}
                    <div class="col-md-2">
                        <div class="form-group mt-3">
                            <label for="from_date">از تاریخ (From Date)</label>
                            <input type="date" name="from_date" id="from_date"
                                   class="form-control"
                                   value="{{ request('from_date') }}">
                        </div>
                    </div>

                    {{-- To Date --}}
                    <div class="col-md-2">
                        <div class="form-group mt-3">
                            <label for="to_date">تا تاریخ (To Date)</label>
                            <input type="date" name="to_date" id="to_date"
                                   class="form-control"
                                   value="{{ request('to_date') }}">
                        </div>
                    </div>

                    {{-- From Customer (Khata) --}}
                    <div class="col-md-4">
                        <div class="form-group mt-3">
                            <label for="from_customer">از کھاتہ (From Account)</label>
                            <select id="from_customer" name="from_customer" class="form-control select2">
                                <option value="">— سب سے پہلا —</option>
                                @foreach($supplierList as $sid => $sname)
                                    <option value="{{ $sid }}"
                                        {{ (string)request('from_customer') === (string)$sid ? 'selected' : '' }}>
                                        {{ $sid }} — {{ $sname }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                    </div>

                    {{-- To Customer (Khata) --}}
                    <div class="col-md-4">
                        <div class="form-group mt-3">
                            <label for="to_customer">تا کھاتہ (To Account)</label>
                            <select id="to_customer" name="to_customer" class="form-control select2">
                                <option value="">— سب سے آخری —</option>
                                @foreach($supplierList as $sid => $sname)
                                    <option value="{{ $sid }}"
                                        {{ (string)request('to_customer') === (string)$sid ? 'selected' : '' }}>
                                        {{ $sid }} — {{ $sname }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                    </div>

                </div>{{-- .row --}}

                <div class="row mt-4">
                    <div class="col-md-12 text-center">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-search"></i> دیکھیں
                        </button>
                        @if(isset($trialData) && $trialData !== null)
                            &nbsp;
                            <a href="{{ route('trialBalance.report.print', array_merge(request()->only(['from_date','to_date','from_customer','to_customer']))) }}"
                               target="_blank" class="btn btn-warning">
                                <i class="fa fa-print"></i> پرنٹ
                            </a>
                        @endif
                    </div>
                </div>

            </form>

            <hr />

            {{-- ════════ RESULT ════════ --}}
            @if(isset($trialData) && $trialData !== null)

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

                    {{-- Summary Cards --}}
                    <div class="summary-cards">
                        <div class="summary-card" style="border-color:#e57373;">
                            <small>کل ڈیبٹ (Total Debit)</small>
                            <strong class="text-danger">{{ number_format($grandTotalDebit, 0) }}</strong>
                        </div>
                        <div class="summary-card" style="border-color:#81c784;">
                            <small>کل کریڈٹ (Total Credit)</small>
                            <strong class="text-success">{{ number_format($grandTotalCredit, 0) }}</strong>
                        </div>
                        <div class="summary-card" style="border-color:#64b5f6;">
                            <small>فرق (Difference)</small>
                            <strong class="text-primary">
                                {{ number_format(abs($grandTotalDebit - $grandTotalCredit), 0) }}
                                @if($grandTotalDebit != $grandTotalCredit)
                                    <span class="badge {{ ($grandTotalDebit - $grandTotalCredit) > 0 ? 'bg-danger' : 'bg-success' }} ms-1" style="font-size:10px">
                                        {{ ($grandTotalDebit - $grandTotalCredit) > 0 ? 'Dr' : 'Cr' }}
                                    </span>
                                @endif
                            </strong>
                        </div>
                    </div>

                    {{-- Trial Balance Table --}}
                    <div class="tb-wrap">

                        {{-- Company Header --}}
                        <div class="tb-company">
                            <div class="co-name">M.D.S</div>
                            <div class="co-sub">Trial Balance</div>
                        </div>

                        {{-- Header Row --}}
                        <div class="tb-header-row">
                            <div>
                                As From &nbsp;
                                <strong>{{ $fromDate ? \Carbon\Carbon::parse($fromDate)->format('d/m/Y') : '/' }}</strong>
                                &nbsp; To &nbsp;
                                <strong>{{ $toDate ? \Carbon\Carbon::parse($toDate)->format('d/m/Y') : '/' }}</strong>
                            </div>
                            <div class="tb-urdu-title">کُل کھاتہ میزان</div>
                        </div>

                        {{-- Account Range --}}
                        <div class="tb-range-row">
                            Account Nos.:
                            <strong>{{ $fromCustomer ?? '—' }}</strong>
                            Till
                            <strong>{{ $toCustomer ?? '—' }}</strong>
                        </div>

                        <table class="tb-table">
                            <thead>
                                {{-- Group headers --}}
                                <tr class="th-group">
                                    <th class="col-ac" rowspan="2">A/C #</th>
                                    <th rowspan="2" style="text-align:right;direction:rtl;">Title</th>
                                    <th colspan="2">Opening</th>
                                    <th colspan="2">Current</th>
                                    <th colspan="2">Balance</th>
                                </tr>
                                <tr class="th-sub">
                                    <th>Debit</th>
                                    <th>Credit</th>
                                    <th>Debit</th>
                                    <th>Credit</th>
                                    <th>Debit</th>
                                    <th>Credit</th>
                                </tr>
                            </thead>
                            <tbody>

                                @foreach($trialData as $row)

                                    @if($row['type'] === 'category')
                                        {{-- e.g. ASSETS / LIABILITIES --}}
                                        <tr class="row-category">
                                            <td class="col-ac">{{ $row['ac'] }}</td>
                                            <td colspan="7" class="col-title" style="font-size:15px;color:#1565C0;">
                                                {{ $row['title'] }}
                                            </td>
                                        </tr>

                                    @elseif($row['type'] === 'subcategory')
                                        {{-- e.g. DEBTORS / CREDITORS --}}
                                        <tr class="row-subcategory">
                                            <td class="col-ac">{{ $row['ac'] }}</td>
                                            <td colspan="7" class="col-title" style="font-size:14px;color:#2E7D32;">
                                                {{ $row['title'] }}
                                            </td>
                                        </tr>

                                    @elseif($row['type'] === 'account')
                                        {{-- Leaf account --}}
                                        <tr>
                                            <td class="col-ac" style="font-size:12px;">{{ $row['ac'] }}</td>
                                            <td class="col-title">{{ $row['title'] }}</td>

                                            {{-- Opening --}}
                                            <td class="col-amt">{{ $row['opening_debit']  ? number_format($row['opening_debit'],  0) : '' }}</td>
                                            <td class="col-amt">{{ $row['opening_credit'] ? number_format($row['opening_credit'], 0) : '' }}</td>

                                            {{-- Current --}}
                                            <td class="col-amt">{{ $row['current_debit']  ? number_format($row['current_debit'],  0) : '' }}</td>
                                            <td class="col-amt">{{ $row['current_credit'] ? number_format($row['current_credit'], 0) : '' }}</td>

                                            {{-- Balance --}}
                                            <td class="col-amt">{{ $row['balance_debit']  ? number_format($row['balance_debit'],  0) : '' }}</td>
                                            <td class="col-amt">{{ $row['balance_credit'] ? number_format($row['balance_credit'], 0) : '' }}</td>
                                        </tr>

                                    @elseif($row['type'] === 'subtotal')
                                        <tr class="row-total">
                                            <td class="col-ac"></td>
                                            <td class="col-title" style="text-align:right;direction:rtl;font-size:12px;">
                                                {{ $row['title'] }} کُل
                                            </td>
                                            <td class="col-amt">{{ $row['opening_debit']  ? number_format($row['opening_debit'],  0) : '' }}</td>
                                            <td class="col-amt">{{ $row['opening_credit'] ? number_format($row['opening_credit'], 0) : '' }}</td>
                                            <td class="col-amt">{{ $row['current_debit']  ? number_format($row['current_debit'],  0) : '' }}</td>
                                            <td class="col-amt">{{ $row['current_credit'] ? number_format($row['current_credit'], 0) : '' }}</td>
                                            <td class="col-amt">{{ $row['balance_debit']  ? number_format($row['balance_debit'],  0) : '' }}</td>
                                            <td class="col-amt">{{ $row['balance_credit'] ? number_format($row['balance_credit'], 0) : '' }}</td>
                                        </tr>

                                    @endif

                                @endforeach

                                {{-- Grand Total --}}
                                <tr class="row-grand-total">
                                    <td class="col-ac" colspan="2">Grand Total &nbsp; کُل مجموعہ</td>
                                    <td class="col-amt">{{ $grandOpeningDebit  ? number_format($grandOpeningDebit,  0) : '' }}</td>
                                    <td class="col-amt">{{ $grandOpeningCredit ? number_format($grandOpeningCredit, 0) : '' }}</td>
                                    <td class="col-amt">{{ number_format($grandTotalDebit,  0) }}</td>
                                    <td class="col-amt">{{ number_format($grandTotalCredit, 0) }}</td>
                                    <td class="col-amt">{{ $grandBalanceDebit  ? number_format($grandBalanceDebit,  0) : '' }}</td>
                                    <td class="col-amt">{{ $grandBalanceCredit ? number_format($grandBalanceCredit, 0) : '' }}</td>
                                </tr>

                            </tbody>
                        </table>

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

                @endif

            @endif
            {{-- end results --}}

        </div>{{-- .card-body --}}
    </div>{{-- .card --}}

@endsection

@push('scripts')
    <script>
        $(document).ready(function () {
            $('.select2').select2();
            $(".date").datepicker({ dateFormat: "yy-mm-dd" });
        });
    </script>
@endpush
