package Koha::Plugin::Pt::KEEPS::CirculationPrintSlip;

use Modern::Perl;

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

our $VERSION = '1.2';

our $metadata = {
    name   => 'KEEPS - Circulation Print Slip',
    author => 'Keep Solutions',
    description => 'Check In, Check Out and Renewals slips',
    date_authored   => '2022-01-25',
    date_updated    => '2022-12-15',
    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;
	var print_slip_plugin_path = "$path";

    var lang = \$('html').attr('lang');
    if (lang == null) {
        lang = \$('html').attr('xml:lang');
    }

    // Set the global configs to synchronous 
    \$.ajaxSetup({
        async: false
    });

    var print_slp_TOKENS = {};
    \$.getJSON(print_slip_plugin_path + `/i18n/\${lang}.json`, function (data) {
        print_slp_TOKENS = data;
    }).fail(function() { 
        \$.getJSON(print_slip_plugin_path + `/i18n/default.json`, function (data) {
            print_slp_TOKENS = data;
        }); 
    });

    \$.ajaxSetup({
        async: true
    });
    
    if (document.getElementById("printsummary") != null) {
        var summary_parent = \$("#printsummary").parent().parent();
        summary_parent.empty();

        summary_parent.append('<li><a id="printsummary" href="#">' + print_slp_TOKENS.summary_slip + '</a></li>');
        summary_parent.append('<li><a id="printquickslip" href="#">' + print_slp_TOKENS.checkout_slip + '</a></li>');
        summary_parent.append('<li><a id="printquickslip2" href="#">' + print_slp_TOKENS.checkin_slip + '</a></li>');
        summary_parent.append('<li><a id="printquickslip3" href="#">' + print_slp_TOKENS.renewals_slip + '</a></li>');

        var href = \$('#changepassword').attr('href');
        var slip_borrowernumber = href.substring(href.lastIndexOf('=') + 1);

        \$("#printquickslip2").click(function(){
            window.open(print_slip_plugin_path + "/printslip.pl?borrowernumber=" + slip_borrowernumber +  "&print=qslip2", "printwindow");
            \$(".btn-group").removeClass("open");
            return false;
        });

        \$("#printquickslip3").click(function(){
            window.open(print_slip_plugin_path + "/printslip.pl?borrowernumber=" + slip_borrowernumber +  "&print=qslip3", "printwindow");
            \$(".btn-group").removeClass("open");
            return false;
        });
    }
</script>
%;
}

1;