package Koha::Plugin::Pt::KEEPS::BiblioNavigator;

use Modern::Perl;

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

our $VERSION = '1.0';

our $metadata = {
    name   => 'KEEPS - Biblio Navigator',
    author => 'Keep Solutions',
    description => 'Allows navigating through biblio records',
    date_authored   => '2022-01-24',
    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 ) = @_;

    my $path = $self->get_plugin_http_path();

    return qq%
<script>
    var pathname = window.location.pathname;
    if (pathname.match('/cgi-bin/koha/catalogue/detail.pl')) {
        var plugin_path = "$path";

        \$('#toolbar').append(`
            <div class="btn-group" style="margin-left: 20px;">
                <a id="first-biblionumber" class="btn btn-default btn-small disabled">
                    <i class="fa fa-fast-backward"></i>
                </a>
            </div>
        `);

        \$('#toolbar').append(`
            <div class="btn-group">
                <a id="previous-biblionumber" class="btn btn-default btn-small disabled">
                    <i class="fa fa-backward"></i>
                </a>
            </div>
        `);

        \$('#toolbar').append(`
            <div class="btn-group">
                <a id="next-biblionumber" class="btn btn-default btn-small disabled">
                    <i class="fa fa-forward"></i>
                </a>
            </div>
        `);

        \$('#toolbar').append(`
            <div class="btn-group">
                <a id="last-biblionumber" class="btn btn-default btn-small disabled">
                    <i class="fa fa-fast-forward"></i>
                </a>
            </div>
        `);

        const params = new URLSearchParams(window.location.search);
        var biblionumber = params.get('biblionumber');

        \$.ajax({
            url: plugin_path + `/biblio-navigator.pl?biblionumber=` + biblionumber,
        }).done(function(data) {

            if (data.first_biblionumber) {
                var href = `/cgi-bin/koha/catalogue/detail.pl?biblionumber=` + data.first_biblionumber;
                \$('#first-biblionumber').removeClass('disabled');
                \$('#first-biblionumber').attr('href', href);
            }
            
            
            if (data.previous_biblionumber) {
                var href = `/cgi-bin/koha/catalogue/detail.pl?biblionumber=` + data.previous_biblionumber;
                \$('#previous-biblionumber').removeClass('disabled');
                \$('#previous-biblionumber').attr('href', href);
            }
            
            
            if (data.next_biblionumber) {
                var href = `/cgi-bin/koha/catalogue/detail.pl?biblionumber=` + data.next_biblionumber;
                \$('#next-biblionumber').removeClass('disabled');
                \$('#next-biblionumber').attr('href', href);
            }
            
            
            if (data.last_biblionumber) {
                var href = `/cgi-bin/koha/catalogue/detail.pl?biblionumber=` + data.last_biblionumber;
                \$('#last-biblionumber').removeClass('disabled');
                \$('#last-biblionumber').attr('href', href);
            }
        });
    }
</script>
%;
}

1;