/**
 * Agenistrator - Status Page Handler.
 *
 * Handles status page operations like deleting reports.
 *
 * @package   Agenistrator
 * @since     1.0.0
 * @author    Alex Ruban <alex@maximum.guru>
 * @author    Maximum Software Inc <contact@maximum.software>
 * @license   GPL-3.0-only
 * @link      https://maximum.software/
 *
 * SPDX-FileCopyrightText: 2025-2026 Alex Ruban <alex@maximum.guru>
 * SPDX-FileCopyrightText: 2025-2026 Maximum Software Inc <contact@maximum.software>
 * SPDX-License-Identifier: GPL-3.0-only
 */
jQuery(document).ready(function() {
    'use strict';

    // Delete report button click handler
    jQuery(document).on('click', '.agenistrator-delete-report', function() {
        var $button = jQuery(this);
        var reportId = $button.data('report-id');
        var $row = $button.closest('tr');

        if (!reportId || !confirm(agenistrator_status.strings.confirm_delete_report)) {
            return;
        }

        $button.prop('disabled', true);

        jQuery.ajax({
            url: ajaxurl,
            method: 'POST',
            data: {
                action: 'agenistrator_delete_report',
                report_id: reportId,
                nonce: agenistrator.nonce
            },
            success: function(response) {
                if (response.success) {
                    $row.fadeOut(function() {
                        var $table = $row.closest('table');
                        var $card = $table.closest('.agenistrator-reports-card');
                        $row.remove();

                        // Hide entire card if no more rows
                        if ($table.find('tbody tr').length === 0) {
                            $card.slideUp(function() {
                                jQuery(this).remove();
                            });
                        }
                    });
                } else {
                    var errorMsg = response.data && response.data.message ? response.data.message : agenistrator_status.strings.failed_to_delete_report;
                    alert(errorMsg);
                }
            },
            error: function(xhr) {
                var errorMsg = agenistrator_status.strings.failed_to_delete_report;
                if (xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) {
                    errorMsg = xhr.responseJSON.data.message;
                }
                alert(errorMsg);
            }
        });
    });

    // Delete all reports button click handler
    jQuery(document).on('click', '.agenistrator-delete-all-reports', function() {
        var $button = jQuery(this);
        var $card = $button.closest('.agenistrator-reports-card');

        if (!confirm(agenistrator_status.strings.confirm_delete_all_reports)) {
            return;
        }

        $button.prop('disabled', true);

        jQuery.ajax({
            url: ajaxurl,
            method: 'POST',
            data: {
                action: 'agenistrator_delete_all_reports',
                nonce: agenistrator.nonce
            },
            success: function(response) {
                if (response.success) {
                    $card.slideUp(function() {
                        jQuery(this).remove();
                    });
                } else {
                    var errorMsg = response.data && response.data.message ? response.data.message : agenistrator_status.strings.failed_to_delete_reports;
                    alert(errorMsg);
                }
            },
            error: function(xhr) {
                var errorMsg = agenistrator_status.strings.failed_to_delete_reports;
                if (xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) {
                    errorMsg = xhr.responseJSON.data.message;
                }
                alert(errorMsg);
            }
        });
    });
});
