
<?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('ledger.create')); ?>" class="btn-back">
            <i class="fas fa-arrow-left me-2"></i> Back to GL Accounts
        </a>
    </div>

    <div class="row">
        <!-- FORM SECTION -->
        <div class="col-lg-5">
            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white py-3">
                    <h5 id="formHeader" class="mb-0"><i class="fas fa-layer-group me-2"></i> Add Main Sub Accounts</h5>
                </div>
                <div class="card-body">
                    <form id="mainSubAccountForm" action="<?php echo e(route('main.sub.account.store')); ?>" method="POST">
                        <?php echo csrf_field(); ?>
                        <input type="hidden" id="mainSubAccountId" name="id">
                        <input type="hidden" id="parentId" name="parent_id" value="<?php echo e($mainAccount->id); ?>">

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

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

                        <div class="mb-3">
                            <label class="form-label"><strong>Main Account Name:</strong></label>
                            <input class="form-control"
                                   type="text"
                                   value="<?php echo e($mainAccount->account_name); ?>"
                                   readonly />
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Main Sub Account Code:</strong></label>
                            <input class="form-control"
                                   type="text"
                                   id="accountCode"
                                   name="account_code"
                                   required />
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Main Sub Account Name:</strong></label>
                            <input class="form-control"
                                   type="text"
                                   id="subName"
                                   name="sub_name"
                                   required />
                        </div>

                        <div class="d-flex justify-content-end mt-4">
                            <input id="submitButton" type="submit" class="btn btn-primary me-2" value="SUBMIT">
                            <input type="reset" class="btn btn-outline-secondary" value="REFRESH" onclick="resetMainSubForm()">
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- TABLE SECTION -->
        <div class="col-lg-7 mt-4 mt-lg-0">
            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white py-3">
                    <h5 class="mb-0"><i class="fas fa-list me-2"></i> Main Sub Accounts</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>Main Sub Account Code</th>
                                    <th>Main Sub Account Name</th>
                                    <th>Main Account Name</th>
                                    <th style="width:150px">Sub Account 1</th>
                                    <th style="width:100px">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $mainSubAccounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $mainSubAccount): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr>
                                    <td><?php echo e($mainSubAccount->account_code); ?></td>
                                    <td><?php echo e($mainSubAccount->sub_name); ?></td>
                                    <td><?php echo e($mainSubAccount->mainAccount->account_name ?? 'N/A'); ?></td>
                                    <td>
                                        <a href="<?php echo e(route('sub.account.1.add', $mainSubAccount->id)); ?>" class="btn btn-sm btn-info">
                                            &rarr; Sub Acc 1
                                        </a>
                                    </td>
                                    <td class="text-center">
                                        <a href="#" class="btn btn-sm btn-primary" onclick="editMainSubAccount(
                                            event,
                                            '<?php echo e($mainSubAccount->id); ?>',
                                            '<?php echo e($mainSubAccount->account_code); ?>',
                                            '<?php echo e($mainSubAccount->sub_name); ?>'
                                        )">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <button class="btn btn-sm btn-danger deleteMainSubBtn" 
                                                data-id="<?php echo e($mainSubAccount->id); ?>">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </td>
                                </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- DELETE MODAL -->
<div class="modal fade" id="deleteMainSubConfirmModal" 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 main sub account?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <form id="deleteMainSubForm" method="POST" action="">
                    <?php echo csrf_field(); ?>
                    <?php echo method_field('DELETE'); ?>
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
function editMainSubAccount(event, id, account_code, sub_name) {
    event.preventDefault();

    document.getElementById('mainSubAccountId').value = id;
    document.getElementById('accountCode').value = account_code;
    document.getElementById('subName').value = sub_name;

    document.getElementById('formHeader').innerHTML = '<i class="fas fa-layer-group me-2"></i> Edit Main Sub Account';
    document.getElementById('submitButton').value = 'UPDATE';
    document.getElementById('mainSubAccountForm').action = '<?php echo e(route("editMainSubAccount")); ?>';
}

function resetMainSubForm() {
    document.getElementById('mainSubAccountId').value = '';
    document.getElementById('accountCode').value = '';
    document.getElementById('subName').value = '';
    document.getElementById('formHeader').innerHTML = '<i class="fas fa-layer-group me-2"></i> Add Main Sub Accounts';
    document.getElementById('submitButton').value = 'SUBMIT';
    document.getElementById('mainSubAccountForm').action = '<?php echo e(route("main.sub.account.store")); ?>';
    document.getElementById('timeout').innerHTML = '';
}

document.addEventListener('DOMContentLoaded', function(){
    const deleteButtons = document.querySelectorAll('.deleteMainSubBtn');
    const deleteForm = document.getElementById('deleteMainSubForm');
    const modal = new bootstrap.Modal(document.getElementById('deleteMainSubConfirmModal'));
    
    deleteButtons.forEach(button => {
        button.addEventListener('click', function() {
            const accountId = this.getAttribute('data-id');
            const actionUrl = "<?php echo e(route('main.sub.account.delete', ['id' => ':id'])); ?>".replace(':id', accountId);
            
            deleteForm.action = actionUrl;
            modal.show();
        });
    });
});
</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;
}
</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/maintanance/chart_of_accounts/createMainSubAccount.blade.php ENDPATH**/ ?>