<?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>
        /* ── Ledger Table Styles ── */
        .ledger-wrap {
            direction: rtl;
            font-family: 'Noto Nastaliq Urdu', 'Cairo', serif;
        }
        .ledger-company {
            text-align: center;
            margin-bottom: 10px;
        }
        .ledger-company .co-name {
            font-size: 22px;
            font-weight: 700;
            letter-spacing: 2px;
        }
        .ledger-company .co-sub {
            font-size: 15px;
            color: #333;
        }
        .ledger-khata-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
            font-size: 14px;
            font-weight: 600;
        }
        .ledger-table {
            width: 100%;
            border-collapse: collapse;
            direction: rtl;
            font-size: 13.5px;
        }
        .ledger-table thead th {
            border: 1px solid #999;
            padding: 9px 12px;
            text-align: center;
            background: #fff;
            font-weight: 700;
            font-size: 14px;
            color: #000;
            white-space: nowrap;
        }
        .ledger-table tbody td {
            border-left: 1px solid #bbb;
            border-right: 1px solid #bbb;
            border-top: none;
            border-bottom: 1px dashed #aaa;
            padding: 9px 12px;
            vertical-align: middle;
        }
        .ledger-table td.col-date    { text-align: center; white-space: nowrap; width: 90px; }
        .ledger-table td.col-detail  { text-align: right; font-weight: 600; font-size: 14px; }
        .ledger-table td.col-amount  { text-align: center; white-space: nowrap; width: 120px; }
        .ledger-table td.col-balance { text-align: center; font-weight: 700; white-space: nowrap; width: 130px; }
        .ledger-table td.col-drcr    { text-align: center; font-weight: 700; font-size: 12px; width: 40px; }
        .ledger-table tr.opening-row td {
            text-align: center !important;
            font-weight: 700;
            font-size: 14px;
            background: #fafafa;
        }
        .ledger-table tr.total-row td {
            border-top: 2px solid #888 !important;
            background: #f0f0f0;
            font-weight: 700;
            text-align: center;
            font-size: 13px;
            padding: 8px 12px;
        }
        /* Badge colours for type indicator */
        .badge-supplier { background: #6c757d; }
        .badge-customer { background: #0d6efd; }
    </style>
<?php $__env->stopPush(); ?>

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

    
    <div class="card">
        <div class="card-header">
            <h3>General Ledger</h3>
        </div>
        <div class="card-body">

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

            <form id="filterForm" class="row" method="GET" action="<?php echo e(route('general-ledger.index')); ?>">

                <div class="row">

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

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

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

                                <optgroup label="━━ سپلائرز (Suppliers) ━━">
                                    <?php $__currentLoopData = $khataList; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $kid => $kname): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <?php if(str_starts_with($kid, 'S_')): ?>
                                            <option value="<?php echo e($kid); ?>"
                                                <?php echo e($selectedKhata === $kid ? 'selected' : ''); ?>>
                                                <?php echo e($kname); ?>

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

                                <optgroup label="━━ کسٹمرز (Customers) ━━">
                                    <?php $__currentLoopData = $khataList; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $kid => $kname): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <?php if(str_starts_with($kid, 'C_')): ?>
                                            <option value="<?php echo e($kid); ?>"
                                                <?php echo e($selectedKhata === $kid ? 'selected' : ''); ?>>
                                                <?php echo e($kname); ?>

                                            </option>
                                        <?php endif; ?>
                                    <?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($ledgerData !== null): ?>
                            &nbsp;
                            <a href="<?php echo e(route('general-ledger.print', ['khata' => $selectedKhata, 'from_date' => $fromDate, 'to_date' => $toDate])); ?>"
                               target="_blank" class="btn btn-warning">
                                <i class="fa fa-print"></i> پرنٹ
                            </a>
                        <?php endif; ?>
                    </div>
                </div>

            </form>

            <hr />

            
            <?php if($ledgerData !== null): ?>

                <?php if(count($ledgerData) <= 1): ?>
                    <div class="alert alert-info text-center">
                        اس کھاتے کے لیے کوئی اندراج نہیں ملا۔
                    </div>
                <?php else: ?>

                    
                    <div class="mb-2 text-center">
                        <?php if($khataType === 'customer'): ?>
                            <span class="badge bg-primary px-3 py-2" style="font-size:13px">
                                <i class="fa fa-user"></i> کسٹمر کھاتہ (Customer Ledger)
                            </span>
                        <?php else: ?>
                            <span class="badge bg-secondary px-3 py-2" style="font-size:13px">
                                <i class="fa fa-truck"></i> سپلائر کھاتہ (Supplier Ledger)
                            </span>
                        <?php endif; ?>
                    </div>

                    
                    <div class="row mb-3">
                        <div class="col-md-4">
                            <div class="card border-danger">
                                <div class="card-body py-2 text-center">
                                    <small class="text-muted d-block">کل ڈیبٹ (Total Debit)</small>
                                    <strong class="text-danger fs-5"><?php echo e(number_format($totalDebit, 0)); ?></strong>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card border-success">
                                <div class="card-body py-2 text-center">
                                    <small class="text-muted d-block">کل کریڈٹ (Total Credit)</small>
                                    <strong class="text-success fs-5"><?php echo e(number_format($totalCredit, 0)); ?></strong>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card border-primary">
                                <div class="card-body py-2 text-center">
                                    <small class="text-muted d-block">بقایا (Balance)</small>
                                    <strong class="text-primary fs-5">
                                        <?php echo e(number_format(abs($totalCredit - $totalDebit), 0)); ?>

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

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

                    
                    <div class="ledger-wrap">

                        <div class="ledger-company">
                            <div class="co-name">M.D.S</div>
                            <div class="co-sub">
                                <?php echo e($khataType === 'customer' ? 'Customer Ledger' : 'Supplier Ledger'); ?>

                            </div>
                        </div>

                        <div class="ledger-khata-row">
                            <div>کھاتہ &nbsp; <?php echo e($khataList[$selectedKhata] ?? $selectedKhata); ?></div>
                            <div>کھا تہ رجسٹر</div>
                            <div>کھاتہ کوڈ &nbsp; <?php echo e($selectedKhata); ?></div>
                        </div>

                        <table class="ledger-table">
                            <thead>
                                <tr>
                                    <th>تاریخ</th>
                                    <th>تفصیل</th>
                                    <th>ڈ یبٹ رقم</th>
                                    <th>کریڈٹ رقم</th>
                                    <th colspan="2">میزان</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $ledgerData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <?php if($row['is_opening']): ?>
                                        
                                    <?php else: ?>
                                        <tr>
                                            <td class="col-date"><?php echo e($row['date']); ?></td>
                                            <td class="col-detail"><?php echo e($row['detail']); ?></td>
                                            <td class="col-amount">
                                                <?php echo e($row['debit'] ? number_format($row['debit'], 0) : ''); ?>

                                            </td>
                                            <td class="col-amount">
                                                <?php echo e($row['credit'] ? number_format($row['credit'], 0) : ''); ?>

                                            </td>
                                            <td class="col-balance">
                                                <?php echo e(number_format($row['balance'], 0)); ?>

                                            </td>
                                            <td class="col-drcr"><?php echo e($row['dr_cr']); ?></td>
                                        </tr>
                                    <?php endif; ?>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                                <tr class="total-row">
                                    <td colspan="2">کُل</td>
                                    <td><?php echo e(number_format($totalDebit,  0)); ?></td>
                                    <td><?php echo e(number_format($totalCredit, 0)); ?></td>
                                    <td colspan="2"></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/general_ledger.blade.php ENDPATH**/ ?>