package Koha::Plugin::Pt::KEEPS::CirculationSectionsVisible;

use Modern::Perl;

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

our $VERSION = '1.2';

our $metadata = {
    name   => 'KEEPS - Circulation Sections Visible',
    author => 'Keep Solutions',
    description => 'Circulation table/configurations always visible',
    date_authored   => '2022-01-20',
    date_updated    => '2023-11-06',
    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 configure {
    my ($self, $args) = @_;
    my $cgi = $self->{'cgi'};

    unless ($cgi->param('save')) {
        my $lang_dialect = C4::Languages::getlanguage($cgi);

        my $template = $self->get_template({ file => 'configure.tt' });

        $template->param(
            lang_dialect => $lang_dialect,
            checkout_configs => $self->retrieve_data('checkout_configs'),
            checkout_table => $self->retrieve_data('checkout_table'),
            on_reserve_renew => $self->retrieve_data('on_reserve_renew'),
            on_reserve_return => $self->retrieve_data('on_reserve_return'),
            last_upgraded   => $self->retrieve_data('last_upgraded'),
        );

        $self->output_html( $template->output() );
    } else {
        $self->store_data(
            {
                checkout_configs => $cgi->param('checkout_configs'),
                checkout_table => $cgi->param('checkout_table'),
                on_reserve_renew => $cgi->param('on_reserve_renew'),
                on_reserve_return => $cgi->param('on_reserve_return'),
                last_configured_by => C4::Context->userenv->{'number'},
            }
        );
        $self->go_home();
    }
}

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

    my $checkout_configs = $self->retrieve_data('checkout_configs');
    my $checkout_table = $self->retrieve_data('checkout_table');
    my $on_reserve_renew = $self->retrieve_data('on_reserve_renew');
    my $on_reserve_return = $self->retrieve_data('on_reserve_return');

    if(!defined $checkout_configs) { $checkout_configs = "1"; };
    if(!defined $checkout_table) { $checkout_table = "1"; };
    if(!defined $on_reserve_renew) { $on_reserve_renew = "0"; };
    if(!defined $on_reserve_return) { $on_reserve_return = "0"; };

    return qq%
<script>
    var checkout_configs_active = "$checkout_configs";
    var checkout_table_active = "$checkout_table";
    var on_reserve_renew_active = "$on_reserve_renew";
    var on_reserve_return_active = "$on_reserve_return";

    \$(document).ready(function () {
        if(checkout_table_active == "1") {
            \$("label[for='issues-table-load-immediately']").hide();
            \$("#issues-table-load-immediately").hide();
            if ( typeof script !== 'undefined' && ! \$.cookie("issues-table-load-immediately-" + script) ) {
                \$.cookie("issues-table-load-immediately-" + script, true, { expires: 365, path: '/' });
            }
        } else {
            if ( typeof script !== 'undefined' && \$.cookie("issues-table-load-immediately-" + script) ) {
                \$.removeCookie("issues-table-load-immediately-" + script, { path: '/' });
            }
        }

        if(checkout_configs_active == "1") {
            \$("#show-circ-settings a").click();
        }

        if(window.pathname.match("cgi-bin/koha/circ/circulation.pl")) {
            waitForTable('#issues-table tbody tr:nth-child(2)', function(){
                \$('#issues-table tbody tr a[href*="reserve/request.pl"]').parent().addClass("on_hold");

                \$("#issues-table tbody tr[id*='-']").each(function() {
                    var itemnumber = \$(this).attr("id").split("-").shift();
                    if(on_reserve_renew_active == "1") {
                        if(\$(this).find('td:nth-last-child(2) .on_hold').length !== 0) {
                            var renew_checkbox = '<span class="" style=""><input type="checkbox" class="renew" id="renew_' + itemnumber + '" name="renew" value="' + itemnumber +'"><br/></span>';
                            \$(this).find('td:nth-last-child(2) .on_hold').before(renew_checkbox);
                        }
                    }

                    if(on_reserve_return_active == "1") {
                        if(\$(this).find('td:nth-last-child(1).on_hold').length !== 0) {
                            var return_checkbox = '<input type="checkbox" class="checkin" id="checkin_' + itemnumber + '" name="checkin" value="' + itemnumber +'"><br/>';
                            \$(this).find('td:nth-last-child(1).on_hold').prepend(return_checkbox);
                        }
                    }
                });
            });
        }
    });

   function waitForTable(selector, callback) {
        let checkExist = setInterval(function() {
            if (\$(selector).length) {
                clearInterval(checkExist);
                callback();
            }
        }, 100);
    }
</script>
%;
}

1;