package Koha::Plugin::Pt::KEEPS::FCSHReserves;

use Modern::Perl;

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

our $VERSION = '1.0';

our $metadata = {
    name   => 'KEEPS - Reserves (FCSH)',
    author => 'Keep Solutions',
    description => 'Changes issues and reserves following FCSH rules',
    date_authored   => '2024-07-19',
    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;
}

sub cronjob {
    my $arg = $_[1];
    my $dir = C4::Context->config("pluginsdir");
    my $path = $dir . "/Koha/Plugin/Pt/KEEPS/FCSHReserves";

    my $PERL= "/usr/bin/perl";
    my $PERL5LIB= "/KEEPS/koha/lib/";

    system("$PERL -I $PERL5LIB $path/reserves.pl $arg");
}


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

    return 1;
}

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

    return 1;
}

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

    my $DIR = "/KEEPS/koha";
    my $CGI_BIN= $DIR . "/intranet/cgi-bin/circ";
    my $OPAC_CGI = $DIR . "/opac/cgi-bin/opac";

    system("rm $CGI_BIN/returns.pl");
    system("mv $CGI_BIN/returns_bkp.pl $CGI_BIN/returns.pl");
    system("rm $OPAC_CGI/opac-reserve.pl");
    system("mv $OPAC_CGI/opac-reserve_bkp.pl $OPAC_CGI/opac-reserve.pl");

    return 1;
}

sub startup {
    my $pluginsdir = C4::Context->config("pluginsdir");
    my $path = $pluginsdir . "/Koha/Plugin/Pt/KEEPS/FCSHReserves";

    my $DIR = "/KEEPS/koha";
    my $CGI_BIN= $DIR . "/intranet/cgi-bin/circ";
    my $OPAC_CGI = $DIR . "/opac/cgi-bin/opac";

    system("mv $CGI_BIN/returns.pl $CGI_BIN/returns_bkp.pl");
    system("cp $path/returns.pl $CGI_BIN/returns.pl");
    system("mv $OPAC_CGI/opac-reserve.pl $OPAC_CGI/opac-reserve_bkp.pl");
    system("cp $path/opac-reserve.pl $OPAC_CGI/opac-reserve.pl");
}

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

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

    return qq%
<script>
    \$(document).ready(function () {
        var pathname = window.location.pathname;

        if (pathname.match("/request.pl")) {
            // UNCHECK NEXT AVAILABLE
            \$("#requestany").prop("checked", false);

            // HIDE NEXT AVAILABLE OPTIONS
            \$("#requestany").parent().hide();
            \$("#holds_to_place_count").parent().hide();
            \$("#hold-request-form button[type=submit]:first").parent().hide();

            // BLOCK RESERVES IF ALREADY IN POSSESION
            if (\$('.alert').text().indexOf("posse") >= 0 ||
                \$('.alert').text().indexOf("possession") >= 0) {
                \$('input[name="checkitem"]').attr('disabled', true);
            }

            // SET PICKUP LIBRARY
            \$("#pickup").parent().hide();
            \$('input[name="checkitem"]').click(function () {
                var \$library = \$('input[name="checkitem"]:checked').closest('tr').find('td').eq(4).text();
                \$("#pickup option").filter(function () {
                    return \$(this).text() == \$library.trim();
                }).attr('selected', true);
            });
            \$('table select[name="pickup"]').each(function (index) {
                \$(this).attr('disabled', 'disabled');
            });
        }

    });
</script>
%;
}


1;
