package Koha::Plugin::Pt::KEEPS::card_print;

use Modern::Perl;

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

our $VERSION = '1.2';

our $metadata = {
    name   => 'KEEPS - Patron Card Print',
    author => 'Keep Solutions - joliveira@keep.pt',
    description => 'Allows printing the Patron card on their page.',
    date_authored   => '2023-06-07',
    date_updated    => '2023-07-13',
    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 ) = @_;

    $self->store_data(
            {
                layout_id_front => '0',
                template_id_front => '0',
                template_id_back => '0',
                layout_id_back => '0',
                front_card_hide => '0',
                back_card_hide => '0',
                f_b_card_hide => '0',
            }
        );

    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;
}


#-------------------------------CAS



sub configure {
    my ( $self, $args ) = @_;
    my $cgi = $self->{'cgi'};
    my $lang_dialect = C4::Languages::getlanguage($cgi);

    if ($cgi->param('set')) {
        my $front_card_hide = $cgi->param('front_card_hide') ? 1 : 0;
        my $back_card_hide = $cgi->param('back_card_hide') ? 1 : 0;
        my $f_b_card_hide = $cgi->param('f_b_card_hide') ? 1 : 0;
        my $layout_id_front = $cgi->param('layout_id_front');
        my $template_id_front = $cgi->param('template_id_front');
        my $template_id_back = $cgi->param('template_id_back');
        my $layout_id_back = $cgi->param('layout_id_back');

        handle_set($self, {
            front_card_hide => $front_card_hide,
            back_card_hide => $back_card_hide,
            f_b_card_hide => $f_b_card_hide,
            layout_id_front => $layout_id_front,
            template_id_front => $template_id_front,
            template_id_back => $template_id_back,
            layout_id_back => $layout_id_back
        });
    }
    else {
        handle_configure_page($self, {
            lang_dialect => $lang_dialect
        });
    }
}



sub handle_set {
    my ($self, $params) = @_;

    my $front_card_hide = $params->{'front_card_hide'};
    my $back_card_hide = $params->{'back_card_hide'};
    my $f_b_card_hide = $params->{'f_b_card_hide'};
    my $layout_id_front = $params->{'layout_id_front'};
    my $template_id_front = $params->{'template_id_front'};
    my $template_id_back = $params->{'template_id_back'};
    my $layout_id_back = $params->{'layout_id_back'};
    
    $self->store_data({
        front_card_hide => $front_card_hide,
        back_card_hide => $back_card_hide,
        f_b_card_hide => $f_b_card_hide,
        layout_id_front => $layout_id_front,
        template_id_front => $template_id_front,
        template_id_back => $template_id_back,
        layout_id_back => $layout_id_back
    });

    go_configure_page($self);
}


sub handle_configure_page {
    my ($self, $params) = @_;

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

    my $front_card_hide = $self->retrieve_data("front_card_hide");
    my $back_card_hide = $self->retrieve_data("back_card_hide");
    my $f_b_card_hide = $self->retrieve_data("f_b_card_hide");
    my $layout_id_front = $self->retrieve_data("layout_id_front");
    my $template_id_front = $self->retrieve_data("template_id_front");
    my $template_id_back = $self->retrieve_data("template_id_back");
    my $layout_id_back = $self->retrieve_data("layout_id_back");

    $template->param(
        lang_dialect => $params->{'lang_dialect'},
        front_card_hide => $front_card_hide,
        back_card_hide => $back_card_hide,
        f_b_card_hide => $f_b_card_hide,
        layout_id_front => $layout_id_front,
        template_id_front => $template_id_front,
        template_id_back => $template_id_back,
        layout_id_back => $layout_id_back
    );

    $self->output_html($template->output());
}


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

    my $class = $self->{'cgi'}->param('class');
    my $method = $self->{'cgi'}->param('method');

    my $query_str = "?class=$class&method=$method";

    print $self->{'cgi'}->redirect(
        -uri => $query_str
    );
}

#------------------END CAS




sub intranet_js {
    my ($self) = @_;
    my $layout_id_front = $self->retrieve_data('layout_id_front');
    my $template_id_front = $self->retrieve_data('template_id_front');
    my $template_id_back = $self->retrieve_data('template_id_back');
    my $layout_id_back = $self->retrieve_data('layout_id_back');
    my $front_card_hide = $self->retrieve_data('front_card_hide');
    my $back_card_hide = $self->retrieve_data('back_card_hide');
    my $f_b_card_hide = $self->retrieve_data('f_b_card_hide');
    my $path = $self->get_plugin_http_path();

    return qq%
        <script>

        \$(document).ready(function() {
            \$('head').append('<link rel="stylesheet" href="/plugin/Koha/Plugin/Pt/KEEPS/card_print/style.css" type="text/css" />');
        });

        \$(window).on("load", function () {
            var borrowernumber = getUrlParameter('borrowernumber');
            var card_print_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 i18n = {};
            \$.getJSON(card_print_plugin_path + `/i18n/\${lang}_menu.json`, function (data) {
                i18n = data;
            }).fail(function() {
                \$.getJSON(card_print_plugin_path + `/i18n/default_menu.json`, function (data) {
                    i18n = data;
                });
            });
            \$.ajaxSetup({
                async: true
            });
            var cardPrint_content = '<li class="li_cardPrint">' +
                '<a href="#" id="cardPrintDropdown" role="button" aria-haspopup="true" aria-expanded="false">' +
                i18n.card_reader +
                '<i class="fa fa-caret-right" aria-hidden="true"></i></a>' +
                '<div class="dropdown-menu cardPrint--container" aria-labelledby="cardPrintDropdown">' +
                '<a id="a_cardPrint--front" class="dropdown-item" href="/cgi-bin/koha/patroncards/create-pdf.pl?template_id=' + $template_id_front + '&layout_id=' + $layout_id_front + '&borrower_number=' + borrowernumber + '">' + i18n.card_reader_front + '</a>' +
                '<a id="a_cardPrint--back" class="dropdown-item" href="/cgi-bin/koha/patroncards/create-pdf.pl?template_id=' + $template_id_back + '&layout_id=' + $layout_id_back + '&borrower_number=' + borrowernumber + '">' + i18n.card_reader_back + '</a>' +
                '<a id="a_cardPrint--front-back" class="dropdown-item" href="/cgi-bin/koha/patroncards/create-pdf.pl?template_id=' + $template_id_front + '&layout_id=' + $layout_id_front + '&layout_back_id=' + $layout_id_back + '&borrower_number=' + borrowernumber + '">' + i18n.card_reader_front_back+ '</a>' +
                '</div>' +
                '</li>';

            \$("#toolbar").find("button i.fa-print").closest("div").find("ul").append(cardPrint_content);

            if ($front_card_hide) {
                \$("#a_cardPrint--front").hide();
            }

            if ($back_card_hide) {
                \$("#a_cardPrint--back").hide();
            }
            
            if ($f_b_card_hide) {
                \$("#a_cardPrint--front-back").hide();
            }

        });

        function getUrlParameter(sParam) {
            var sPageURL = decodeURIComponent(window.location.search.substring(1)),
                sURLVariables = sPageURL.split('&'),
                sParameterName,
                i;

            for (i = 0; i < sURLVariables.length; i++) {
                sParameterName = sURLVariables[i].split('=');

                if (sParameterName[0] === sParam) {
                    return sParameterName[1] === undefined ? true : sParameterName[1];
                }
            }
        }

        </script>
    %;
}

1;