<?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="row">
        <!-- Form Section -->
        <div class="col-lg-4">
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-primary text-white py-3">
                    <h5 class="mb-0" id="formTitle"><i class="fas fa-book me-2"></i> Process Supplier Journal</h5>
                </div>
                <div class="card-body">
                    <form action="<?php echo e(route('supplier.journal.store')); ?>" method="POST" novalidate id="journalForm">
                        <?php echo csrf_field(); ?>
                        <input type="hidden" name="id" id="entryId" value="">

                        <div id="timeout">
                            <?php if(Session::has('success')): ?>
                                <p class="alert alert-info"><?php echo e(Session('success')); ?></p>
                            <?php endif; ?>
                            <?php if(Session::has('error')): ?>
                                <p class="alert alert-danger"><?php echo e(Session('error')); ?></p>
                            <?php endif; ?>
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Account Code:</strong></label>
                            <input type="text" class="form-control" value="<?php echo e($supplierAccount->code ?? '9600'); ?>" disabled>
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Date:</strong></label>
                            <input type="date" name="date" class="form-control" id="date" value="<?php echo e(old('date', date('Y-m-d'))); ?>" required />
                            <?php $__errorArgs = ['date'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><div class="text-danger small"><?php echo e($message); ?></div><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Narration:</strong></label>
                            <input class="form-control" type="text" name="narration" id="narration" value="<?php echo e(old('narration')); ?>" placeholder="Enter narration" required />
                            <?php $__errorArgs = ['narration'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><div class="text-danger small"><?php echo e($message); ?></div><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Supplier Account:</strong></label>
                            <select name="supplier_account_id" id="supplier_account_id" class="form-control" required>
                                <option value="">Select Supplier Account</option>
                                <?php $__currentLoopData = $suppliers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $supplier): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($supplier->id); ?>" <?php echo e(old('supplier_account_id') == $supplier->id ? 'selected' : ''); ?>>
                                        <?php echo e($supplier->supplier_code); ?> - <?php echo e($supplier->supplier_account); ?>

                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <?php $__errorArgs = ['supplier_account_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><div class="text-danger small"><?php echo e($message); ?></div><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Reference:</strong></label>
                            <input class="form-control" type="text" name="reference" id="reference" value="<?php echo e(old('reference')); ?>" placeholder="Enter reference" required />
                            <?php $__errorArgs = ['reference'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><div class="text-danger small"><?php echo e($message); ?></div><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>TR Code:</strong></label>
                            <select name="tr_code" id="tr_code" class="form-control" required>
                                <option value="">Select TR Code</option>
                                <option value="DR" <?php echo e(old('tr_code') == 'DR' ? 'selected' : ''); ?>>DR</option>
                                <option value="CR" <?php echo e(old('tr_code') == 'CR' ? 'selected' : ''); ?>>CR</option>
                            </select>
                            <?php $__errorArgs = ['tr_code'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><div class="text-danger small"><?php echo e($message); ?></div><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Currency:</strong></label>
                            <select name="currency_id" id="currency_id" class="form-control" required>
                                <option value="">Select Currency</option>
                                <?php $__currentLoopData = $currencies; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $currency): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($currency->id); ?>" <?php echo e(old('currency_id') == $currency->id ? 'selected' : ''); ?>>
                                        <?php echo e($currency->code); ?> - <?php echo e($currency->name); ?>

                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <?php $__errorArgs = ['currency_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><div class="text-danger small"><?php echo e($message); ?></div><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Amount:</strong></label>
                            <input class="form-control" type="number" step="0.01" name="amount" id="amount" value="<?php echo e(old('amount')); ?>" placeholder="00.00" required />
                            <?php $__errorArgs = ['amount'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><div class="text-danger small"><?php echo e($message); ?></div><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Project:</strong></label>
                            <select name="project_id" class="form-control" id="project_id" required>
                                <option value="">Select Project (Optional)</option>
                                <?php $__currentLoopData = $projects; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $project): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($project->id); ?>" <?php echo e(old('project_id') == $project->id ? 'selected' : ''); ?>>
                                                <?php echo e($project->code); ?> - <?php echo e($project->name); ?>

                                            </option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <?php $__errorArgs = ['project_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <small class="text-danger"><?php echo e($message); ?></small>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="d-flex justify-content-end mt-4">
                            <button type="submit" id="submitButton" class="btn btn-primary me-2">Save</button>
                            <button type="reset" class="btn btn-outline-secondary" id="resetButton">Refresh</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Table Section -->
        <div class="col-lg-8 mt-4 mt-lg-0">
            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white py-3 d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="fas fa-list me-2"></i> Journal Entries</h5>
                    <?php if($tempEntries->isNotEmpty()): ?>
                        <form action="<?php echo e(route('supplier.journal.processAll')); ?>" method="POST" class="m-0">
                            <?php echo csrf_field(); ?>
                            <button type="submit" id="processJournalBtn" class="btn btn-success btn-sm">PROCESS ALL</button>
                        </form>
                    <?php endif; ?>
                </div>
                <div class="card-body">
                    <?php if($tempEntries->isEmpty()): ?>
                        <p class="text-muted mb-0">No journal entries available.</p>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-bordered table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>Date</th>
                                        <th>Supplier Code</th>
                                        <th>Supplier Name</th> 
                                        <th>Narration</th>
                                        <th>DR</th>
                                        <th>CR</th>
                                        <th>Status</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $__currentLoopData = $tempEntries; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $entry): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <tr>
                                        <td><?php echo e(date('d/m/Y', strtotime($entry->date))); ?></td>
                                        <td><?php echo e($entry->supplier->supplier_code); ?></td>
                                        <td><?php echo e($entry->supplier->supplier_account); ?></td>
                                        <td><?php echo e($entry->narration); ?></td>
                                        <td><?php echo e($entry->tr_code === 'DR' ? number_format($entry->amount, 2) : ''); ?></td>
                                        <td><?php echo e($entry->tr_code === 'CR' ? number_format($entry->amount, 2) : ''); ?></td>
                                        <td>
                                            <?php if($entry->status === 'Processed'): ?>
                                                <span class="badge bg-success">Processed</span>
                                            <?php else: ?>
                                                <span class="badge bg-warning text-dark">Open Journal</span>
                                            <?php endif; ?>
                                        </td>
                                        <td class="text-center">
                                            <a href="#" class="btn btn-sm btn-primary" onclick="editEntry(<?php echo e($entry->id); ?>)">
                                                <i class="fas fa-edit"></i>
                                            </a>

                                            <!-- Process single entry -->
                                            <?php if($entry->status !== 'Processed'): ?>
                                            <form action="<?php echo e(route('supplier.journal.processSingle', $entry->id)); ?>" method="POST" class="d-inline">
                                                <?php echo csrf_field(); ?>
                                                <button type="submit" class="btn btn-sm btn-success process-btn">
                                                    <i class="fas fa-check"></i>
                                                </button>
                                            </form>
                                            <?php endif; ?>

                                            <!-- Delete button triggers modal -->
                                            <button class="btn btn-sm btn-danger deleteBtn" data-id="<?php echo e($entry->id); ?>">
                                                <i class="fas fa-trash-alt"></i>
                                            </button>
                                        </td>
                                    </tr>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Delete Modal -->
<div class="modal fade" id="deleteConfirmModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <form method="POST" id="deleteForm">
            <?php echo csrf_field(); ?>
            <?php echo method_field('DELETE'); ?>
            <div class="modal-content">
                <div class="modal-header bg-danger text-white">
                    <h5 class="modal-title">Confirm Deletion</h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">Are you sure you want to delete this journal entry?</div>
                <div class="modal-footer">
                    <button id="cancelDeleteBtn" type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button id="confirmDeleteBtn" type="submit" class="btn btn-danger">Delete</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const bsModal = new bootstrap.Modal(document.getElementById('deleteConfirmModal'));
        const deleteForm = document.getElementById('deleteForm');

        document.querySelectorAll('.deleteBtn').forEach(btn => {
            btn.addEventListener('click', function (e) {
                e.preventDefault();
                const entryId = this.getAttribute('data-id');
                const url = '<?php echo e(url("supplier-journal/delete")); ?>/' + entryId;
                deleteForm.setAttribute('action', url);
                bsModal.show();
            });
        });
    });

    function editEntry(id) {
        const entry = <?php echo json_encode($tempEntries->keyBy('id'), 15, 512) ?>;
        const selected = entry[id];
        if (!selected) {
            alert('Entry data not found');
            return;
        }

        document.getElementById('entryId').value = selected.id;
        document.getElementById('date').value = selected.date;
        document.getElementById('narration').value = selected.narration;
        document.getElementById('supplier_account_id').value = selected.supplier_account_id;
        document.getElementById('reference').value = selected.reference;
        document.getElementById('tr_code').value = selected.tr_code;
        document.getElementById('currency_id').value = selected.currency_id;
        document.getElementById('amount').value = selected.amount;

        document.getElementById('formTitle').innerHTML = '<i class="fas fa-book me-2"></i> Edit Supplier Journal Entry';
        document.getElementById('submitButton').textContent = 'Update';
        document.getElementById('journalForm').action = '<?php echo e(route("supplier.journal.update")); ?>';

        window.scrollTo({ top: 0, behavior: 'smooth' });
    }

    document.getElementById('resetButton').addEventListener('click', function() {
        document.getElementById('entryId').value = '';
        document.getElementById('formTitle').innerHTML = '<i class="fas fa-book me-2"></i> Process Supplier Journal';
        document.getElementById('submitButton').textContent = 'Save';
        document.getElementById('journalForm').action = '<?php echo e(route("supplier.journal.store")); ?>';
    });

    setTimeout(() => {
        document.querySelectorAll('.alert').forEach(alert => alert.style.display = 'none');
    }, 5000);
</script>

<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;
}
#processJournalBtn {
    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;
}
#processJournalBtn:hover {
    background-color: #f5fbfc !important;
    color: #1498b0 !important;
    border-color: rgba(20,152,176,0.45) !important;
}
.table .process-btn {
    background-color: #1AB9D9 !important;
    border: 1px solid rgba(26,185,217,0.35) !important;
    color: #fff !important;
}
.table .process-btn:hover {
    background-color: #1498b0 !important;
    border-color: rgba(20,152,176,0.45) !important;
}
.table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

@media (max-width: 768px) {
    table.table {
        font-size: 0.85rem;
    }
    table.table th,
    table.table td {
        white-space: nowrap;
    }
}

.text-danger.small {
    font-size: 0.85rem;
}
</style>
<?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/supplierJournal/supplier-journal.blade.php ENDPATH**/ ?>