

<?php $__env->startSection('title', 'Purchase Request Approvals'); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h4 class="card-title">Purchase Request Approvals</h4>
                    <div class="card-tools">
                        <a href="<?php echo e(route('purchase-requests.index')); ?>" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> Back to Purchase Requests
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <!-- Filters -->
                    <div class="row mb-3">
                        <div class="col-md-3">
                            <label for="approval_status_filter" class="form-label">Approval Status</label>
                            <select class="form-select" id="approval_status_filter">
                                <option value="">All Statuses</option>
                                <option value="pending">Pending</option>
                                <option value="approved">Approved</option>
                                <option value="rejected">Rejected</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label for="date_from" class="form-label">Date From</label>
                            <input type="date" class="form-control" id="date_from">
                        </div>
                        <div class="col-md-3">
                            <label for="date_to" class="form-label">Date To</label>
                            <input type="date" class="form-control" id="date_to">
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">&nbsp;</label>
                            <button class="btn btn-primary d-block" id="apply_filters">
                                <i class="fas fa-filter"></i> Apply Filters
                            </button>
                        </div>
                    </div>

                    <div class="table-responsive">
                        <table class="table table-striped" id="approvalTable">
                            <thead>
                                <tr>
                                    <th>PR Number</th>
                                    <th>Title</th>
                                    <th>Requested By</th>
                                    <th>Approval Status</th>
                                    <th>Approval Info</th>
                                    <th>Items</th>
                                    <th>Created Date</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Approval Modal -->
<div class="modal fade" id="approvalModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Update Approval Status</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form id="approvalForm">
                
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="approval_status" class="form-label">Approval Status *</label>
                        <select class="form-select" id="approval_status" name="approval_status" required>
                            <option value="">Select Status</option>
                            <option value="approved">Approve</option>
                            <option value="rejected">Reject</option>
                        </select>
                    </div>
                    <div class="mb-3" id="notes_group">
                        <label for="notes" class="form-label">Approval Notes</label>
                        <textarea class="form-control" id="notes" name="notes" rows="3" placeholder="Optional approval notes..."></textarea>
                    </div>
                    <div class="mb-3" id="rejection_reason_group" style="display: none;">
                        <label for="rejection_reason" class="form-label">Rejection Reason *</label>
                        <textarea class="form-control" id="rejection_reason" name="rejection_reason" rows="3" placeholder="Please provide a reason for rejection..."></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Update Status</button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('plugin-scripts'); ?>
<script>
$(document).ready(function() {
    // Setup CSRF token for all AJAX requests
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    var table = $('#approvalTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: '<?php echo e(route("purchase-requests.approval")); ?>',
            data: function(d) {
                d.approval_status = $('#approval_status_filter').val();
                d.date_from = $('#date_from').val();
                d.date_to = $('#date_to').val();
            }
        },
        columns: [
            {data: 'pr_number', name: 'pr_number'},
            {data: 'title', name: 'title'},
            {data: 'user_name', name: 'user_name'},
            {data: 'approval_status', name: 'approval_status'},
            {data: 'approval_info', name: 'approval_info'},
            {data: 'items_count', name: 'items_count'},
            {data: 'created_at', name: 'created_at'},
            {data: 'actions', name: 'actions', orderable: false, searchable: false}
        ],
        order: [[6, 'desc']],
        pageLength: 25
    });

    // Filter functionality
    $('#apply_filters').click(function() {
        table.ajax.reload();
    });

    // Approval modal functionality
    var currentPrId = null;

    $(document).on('click', '.approve-pr, .reject-pr', function() {
        currentPrId = $(this).data('id');
        var action = $(this).hasClass('approve-pr') ? 'approved' : 'rejected';

        $('#approval_status').val(action);
        $('#approvalModal').modal('show');
    });

    $('#approval_status').change(function() {
        var status = $(this).val();
        if (status === 'rejected') {
            $('#notes_group').hide();
            $('#rejection_reason_group').show();
            $('#rejection_reason').prop('required', true);
        } else if (status === 'approved') {
            $('#notes_group').show();
            $('#rejection_reason_group').hide();
            $('#rejection_reason').prop('required', false);
        } else {
            $('#notes_group').hide();
            $('#rejection_reason_group').hide();
            $('#rejection_reason').prop('required', false);
        }
    });

        $('#approvalForm').submit(function(e) {
        e.preventDefault();

        // Get form data as regular object instead of FormData
        var formData = {
            _token: '<?php echo e(csrf_token()); ?>',
            approval_status: $('#approval_status').val(),
            notes: $('#notes').val(),
            rejection_reason: $('#rejection_reason').val()
        };

        $.ajax({
            url: '<?php echo e(route("purchase-requests.update-approval-status", ":id")); ?>'.replace(':id', currentPrId),
            type: 'PATCH',
            data: formData,
            headers: {
                'X-CSRF-TOKEN': '<?php echo e(csrf_token()); ?>'
            },
            success: function(response) {
                if (response.success) {
                    $('#approvalModal').modal('hide');
                    table.ajax.reload();

                    Swal.fire({
                        icon: 'success',
                        title: 'Success!',
                        text: response.message,
                        timer: 2000,
                        showConfirmButton: false
                    });
                } else {
                    Swal.fire({
                        icon: 'error',
                        title: 'Error!',
                        text: response.message
                    });
                }
            },
            error: function(xhr) {
                var message = 'An error occurred while updating approval status.';
                if (xhr.responseJSON && xhr.responseJSON.message) {
                    message = xhr.responseJSON.message;
                }

                Swal.fire({
                    icon: 'error',
                    title: 'Error!',
                    text: message
                });
            }
        });
    });
});
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\habibcables-ERP\resources\views\purchase_requests\approval_index.blade.php ENDPATH**/ ?>