Path : /home/vishqocm/pcib.in/admin/js/
File Upload :
Current File : //home/vishqocm/pcib.in/admin/js/manage_questions.js

// Function to delete a question with confirmation
function deleteQuestion(questionId) {
    if (!questionId) {
        toastr.error('Invalid question ID');
        return;
    }
    
    // Show confirmation dialog
    Swal.fire({
        title: 'Are you sure?',
        text: "This question will be permanently deleted. This action cannot be undone!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#d33',
        cancelButtonColor: '#3085d6',
        confirmButtonText: 'Yes, delete it!'
    }).then((result) => {
        if (result.isConfirmed) {
            // Send AJAX request to delete the question
            $.ajax({
                url: 'ajax/delete_exam_question.php',
                type: 'POST',
                data: { question_id: questionId },
                dataType: 'json',
                success: function(response) {
                    if (response.status === 'success') {
                        // Show success message
                        Swal.fire(
                            'Deleted!',
                            response.message,
                            'success'
                        );
                        
                        // Remove the question from the UI or refresh the page
                        $('#question-' + questionId).fadeOut(500, function() {
                            $(this).remove();
                            
                            // If no questions left, show a message
                            if ($('.question-item').length === 0) {
                                $('#questions-container').html('<div class="alert alert-info">No questions found.</div>');
                            }
                        });
                    } else {
                        // Show error message
                        Swal.fire(
                            'Error!',
                            response.message,
                            'error'
                        );
                    }
                },
                error: function() {
                    Swal.fire(
                        'Error!',
                        'An error occurred while deleting the question.',
                        'error'
                    );
                }
            });
        }
    });
}

// Function to view question details
function viewQuestionDetails(questionId) {
    if (!questionId) {
        toastr.error('Invalid question ID');
        return;
    }
    
    // Show loading state
    $('#questionDetailsModal .modal-body').html('<div class="text-center"><i class="fas fa-spinner fa-spin fa-3x"></i><p class="mt-2">Loading question details...</p></div>');
    $('#questionDetailsModal').modal('show');
    
    // Fetch question details via AJAX
    $.ajax({
        url: 'ajax/get_question_details.php',
        type: 'GET',
        data: { question_id: questionId },
        success: function(response) {
            $('#questionDetailsModal .modal-body').html(response);
        },
        error: function() {
            $('#questionDetailsModal .modal-body').html('<div class="alert alert-danger">Failed to load question details.</div>');
        }
    });
}

// Document ready function
$(document).ready(function() {
    // Initialize toastr notification options
    toastr.options = {
        "closeButton": true,
        "positionClass": "toast-top-right",
        "showDuration": "300",
        "hideDuration": "1000",
        "timeOut": "5000"
    };
    
    // Handle delete button clicks
    $(document).on('click', '.delete-question', function(e) {
        e.preventDefault();
        const questionId = $(this).data('id');
        deleteQuestion(questionId);
    });
    
    // Handle view details button clicks
    $(document).on('click', '.view-question', function(e) {
        e.preventDefault();
        const questionId = $(this).data('id');
        viewQuestionDetails(questionId);
    });
});