<?php $__env->startSection('content'); ?>
<div class="container-fluid py-3">
    <div class="row">
        <div class="col-lg-5">
            <div class="card shadow-sm">
                <div class="card-header bg-success text-white py-3">
                    <h5 class="mb-0"><i class="fas fa-book me-2"></i> Add Cashbook Entry</h5>
                </div>
                <div class="card-body">
                    <form action="<?php echo e(route('cashbook.entries.store')); ?>" method="POST" id="cashbookForm">
                        <?php echo csrf_field(); ?>
                        <input type="hidden" name="id" id="cashbookId" value="">
<?php if($errors->any()): ?>
    <div class="alert alert-danger">
        <h6>Validation Errors:</h6>
        <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; ?>

<?php if(session('success')): ?>
    <div class="alert alert-success"><?php echo e(session('success')); ?></div>
<?php endif; ?>

<?php if(session('error')): ?>
    <div class="alert alert-danger"><?php echo e(session('error')); ?></div>
<?php endif; ?>
                        <div class="mb-3">
                            <label class="form-label"><strong>Code:</strong></label>
                            <input type="text" class="form-control" name="code" id="code" required placeholder="Enter code">
                        </div>
                        <div class="mb-3">
                            <label class="form-label"><strong>GL Account:</strong></label>
                            <select class="form-control" name="gl_account_id" id="gl_account_id" required>
                                <option value="">Select GL Account</option>
                                <?php $__currentLoopData = $glAccounts->sortBy('code'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $account): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($account->id); ?>"><?php echo e($account->code); ?> - <?php echo e($account->account_name); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label"><strong>Description:</strong></label>
                            <textarea class="form-control" name="description" id="description" rows="3" placeholder="Enter description"></textarea>
                        </div>
                        <div class="d-flex justify-content-end mt-4">
                            <input type="submit" id="submitButton" class="btn btn-primary me-2" value="SUBMIT">
                            <input type="reset" class="btn btn-outline-secondary" onclick="resetForm()" value="REFRESH">
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-lg-7 mt-4 mt-lg-0">
            <div class="card shadow-sm">
                <div class="card-header bg-success text-white py-3">
                    <h5 class="mb-0"><i class="fas fa-list me-2"></i> Cashbook Entries</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>GL Account</th>
                                    <th>Code</th>
                                    <th>Description</th>
                                    <th style="width:100px">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__empty_1 = true; $__currentLoopData = $cashbooks; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cashbook): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td>
<?php echo e($cashbook->mainAccount->code ?? 'N/A'); ?> - <?php echo e($cashbook->mainAccount->account_name ?? 'N/A'); ?>

                                    </td>
                                    <td><?php echo e($cashbook->code); ?></td>
                                    <td><?php echo e($cashbook->description); ?></td>
                                    <td class="text-center">
                                        <a href="#" class="btn btn-sm btn-primary" onclick="editCashbook(event, '<?php echo e($cashbook->id); ?>', '<?php echo e($cashbook->code); ?>', '<?php echo e($cashbook->gl_account_id); ?>', `<?php echo e(addslashes($cashbook->description)); ?>`)">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <a href="<?php echo e(route('cashbook.entries.delete', ['id' => $cashbook->id])); ?>" class="btn btn-sm btn-danger deleteBtn">
                                            <i class="fas fa-trash-alt"></i>
                                        </a>
                                    </td>
                                </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr>
                                    <td colspan="4" class="text-center text-muted py-4">No cashbook entries found. Create your first entry above.</td>
                                </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Delete Modal -->
<div class="modal fade" id="deleteConfirmModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <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" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                Are you sure you want to delete this cashbook 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="button" class="btn btn-danger">Delete</button>
            </div>
        </div>
    </div>
</div>

<script>
    function editCashbook(event, id, code, glAccountId, description) {
        event.preventDefault();
        document.getElementById('cashbookId').value = id;
        document.getElementById('code').value = code;
        document.getElementById('gl_account_id').value = glAccountId;
        document.getElementById('description').value = description;
        document.querySelector('.card-header h5').innerHTML = '<i class="fas fa-book me-2"></i> Edit Cashbook Entry';
        document.getElementById('submitButton').value = 'UPDATE';
        document.getElementById('cashbookForm').action = '<?php echo e(route("cashbook.entries.edit")); ?>';
    }

    function resetForm() {
        document.getElementById('cashbookId').value = '';
        document.getElementById('code').value = '';
        document.getElementById('gl_account_id').value = '';
        document.getElementById('description').value = '';
        document.querySelector('.card-header h5').innerHTML = '<i class="fas fa-book me-2"></i> Add Cashbook Entry';
        document.getElementById('submitButton').value = 'SUBMIT';
        document.getElementById('cashbookForm').action = '<?php echo e(route("cashbook.entries.store")); ?>';
        document.getElementById('timeout').innerHTML = '';
    }
</script>

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

        document.querySelectorAll('.deleteBtn').forEach(btn => {
            btn.addEventListener('click', function (e) {
                e.preventDefault();
                deleteUrl = this.getAttribute('href');
                bsModal.show();
            });
        });

        document.getElementById('confirmDeleteBtn').addEventListener('click', function () {
            if (deleteUrl) {
                window.location.href = deleteUrl;
            }
        });

        document.getElementById('cancelDeleteBtn').addEventListener('click', function () {
            bsModal.hide();
        });
    });
</script>

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

<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;
}
</style>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\odza\one\melcredora\resources\views/maintanance/cashbook/create_cashbook.blade.php ENDPATH**/ ?>