package Koha::Plugin::Pt::KEEPS::BiblioLabelPrint;

use Modern::Perl;

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

our $VERSION = '1.0';

our $metadata = {
    name   => 'KEEPS - Biblio Label Print',
    author => 'Keep Solutions',
    description => 'Allows to print item labels',
    date_authored   => '2022-01-25',
    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 $GB_ROOT_DIR = "/intranet-tmpl/lib/greybox/";

    return qq%
<script>
    var pathname = window.location.pathname;
    if (pathname.match('/cgi-bin/koha/catalogue/detail.pl') && (\$("body#catalog_detail").length)) {
        //LOAD GREYBOX
        window.GB_ROOT_DIR = "$GB_ROOT_DIR";
        \$.getScript( GB_ROOT_DIR + "AJS.js" );
        \$.getScript( GB_ROOT_DIR + "AJS_fx.js" );
        \$.getScript( GB_ROOT_DIR + "gb_scripts.js" );
        \$('head').append( \$('<link rel="stylesheet" type="text/css" />').attr('href', GB_ROOT_DIR + 'gb_styles.css') );

        if (\$('.items_table > tbody  > tr > .actions').length == 0) {
            \$('.items_table > thead  > tr').append('<th class="NoSort sorting_disabled" rowspan="1" colspan="1" aria-label="&amp;nbsp;">&nbsp;</th>');
            \$('.items_table > tbody  > tr').append('<td class="actions"></td>');
        }
        \$('.items_table > tbody  > tr').each(function () {
            var itemnumber = \$(this).find("input[name=itemnumber]").val();
            \$(this).find("td[class=actions]").append('<a class="btn btn-xs" id="export_label_' + itemnumber + '"><i class="fa fa-print"></i> Imprimir</a>');
            \$("#export_label_" + itemnumber).click(function () {
                GB_showCenter(_("Export labels"), "/cgi-bin/koha/labels/label-print.pl?item_number=" + itemnumber, 400, 800);
            });
        });
    }
</script>
%;
}

1;