<?php $__env->startPush('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;
        }

        .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; }

        .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;
        }

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

        .tb-table { width: 100%; border-collapse: collapse; font-size: 13px; }
        .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-sub th { font-size: 12px; padding: 5px 8px; }

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

        .tb-table td.col-ac, .tb-table th.col-ac { width: 110px; white-space: nowrap; }
        .tb-table td.col-title { text-align: right; direction: rtl; font-size: 13.5px; }
        .tb-table td.col-amt   { text-align: right; white-space: nowrap; width: 110px; padding-right: 12px; }

        /* ── Category heading (Suppliers / Customers) ── */
        .tb-table tr.row-category td {
            font-weight: 700;
            font-size: 14px;
            background: #f0f4ff;
            color: #1565C0;
            text-align: right;
            direction: rtl;
            border-top: 2px solid #1565C0;
            border-bottom: 1px solid #aac;
            padding: 9px 12px 6px;
        }
        .tb-table tr.row-category td.col-ac {
            text-align: left;
            direction: ltr;
            font-size: 12px;
        }

        /* ── Sub-category heading ── */
        .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; }

        /* ── Sub-total row ── */
        .tb-table tr.row-total td {
            font-weight: 700; background: #f5f5f5;
            border-top: 2px solid #666 !important;
            text-align: right; padding-right: 12px;
        }
        .tb-table tr.row-total td.col-ac,
        .tb-table tr.row-total td.col-title { text-align: right; direction: rtl; }

        /* ── Grand total row ── */
        .tb-table tr.row-grand-total td {
            font-weight: 700; font-size: 13px; background: #e8e8e8;
            border-top: 2px solid #333 !important;
            border-bottom: 2px solid #333 !important;
            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>
<?php $__env->stopPush(); ?>

<?php $__env->startSection('content'); ?>

    <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">

            <?php if($errors->any()): ?>
                <div style="border:1px solid red;padding:10px;margin-bottom:20px;background:#fdd;">
                    <strong>Errors:</strong>
                    <ul><?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $e): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><li><?php echo e($e); ?></li><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></ul>
                </div>
            <?php endif; ?>

            <form id="filterForm" class="row" method="GET" action="<?php echo e(route('trialBalance.report')); ?>">

                <div class="row">

                    
                    <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="<?php echo e(request('from_date')); ?>">
                        </div>
                    </div>

                    
                    <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="<?php echo e(request('to_date')); ?>">
                        </div>
                    </div>

                    
                    <div class="col-md-4">
                        <div class="form-group mt-3">
                            <label for="from_customer">
                                از کھاتہ (From Account)
                                <small class="text-muted">(S)=سپلائر &nbsp; (C)=کسٹمر</small>
                            </label>
                            <select id="from_customer" name="from_customer" class="form-control select2">
                                <option value="">— سب سے پہلا —</option>

                                <optgroup label="━━ سپلائرز (Suppliers) ━━">
                                    <?php $__currentLoopData = $khataList['suppliers']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $sid => $sname): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($sid); ?>"
                                            <?php echo e((string)request('from_customer') === (string)$sid ? 'selected' : ''); ?>>
                                            <?php echo e($sid); ?> — (S) <?php echo e($sname); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </optgroup>

                                <optgroup label="━━ کسٹمرز (Customers) ━━">
                                    <?php $__currentLoopData = $khataList['customers']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cid => $cname): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($cid); ?>"
                                            <?php echo e((string)request('from_customer') === (string)$cid ? 'selected' : ''); ?>>
                                            <?php echo e($cid); ?> — (C) <?php echo e($cname); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </optgroup>

                            </select>
                        </div>
                    </div>

                    
                    <div class="col-md-4">
                        <div class="form-group mt-3">
                            <label for="to_customer">
                                تا کھاتہ (To Account)
                                <small class="text-muted">(S)=سپلائر &nbsp; (C)=کسٹمر</small>
                            </label>
                            <select id="to_customer" name="to_customer" class="form-control select2">
                                <option value="">— سب سے آخری —</option>

                                <optgroup label="━━ سپلائرز (Suppliers) ━━">
                                    <?php $__currentLoopData = $khataList['suppliers']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $sid => $sname): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($sid); ?>"
                                            <?php echo e((string)request('to_customer') === (string)$sid ? 'selected' : ''); ?>>
                                            <?php echo e($sid); ?> — (S) <?php echo e($sname); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </optgroup>

                                <optgroup label="━━ کسٹمرز (Customers) ━━">
                                    <?php $__currentLoopData = $khataList['customers']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cid => $cname): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($cid); ?>"
                                            <?php echo e((string)request('to_customer') === (string)$cid ? 'selected' : ''); ?>>
                                            <?php echo e($cid); ?> — (C) <?php echo e($cname); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </optgroup>

                            </select>
                        </div>
                    </div>

                </div>

                <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>
                        <?php if(isset($trialData) && $trialData !== null): ?>
                            &nbsp;
                            <a href="<?php echo e(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>
                        <?php endif; ?>
                    </div>
                </div>

            </form>

            <hr />

            
            <?php if(isset($trialData) && $trialData !== null): ?>

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

                    
                    <div class="summary-cards">
                        <div class="summary-card" style="border-color:#e57373;">
                            <small>کل ڈیبٹ (Total Debit)</small>
                            <strong class="text-danger"><?php echo e(number_format($grandTotalDebit, 0)); ?></strong>
                        </div>
                        <div class="summary-card" style="border-color:#81c784;">
                            <small>کل کریڈٹ (Total Credit)</small>
                            <strong class="text-success"><?php echo e(number_format($grandTotalCredit, 0)); ?></strong>
                        </div>
                        <div class="summary-card" style="border-color:#64b5f6;">
                            <small>فرق (Difference)</small>
                            <strong class="text-primary">
                                <?php echo e(number_format(abs($grandTotalDebit - $grandTotalCredit), 0)); ?>

                                <?php if($grandTotalDebit != $grandTotalCredit): ?>
                                    <span class="badge <?php echo e(($grandTotalDebit - $grandTotalCredit) > 0 ? 'bg-danger' : 'bg-success'); ?> ms-1" style="font-size:10px">
                                        <?php echo e(($grandTotalDebit - $grandTotalCredit) > 0 ? 'Dr' : 'Cr'); ?>

                                    </span>
                                <?php endif; ?>
                            </strong>
                        </div>
                    </div>

                    
                    <div class="tb-wrap">

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

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

                        <div class="tb-range-row">
                            Account Nos.:
                            <strong><?php echo e($fromCustomer ?? '—'); ?></strong>
                            Till
                            <strong><?php echo e($toCustomer ?? '—'); ?></strong>
                        </div>

                        <table class="tb-table">
                            <thead>
                                <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>

                                <?php $__currentLoopData = $trialData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                                    <?php if($row['type'] === 'category'): ?>
                                        
                                        <tr class="row-category">
                                            <td class="col-ac"></td>
                                            <td colspan="7" style="font-size:15px; font-weight:800; color:#1565C0; text-align:right; direction:rtl;">
                                                <?php echo e($row['title']); ?>

                                            </td>
                                        </tr>

                                    <?php elseif($row['type'] === 'subcategory'): ?>
                                        <tr class="row-subcategory">
                                            <td class="col-ac"><?php echo e($row['ac']); ?></td>
                                            <td colspan="7" style="font-size:14px;color:#2E7D32;">
                                                <?php echo e($row['title']); ?>

                                            </td>
                                        </tr>

                                    <?php elseif($row['type'] === 'account'): ?>
                                        <tr>
                                            <td class="col-ac" style="font-size:12px;"><?php echo e($row['ac']); ?></td>
                                            <td class="col-title"><?php echo e($row['title']); ?></td>
                                            <td class="col-amt"><?php echo e($row['opening_debit']  ? number_format($row['opening_debit'],  0) : ''); ?></td>
                                            <td class="col-amt"><?php echo e($row['opening_credit'] ? number_format($row['opening_credit'], 0) : ''); ?></td>
                                            <td class="col-amt"><?php echo e($row['current_debit']  ? number_format($row['current_debit'],  0) : ''); ?></td>
                                            <td class="col-amt"><?php echo e($row['current_credit'] ? number_format($row['current_credit'], 0) : ''); ?></td>
                                            <td class="col-amt"><?php echo e($row['balance_debit']  ? number_format($row['balance_debit'],  0) : ''); ?></td>
                                            <td class="col-amt"><?php echo e($row['balance_credit'] ? number_format($row['balance_credit'], 0) : ''); ?></td>
                                        </tr>

                                    <?php 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;">
                                                <?php echo e($row['title']); ?> — کُل
                                            </td>
                                            <td class="col-amt"><?php echo e($row['opening_debit']  ? number_format($row['opening_debit'],  0) : ''); ?></td>
                                            <td class="col-amt"><?php echo e($row['opening_credit'] ? number_format($row['opening_credit'], 0) : ''); ?></td>
                                            <td class="col-amt"><?php echo e($row['current_debit']  ? number_format($row['current_debit'],  0) : ''); ?></td>
                                            <td class="col-amt"><?php echo e($row['current_credit'] ? number_format($row['current_credit'], 0) : ''); ?></td>
                                            <td class="col-amt"><?php echo e($row['balance_debit']  ? number_format($row['balance_debit'],  0) : ''); ?></td>
                                            <td class="col-amt"><?php echo e($row['balance_credit'] ? number_format($row['balance_credit'], 0) : ''); ?></td>
                                        </tr>

                                    <?php endif; ?>

                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

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

                            </tbody>
                        </table>

                    </div>

                <?php endif; ?>

            <?php endif; ?>

        </div>
    </div>

<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
    <script>
        $(document).ready(function () {
            $('.select2').select2();
            $(".date").datepicker({ dateFormat: "yy-mm-dd" });
        });
    </script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/mdskssye/crm.mdskarachi.com/resources/views/reports/trial_balance.blade.php ENDPATH**/ ?>