package Koha::Plugin::Pt::KEEPS::BorrowersGenerateCardnumber;

use Modern::Perl;

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

our $VERSION = '1.2.3';

our $metadata = {
    name   => 'KEEPS - Borrowers Generate Cardnumber',
    author => 'Keep Solutions',
    description => 'Adds a button to generate a member cardnumber',
    date_authored   => '2022-01-25',
    date_updated    => '2024-01-12',
    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,
            option_cardnumber => $self->retrieve_data('option_cardnumber'),
            last_upgraded   => $self->retrieve_data('last_upgraded'),
        );

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


sub intranet_js {
    my ( $self ) = @_;
   
    my $option_cardnumber = $self->retrieve_data('option_cardnumber') || 'load';
    my $path = $self->get_plugin_http_path();

    return qq%
<script>
    var pathname = window.location.pathname;
    var gen_cardnumber_plugin_path = "$path";
    var option_cardnumber = "$option_cardnumber";
    var branchcode = \$('#libraries').val();
    var categorycode = \$('#categorycode_entry').val();

    if (pathname.match('/memberentry')) {

        var lang = \$('html').attr('lang');
        if (lang == null) {
            lang = \$('html').attr('xml:lang');
        }
        
        // Set the global configs to synchronous 
        \$.ajaxSetup({
            async: false
        });

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

        \$.ajaxSetup({
            async: true
        });

        if (\$('#cardnumber').val() == '' && option_cardnumber == 'load') {
            generateClick();
        }

        else if (option_cardnumber == 'button') {

        \$('#cardnumber').parent().append(`
            <div class="btn btn-default" style="margin-left: 10.5em;"
                onclick="generateClick();">
                \${TOKENS.new_cardnumber_button}
            </div>
        `);
        }
        else if (option_cardnumber == 'button_and_load') {
        \$('#cardnumber').parent().append(`
            <div class="btn btn-default" style="margin-left: 10.5em;"
                onclick="generateClick();">
                \${TOKENS.new_cardnumber_button}
            </div>
        `);
            if (\$('#cardnumber').val() == '') {
                generateClick();
            }
        }

    }

    function generateClick() {

        branchcode = \$('#libraries').val();

        \$.ajax({
            url: gen_cardnumber_plugin_path + '/generate-membercardnumber.pl',
            type: 'get',
            data: {
                branchcode: branchcode,
                categorycode: categorycode
            }
        }).done(function(data) {
            \$('#cardnumber').val(data.cardnumber);
        })
    }

</script>
%;
}

1;