package Koha::Plugin::Pt::KEEPS::AuthoritiesField100;

use Modern::Perl;

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

our $VERSION = '1.0';

our $metadata = {
    name   => 'KEEPS - Authorities Field 100',
    author => 'Keep Solutions',
    description => 'Generates field 100$a for authority record',
    date_authored   => '2022-01-20',
    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 ) = @_;

    return q%
<script>
    var pathname = window.location.pathname;
    if (pathname.match("/cgi-bin/koha/authorities/authorities.pl") && $("body#auth_authorities").length) {
        var value_of_100a = $('input[id^="tag_100_subfield_a_"]').val();
        if (!value_of_100a || value_of_100a.length === 0) {
            var today = new Date();
            var dd = today.getDate();
            var mm = today.getMonth() + 1;
            var yyyy = today.getFullYear();

            if (dd < 10) dd = '0' + dd;
            if (mm < 10) mm = '0' + mm;

            $('input[id^="tag_100_subfield_a_"]').val(yyyy + '' + mm + '' + dd + 'apory50      ba0');
        }
    }	
</script>
%;
}

1;