<?php $__env->startSection('content'); ?>
 <div class="card">
    <div class="card-header">
        <div class="row align-items-center">
            <div class="col">
                <h5 class="mb-0">Payroll Process</h5>
            </div>
            <div class="col-auto">
                <a href="<?php echo e(route('payroll.payroll-processes.create')); ?>" class="btn btn-primary btn-sm">
                    Add Payroll Process
                </a>
            </div>
        </div>   
    </div>
    <div class="card-body">
        <table class="table table-bordered table-striped w-100" id="payroll-processes-table">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Group</th>
                    <th>From Date</th>
                    <th>To Date</th>
                    <th>Status</th>
                    <th>Freeze</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
    $(document).ready(function() {
        // Initialize DataTable
            let table = $('#payroll-processes-table').DataTable({
                processing: true,
                serverSide: true,
                ajax: "<?php echo e(route('payroll.payroll-processes.index')); ?>",
                order: [[1, 'desc']],
                dom:
                    "<'row mb-3 align-items-center'<'col-md-6'l><'col-md-6 d-flex justify-content-end'f>>" +
                    "<'table-responsive'tr>" +
                    "<'row mt-3 align-items-center'<'col-md-6'i><'col-md-6 d-flex justify-content-end'p>>",
                language: {
                    search: "",
                    searchPlaceholder: "Search payroll process...",
                    lengthMenu: "Show _MENU_ entries",
                    info: "Showing _START_–_END_ of _TOTAL_ payroll process"
                },
                columns: [
                    { data: 'DT_RowIndex', name: 'DT_RowIndex', orderable: false, searchable: false },
                    { data: 'payroll_group_id', name: 'payroll_group_id' },
                    { data: 'date_from', name: 'date_from' },
                    { data: 'date_to', name: 'date_to' },
                    { data: 'status', name: 'status' },
                    { data: 'freeze', name: 'freeze' },
                    { data: 'action', name: 'action', orderable: false, searchable: false }
                ]
            });

    });

     // Delete handler
        $(document).on('click', '.delete-btn', function() {
            const url = $(this).data('url');
            Swal.fire({
                title: 'Are you sure?',
                text: 'This record will be deleted!',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it'
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajax({
                        url: url,
                        type: 'DELETE',
                        data: { _token: "<?php echo e(csrf_token()); ?>" },
                        success: function (res) {
                            Swal.fire('Deleted!', res.message, 'success');
                            $('#payroll-processes-table').DataTable().ajax.reload();
                        }
                    });
                }
            });
        });

        // Freeze handler
        $(document).on('click', '.freeze-btn', function() {
            const url = $(this).data('url');
            Swal.fire({
                title: 'Are you sure?',
                text: 'you want to freeze this payroll? This action cannot be undone!',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#d33',
                confirmButtonText: 'Yes, freeze it'
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajax({
                        url: url,
                        type: 'POST',
                        data: { _token: "<?php echo e(csrf_token()); ?>" },
                        success: function (res) {
                            Swal.fire('Freezed!', res.message, 'success');
                            $('#payroll-processes-table').DataTable().ajax.reload();
                        }
                    });
                }
            });
        });

        // Unfreeze handler
        $(document).on('click', '.unfreeze-btn', function() {
            const url = $(this).data('url');
            Swal.fire({
                title: 'Are you sure?',
                text: 'You want to unfreeze this payroll? This will allow modifications.',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#17a2b8',
                confirmButtonText: 'Yes, unfreeze it'
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajax({
                        url: url,
                        type: 'POST',
                        data: { _token: "<?php echo e(csrf_token()); ?>" },
                        success: function (res) {
                            Swal.fire('Unfrozen!', res.message, 'success');
                            $('#payroll-processes-table').DataTable().ajax.reload();
                        },
                        error: function(xhr) {
                            let errorMessage = 'An error occurred';
                            if (xhr.responseJSON && xhr.responseJSON.message) {
                                errorMessage = xhr.responseJSON.message;
                            }
                            Swal.fire('Error!', errorMessage, 'error');
                        }
                    });
                }
            });
        });
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/codeproh/va.laraoffice.com/app/Modules/HR/Payroll/Providers/../Views/payroll-processes/index.blade.php ENDPATH**/ ?>