<?php $__env->startSection('title', 'GL Transaction Report'); ?>
<?php $__env->startSection('content'); ?>

<div class="container-fluid py-3">
    <div class="d-flex justify-content-end mb-3">
        <a href="<?php echo e(route('home')); ?>" class="btn-back">
            <i class="fas fa-arrow-left me-2"></i> Back to Dashboard
        </a>
    </div>

    <div class="card shadow-sm">
        <div class="card-header bg-primary text-white py-3">
            <h5 class="mb-0"><i class="fas fa-file-invoice-dollar me-2"></i> GL Transaction Report</h5>
        </div>
        
        <div class="card-body">
            <?php if(session('success')): ?>
                <div class="alert alert-success"><?php echo e(session('success')); ?></div>
            <?php endif; ?>
            
            <?php if($errors->any()): ?>
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        <?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 action="<?php echo e(route('glTransaction.generate')); ?>" method="POST" id="transactionForm">
                <?php echo csrf_field(); ?>
                <div class="row mb-3">
                    <div class="col-md-3">
                        <label class="form-label"><strong>GL Account From</strong></label>
                        <select class="form-control select2" name="gl_account_from" id="gl_account_from" required>
                            <option value="" disabled selected>Select Account</option>
                            <?php $__currentLoopData = $accounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $account): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($account->id); ?>" <?php echo e((isset($accountFrom) && $accountFrom == $account->id) ? 'selected' : ''); ?>>
                                    <?php echo e($account->code); ?> - <?php echo e($account->account_name); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label"><strong>GL Account To</strong></label>
                        <select class="form-control select2" name="gl_account_to" id="gl_account_to" required>
                            <option value="" disabled selected>Select Account</option>
                            <?php $__currentLoopData = $accounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $account): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($account->id); ?>" <?php echo e((isset($accountTo) && $accountTo == $account->id) ? 'selected' : ''); ?>>
                                    <?php echo e($account->code); ?> - <?php echo e($account->account_name); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label"><strong>Date From</strong></label>
                        <input type="date" class="form-control" name="date_from" id="date_from" value="<?php echo e(isset($dateFrom) ? $dateFrom : ''); ?>" required>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label"><strong>Date To</strong></label>
                        <input type="date" class="form-control" name="date_to" id="date_to" value="<?php echo e(isset($dateTo) ? $dateTo : ''); ?>" required>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label"><strong>Currency</strong></label>
                        <select class="form-control select2" name="currency" id="currency" required>
                            <option value="" disabled selected>Select Currency</option>
                            <?php $__currentLoopData = $currencies; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $currency): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($currency->code); ?>" <?php echo e((isset($currencyCode) && $currencyCode == $currency->code) ? 'selected' : ''); ?>>
                                    <?php echo e($currency->code); ?> - <?php echo e($currency->name); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                </div>
                
                <div class="d-flex justify-content-end">
                    <button type="submit" class="btn btn-primary me-2">Generate Report</button>
                    <button type="reset" class="btn btn-secondary" onclick="resetForm()">Refresh</button>
                </div>
            </form>
        </div>
    </div>
    
    <?php if(isset($transactionData) && count($transactionData) > 0): ?>
        <!-- PDF Download Button -->
        <div class="d-flex justify-content-end mt-4">
            <form action="<?php echo e(route('glTransaction.downloadPdf')); ?>" method="POST" class="d-inline">
                <?php echo csrf_field(); ?>
                <input type="hidden" name="gl_account_from" value="<?php echo e($accountFrom); ?>">
                <input type="hidden" name="gl_account_to" value="<?php echo e($accountTo); ?>">
                <input type="hidden" name="date_from" value="<?php echo e($dateFrom); ?>">
                <input type="hidden" name="date_to" value="<?php echo e($dateTo); ?>">
                <input type="hidden" name="currency" value="<?php echo e($currencyCode); ?>">
                <button type="submit" class="btn btn-process-all btn-sm">
                    <i class="fas fa-download me-1"></i> DOWNLOAD PDF
                </button>
            </form>
        </div>
        
        <!-- Display Transactions -->
        <div class="card shadow-sm mt-4">
            <div class="card-header bg-light py-3 d-flex justify-content-between align-items-center">
                <h6 class="mb-0"><i class="fas fa-list me-2"></i> GL Transactions</h6>
            </div>
            <div class="card-body">
                <?php
                    $grandTotalOpening = 0;
                    $grandTotalDr = 0;
                    $grandTotalCr = 0;
                    $grandTotalClosing = 0;
                ?>
                
                <?php $__currentLoopData = $transactionData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $typeId => $typeData): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <?php if(count($typeData['accounts']) > 0): ?>
                        <div class="account-type-section mb-4">
                            <h5 class="mb-3">
                                <strong><?php echo e($typeData['type']->type_code); ?> - <?php echo e($typeData['type']->type_name); ?></strong>
                            </h5>
                            
                            <?php $__currentLoopData = $typeData['accounts']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $accountId => $accountData): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <div class="account-section mb-4">
                                    <h6 class="mb-3">
                                        <strong><?php echo e($accountData['account']->code); ?> - <?php echo e($accountData['account']->account_name); ?></strong>
                                    </h6>
                                    
                                    <div class="table-responsive mb-3">
                                        <table class="table table-bordered table-hover">
                                            <thead class="table-light">
                                                <tr>
                                                    <th>Date</th>
                                                    <th>Description</th>
                                                    <th>Reference</th>
                                                    <th>DR</th>
                                                    <th>CR</th>
                                                    <th>Balance</th>
                                                </tr>
                                                <tr class="table-info">
                                                    <td colspan="3" class="text-end"><strong>Opening Balance:</strong></td>
                                                    <td colspan="3" class="text-end"><strong><?php echo e(number_format($accountData['opening_balance'], 2)); ?></strong></td>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php $__currentLoopData = $accountData['transactions']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                    <tr>
                                                        <td><?php echo e($row['date']); ?></td>
                                                        <td><?php echo e($row['description']); ?></td>
                                                        <td><?php echo e($row['reference']); ?></td>
                                                        <td><?php echo e(number_format($row['dr'], 2)); ?></td>
                                                        <td><?php echo e(number_format($row['cr'], 2)); ?></td>
                                                        <td><?php echo e(number_format($row['balance'], 2)); ?></td>
                                                    </tr>
                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                <!-- Account Total Row -->
                                                <tr class="table-active">
                                                    <td colspan="3" class="text-end"><strong>Account Totals:</strong></td>
                                                    <td><strong><?php echo e(number_format($accountData['total_dr'], 2)); ?></strong></td>
                                                    <td><strong><?php echo e(number_format($accountData['total_cr'], 2)); ?></strong></td>
                                                    <td><strong><?php echo e(number_format($accountData['closing_balance'], 2)); ?></strong></td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            
                            <!-- Account Type Total Row -->
                            <div class="table-responsive">
                                <table class="table table-bordered">
                                    <tr class="table-primary">
                                        <td colspan="3" class="text-end"><strong>Account Type Totals:</strong></td>
                                        <td><strong><?php echo e(number_format($typeData['total_dr'], 2)); ?></strong></td>
                                        <td><strong><?php echo e(number_format($typeData['total_cr'], 2)); ?></strong></td>
                                        <td><strong><?php echo e(number_format($typeData['total_closing'], 2)); ?></strong></td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                        
                        <?php
                            $grandTotalOpening += $typeData['total_opening'];
                            $grandTotalDr += $typeData['total_dr'];
                            $grandTotalCr += $typeData['total_cr'];
                            $grandTotalClosing += $typeData['total_closing'];
                        ?>
                    <?php endif; ?>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                
                <!-- Grand Total Row -->
                <div class="table-responsive mt-4">
                    <table class="table table-bordered">
                        <tr class="table-success">
                            <td colspan="3" class="text-end"><strong>Grand Totals:</strong></td>
                            <td><strong><?php echo e(number_format($grandTotalDr, 2)); ?></strong></td>
                            <td><strong><?php echo e(number_format($grandTotalCr, 2)); ?></strong></td>
                            <td><strong><?php echo e(number_format($grandTotalClosing, 2)); ?></strong></td>
                        </tr>
                    </table>
                </div>
                
                <div class="mt-4 text-center">
                    <p class="text-muted">Copyright © Melsoft (Private) Limited</p>
                </div>
            </div>
        </div>
    <?php elseif(isset($transactionData) && count($transactionData) == 0): ?>
        <div class="card shadow-sm mt-4">
            <div class="card-body">
                <div class="alert alert-info mt-3">
                    No transactions found for the selected criteria.
                </div>
            </div>
        </div>
    <?php endif; ?>
