package Koha::Plugin::Pt::KEEPS::BiblioSearchTab;

use Modern::Perl;

use base qw(Koha::Plugins::Base);

our $VERSION = '1.0';

our $metadata = {
    name   => 'KEEPS - Biblio Search Tab',
    author => 'Keep Solutions',
    description => 'Adds a merge records search tab to the cataloguing main page',
    date_authored   => '2022-01-21',
    date_updated    => undef,
    minimum_version => '21.05',
    maximum_version => undef,
    version         => $VERSION,
};

sub new {
    my ( $class, $args ) = @_;

    $args->{'metadata'} = $metadata;
    $args->{'metadata'}->{'class'} = $class;

    my $self = $class->SUPER::new($args);

    return $self;
}

# Mandatory even if does nothing
sub install {
    my ( $self, $args ) = @_;

    return 1;
}

# Mandatory even if does nothing
sub upgrade {
    my ( $self, $args ) = @_;

    return 1;
}

# Mandatory even if does nothing
sub uninstall {
    my ( $self, $args ) = @_;

    return 1;
}

sub intranet_js {
    my ( $self ) = @_;

    return q%
<script>
    $( document ).ready(function() {
        var pathname = window.location.pathname;
        if (pathname == '/cgi-bin/koha/cataloguing/addbooks.pl') {

            //--- Search tab translation and behavior modification (catalog search in the first preference) ---// 
            $("#cat_addbooks .ui-tabs-nav .ui-tabs-active a").text("Pesquisar para unificar");
            $("#cat_addbooks .ui-tabs-nav .ui-tabs-active").attr({
                "tabindex": -1,
                "aria-selected": "false",
                "aria-expanded": "false"
            });
            $("#cat_addbooks .ui-tabs-nav .ui-tabs-active").removeClass("ui-tabs-active ui-state-active");
            $("#addbooks_search").css("display", "none");

            $("#cat_addbooks .ui-tabs-nav li:last-of-type").addClass("ui-tabs-active ui-state-active");
            $("#catalog_search").css("display", "block");
            $("#cat_addbooks .ui-tabs-nav li:last-of-type").attr({
                "tabindex": 0,
                "aria-selected": "true",
                "aria-expanded": "true"
            });
            $("#cat_addbooks .ui-tabs-nav li:last-of-type").prependTo($("#cat_addbooks .ui-tabs-nav"));

            $("#ui-id-5").trigger("click");
        }
    });
</script>
%;
}

1;