/**
 * Tiriri - Admin JavaScript
 */
jQuery(function ($) {
    'use strict';

    if (typeof tiririAdmin === 'undefined') {
        return;
    }

    var config = tiririAdmin;

    // Re-scan button
    $('#tiriri-rescan').on('click', function () {
        var $btn = $(this);
        $btn.prop('disabled', true).text(config.strings.scanning);

        $.post(config.ajaxUrl, {
            action: 'tiriri_rescan',
            nonce: config.nonce,
        })
            .done(function (response) {
                if (response.success) {
                    $btn.text(config.strings.scanComplete);
                    // Reload page to show updated results
                    setTimeout(function () {
                        location.reload();
                    }, 1000);
                } else {
                    $btn.text(config.strings.error);
                }
            })
            .fail(function () {
                $btn.text(config.strings.error);
            })
            .always(function () {
                setTimeout(function () {
                    $btn.prop('disabled', false).text(config.strings.rescan);
                }, 3000);
            });
    });

    // Remove manual declaration
    $(document).on('click', '.tiriri-remove-manual', function () {
        if (!confirm(config.strings.confirm)) {
            return;
        }

        var $btn = $(this);
        var id = $btn.data('id');
        $btn.prop('disabled', true).text(config.strings.removing);

        $.post(config.ajaxUrl, {
            action: 'tiriri_remove_manual',
            nonce: config.nonce,
            id: id,
        })
            .done(function (response) {
                if (response.success) {
                    location.reload();
                } else {
                    alert(config.strings.error);
                    $btn.prop('disabled', false).text(config.strings.remove);
                }
            })
            .fail(function () {
                alert(config.strings.error);
                $btn.prop('disabled', false).text('Remove');
            });
    });

    // Manual declaration form
    $('#tiriri-manual-form').on('submit', function (e) {
        e.preventDefault();

        var name = $('#manual_name').val().trim();
        if (!name) {
            return;
        }

        var $btn = $(this).find('button[type="submit"]');
        $btn.prop('disabled', true);

        $.post(config.ajaxUrl, {
            action: 'tiriri_add_manual',
            nonce: config.nonce,
            name: name,
            category: $('#manual_category').val(),
            note: $('#manual_note').val().trim(),
        })
            .done(function (response) {
                if (response.success) {
                    // Clear form and reload
                    $('#manual_name').val('');
                    $('#manual_note').val('');
                    location.reload();
                } else {
                    alert(config.strings.error);
                }
            })
            .fail(function () {
                alert(config.strings.error);
            })
            .always(function () {
                $btn.prop('disabled', false);
            });
    });
});