</div>

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

<?php $__env->startSection('styles'); ?>
<style>
.btn-back {
    text-decoration: none;
    color: #1AB9D9;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    padding: 5px 15px;
    border-radius: 4px;
    transition: all 0.3s;
}
.btn-back:hover {
    background-color: rgba(26, 185, 217, 0.1);
    text-decoration: none;
}
.card {
    border-radius: 8px;
    overflow: hidden;
    border: none;
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.1);
}
.card-header {
    border-bottom: 1px solid rgba(0,0,0,0.1);
    padding: 12px 20px;
}
.table th {
    background-color: #f8f9fa;
}
.table-responsive {
    overflow-x: auto;
}
.table-active, .table-info, .table-primary, .table-success {
    background-color: rgba(0,0,0,.05) !important;
}
.btn-process-all {
    background-color: #ffffff !important;
    color: #1AB9D9 !important;
    border: 1px solid rgba(26,185,217,0.35) !important;
    font-weight: 600 !important;
    padding: 7px 14px !important;
    box-shadow: none !important;
    line-height: 1 !important;
}
.btn-process-all:hover {
    background-color: #f5fbfc !important;
    color: #1498b0 !important;
    border-color: rgba(20,152,176,0.45) !important;
}
.account-type-section {
    border-left: 4px solid #1AB9D9;
    padding-left: 15px;
    margin-bottom: 30px;
}
.account-section {
    border-left: 2px solid #6c757d;
    padding-left: 15px;
    margin-bottom: 20px;
}

@media print {
    .btn-back, .card-header .btn, form {
        display: none !important;
    }
    .card {
        box-shadow: none;
        border: 1px solid #ddd;
    }
    .table-responsive {
        overflow-x: visible;
    }
}
</style>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
    // Initialize Select2 for dropdowns
    $(document).ready(function() {
        $('.select2').select2({
            placeholder: "Select Option",
            allowClear: true
        });
    });

    function resetForm() {
        // Reset the select2 dropdowns
        $('#gl_account_from').val(null).trigger('change');
        $('#gl_account_to').val(null).trigger('change');
        $('#currency').val(null).trigger('change');
        $('#date_from').val('');
        $('#date_to').val('');
    }
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/melsoftzimco/public_html/demo/resources/views/reports/gl-transaction.blade.php ENDPATH**/ ?>