[% USE raw %]
[% USE Asset %]
[% USE Koha %]
[% USE KohaDates %]
[% USE TablesSettings %]
[% USE AdditionalContents %]
[% PROCESS 'i18n.inc' %]
[% PROCESS 'html_helpers.inc' %]
[% SET OpacNav = AdditionalContents.get( location => "OpacNav", lang => lang, library => logged_in_user.branchcode || default_branch, blocktitle => 0 ) %]
[% SET OpacNavBottom = AdditionalContents.get( location => "OpacNavBottom", lang => lang, library => logged_in_user.branchcode || default_branch, blocktitle => 0 ) %]
[% INCLUDE 'doc-head-open.inc' %]
<title
    >[% FILTER collapse %]
        [% t("Your search history") | html %]
        &rsaquo; [% PROCESS 'library_name_title' %]
    [% END %]</title
>
[% INCLUDE 'doc-head-close.inc' %]
[% BLOCK cssinclude %]
[% END %]
</head>
[% INCLUDE 'bodytag.inc' bodyid='opac-search-history' bodyclass='scrollto' %]
[% INCLUDE 'masthead.inc' %]

<main class="main">
    [% WRAPPER breadcrumbs %]
        [% IF ( loggedinusername ) %]
            [% WRAPPER breadcrumb_item %]
                <a href="/cgi-bin/koha/opac-user.pl">[% INCLUDE 'patron-title.inc' patron = logged_in_user %]</a>
            [% END %]
            [% WRAPPER breadcrumb_item bc_active= 1 %]
                <span>Your search history</span>
            [% END %]
        [% ELSE %]
            [% WRAPPER breadcrumb_item bc_active= 1 %]
                <span>Search history</span>
            [% END %]
        [% END %]
    [% END #/ WRAPPER breadcrumbs %]

    <div class="container-fluid">
        <div class="row">
            [% SET column_class = "col order-first order-md-first order-lg-2" %]
            [% IF ( OpacNav||loggedinusername ) %]
                [% column_class = "col-md-12 col-lg-10 order-1" %]
                <div class="col col-lg-2 order-2 order-lg-1">
                    <div id="navigation"> [% INCLUDE 'navigation.inc' IsPatronPage=1 %] </div>
                </div>
            [% END %]
            <div class="[% column_class | html %]">
                <div id="searchhistory" class="maincontent">
                    <h1>Search history</h1>

                    [% IF Koha.Preference( 'OpacAuthorities' ) == 1 %]

                        [% WRAPPER tabs id= "tabs" %]
                            [% WRAPPER tabs_nav %]
                                [% WRAPPER tab_item tabname= "biblio_tab" bt_active= 1 %]<span>Catalog</span>[% END %]
                                [% WRAPPER tab_item tabname= "authority_tab" %]<span>Authority</span>[% END %]
                            [% END # /WRAPPER tabs_nav %]

                            [% WRAPPER tab_panels %]
                                [% WRAPPER tab_panel tabname="biblio_tab" bt_active= 1 %]
                                    <div id="biblio_tab" role="tabpanel" class="tab-pane active" aria-labelledby="biblio-tab-link"> [% PROCESS catalog_search_history %] </div>
                                    <!-- / #biblio_tab -->
                                [% END # /tab_panel# %]
                                [% WRAPPER tab_panel tabname="authority_tab" %]
                                    <div id="authority_tab" role="tabpanel" class="tab-pane" aria-labelledby="authority-tab-link"> [% PROCESS authority_search_history %] </div>
                                    <!-- / #authority_tab -->
                                [% END # /tab_panel# %]
                            [% END # /WRAPPER tab_panels %]
                        [% END # /WRAPPER tabs %]
                    [% ELSE %]
                        [% PROCESS catalog_search_history %]
                    [% END %]
                </div>
                <!-- / #searchhistory -->
            </div>
            <!-- / .col-lg-10/12 -->
        </div>
        <!-- / .row -->
    </div>
    <!-- / .container-fluid -->
</main>
<!-- / #main -->

[% BLOCK catalog_search_history %]
    <div id="current_biblio">
        [% IF ( current_biblio_searches ) %]
            <h2>Current session</h2>
            <form action="/cgi-bin/koha/opac-search-history.pl" method="post">
                [% INCLUDE 'csrf-token.inc' %]
                <legend class="sr-only">Toolbar control</legend>

                [% INCLUDE 'toolbar_controls' %]
                <input type="hidden" name="op" value="cud-delete" />
                <table class="historyt table table-bordered table-striped">
                    <caption class="sr-only">Current session</caption>
                    <thead>
                        <tr>
                            <th class="no-sort">
                                <i data-bs-toggle="tooltip" title="Check the boxes in this column to select searches for deletion" data-bs-placement="right" class="fa fa-info-circle" tabindex="0"></i>
                            </th>
                            <th>Date</th>
                            <th>Search</th>
                            <th>Results</th>
                            <th class="dtr-control-col"></th>
                        </tr>
                    </thead>
                    <tbody>
                        [% FOREACH s IN current_biblio_searches %]
                            <tr>
                                <td class="selectcol">
                                    <input type="checkbox" name="id" value="[% s.id | html %]" id="result[% s.id | html %]" />
                                </td>
                                <td data-order="[% s.time | html %]">
                                    <label for="result[% s.id | html %]"> [% s.time |$KohaDates  with_hours => 1 %] </label>
                                </td>
                                <td>
                                    <a
                                        href="[% OPACBaseURL | url %]/cgi-bin/koha/opac-search.pl?[% query_cgi | $raw %][% limit_cgi | $raw %]&amp;[% s.query_cgi | $raw %]&amp;count=[% countrss | uri %]&amp;sort_by=acqdate_dsc&amp;format=rss"
                                        aria-label="Subscribe to this search"
                                        class="rsssearchlink"
                                    >
                                        <i class="fa fa-rss rsssearchicon" aria-hidden="true" title="Subscribe to this search"></i>
                                    </a>
                                    <a href="/cgi-bin/koha/opac-search.pl?[% s.query_cgi | $raw %]">[% s.query_desc | html %]</a>
                                </td>
                                <td>[% s.total | html %]</td>
                                <td></td>
                            </tr>
                        [% END %]
                    </tbody>
                </table>
                <input type="submit" class="btn btn-danger remove-selected" value="Remove selected searches" />
            </form>
        [% END # IF ( current_biblio_searches ) %]
    </div>
    <!-- / #current_biblio -->
    <hr />
    <div id="previous_biblio">
        [% IF ( previous_biblio_searches ) %]
            <h2>Previous sessions</h2>
            <form action="/cgi-bin/koha/opac-search-history.pl" method="post">
                [% INCLUDE 'csrf-token.inc' %]
                <legend class="sr-only">Toolbar control</legend>
                [% INCLUDE 'toolbar_controls' %]

                <input type="hidden" name="op" value="cud-delete" />
                <table class="historyt table table-bordered table-striped">
                    <caption class="sr-only">Previous sessions</caption>
                    <thead>
                        <tr>
                            <th class="no-sort">
                                <i data-bs-toggle="tooltip" title="Check the boxes in this column to select searches for deletion" data-bs-placement="right" class="fa fa-info-circle" tabindex="0"></i>
                            </th>
                            <th>Date</th>
                            <th>Search</th>
                            <th>Results</th>
                            <th class="dtr-control-col"></th>
                        </tr>
                    </thead>
                    <tbody>
                        [% FOREACH s IN previous_biblio_searches %]
                            <tr>
                                <td class="selectcol">
                                    <input type="checkbox" name="id" value="[% s.id | html %]" id="result[% s.id | html %]" />
                                </td>
                                <td data-order="[% s.time | html %]">
                                    <label for="result[% s.id | html %]"> [% s.time |$KohaDates  with_hours => 1 %] </label>
                                </td>
                                <td>
                                    <a
                                        href="[% OPACBaseURL | url %]/cgi-bin/koha/opac-search.pl?[% query_cgi | $raw %][% limit_cgi | $raw %]&amp;[% s.query_cgi | $raw %]&amp;count=[% countrss | uri %]&amp;sort_by=acqdate_dsc&amp;format=rss"
                                        aria-label="Subscribe to this search"
                                        class="rsssearchlink"
                                    >
                                        <i class="fa fa-rss rsssearchicon" aria-hidden="true" title="Subscribe to this search"></i>
                                    </a>
                                    <a href="/cgi-bin/koha/opac-search.pl?[% s.query_cgi | $raw %]"> [% s.query_desc | html %] </a>
                                </td>
                                <td>[% s.total | html %]</td>
                                <td></td>
                            </tr>
                        [% END %]
                    </tbody>
                </table>
                <input type="submit" class="btn btn-danger remove-selected" value="Remove selected searches" />
            </form>
        [% END # IF ( previous_biblio_searches ) %]
    </div>
    <!-- / #previous_biblio -->

    [% IF !current_biblio_searches && !previous_biblio_searches %]
        <p>Your catalog search history is empty.</p>
    [% END %]
[% END %]

[% BLOCK authority_search_history %]
    <div id="current_authority">
        [% IF ( current_authority_searches ) %]
            <h2>Current session</h2>
            <form action="/cgi-bin/koha/opac-search-history.pl" method="post">
                [% INCLUDE 'csrf-token.inc' %]
                <legend class="sr-only">Toolbar control</legend>
                [% INCLUDE 'toolbar_controls' %]
                <input type="hidden" name="op" value="cud-delete" />
                <table class="historyt table table-bordered table-striped">
                    <caption class="sr-only">Current session</caption>
                    <thead>
                        <tr>
                            <th class="no-sort">
                                <i data-bs-toggle="tooltip" title="Check the boxes in this column to select searches for deletion" data-bs-placement="right" class="fa fa-info-circle" tabindex="0"></i>
                            </th>
                            <th>Date</th>
                            <th>Search</th>
                            <th>Results</th>
                            <th class="dtr-control-col"></th>
                        </tr>
                    </thead>
                    <tbody>
                        [% FOREACH s IN current_authority_searches %]
                            <tr>
                                <td class="selectcol">
                                    <input type="checkbox" name="id" value="[% s.id | html %]" id="result[% s.id | html %]" />
                                </td>
                                <td data-order="[% s.time | html %]">
                                    <label for="result[% s.id | html %]"> [% s.time |$KohaDates  with_hours => 1 %] </label>
                                </td>
                                <td><a href="/cgi-bin/koha/opac-authorities-home.pl?[% s.query_cgi | $raw %]">[% s.query_desc | html %]</a></td>
                                <td>[% s.total | html %]</td>
                                <td></td>
                            </tr>
                        [% END %]
                    </tbody>
                </table>
                <input type="submit" class="btn btn-danger remove-selected" value="Remove selected searches" />
            </form>
        [% END # / IF ( current_authority_searches ) %]
    </div>
    <!-- / #current_authority -->
    <hr />
    <div id="previous_authority">
        [% IF ( previous_authority_searches ) %]
            <h2>Previous sessions</h2>
            <form action="/cgi-bin/koha/opac-search-history.pl" method="post">
                [% INCLUDE 'csrf-token.inc' %]
                <legend class="sr-only">Toolbar control</legend>
                [% INCLUDE 'toolbar_controls' %]
                <input type="hidden" name="op" value="cud-delete" />
                <table class="historyt table table-bordered table-striped">
                    <caption class="sr-only">Previous sessions</caption>
                    <thead>
                        <tr>
                            <th class="no-sort">
                                <i data-bs-toggle="tooltip" title="Check the boxes in this column to select searches for deletion" data-bs-placement="right" class="fa fa-info-circle" tabindex="0"></i>
                            </th>
                            <th>Date</th>
                            <th>Search</th>
                            <th>Results</th>
                            <th class="dtr-control-col"></th>
                        </tr>
                    </thead>
                    <tbody>
                        [% FOREACH s IN previous_authority_searches %]
                            <tr>
                                <td class="selectcol">
                                    <input type="checkbox" name="id" value="[% s.id | html %]" id="result[% s.id | html %]" />
                                </td>
                                <td data-order="[% s.time | html %]">
                                    <label for="result[% s.id | html %]"> [% s.time |$KohaDates  with_hours => 1 %] </label>
                                </td>
                                <td><a href="/cgi-bin/koha/opac-authorities-home.pl?[% s.query_cgi | $raw %]">[% s.query_desc | html %]</a></td>
                                <td>[% s.total | html %]</td>
                                <td></td>
                            </tr>
                        [% END %]
                    </tbody>
                </table>
                <input type="submit" class="btn btn-danger remove-selected" value="Remove selected searches" />
            </form>
        [% END # / IF ( previous_authority_searches ) %]
    </div>

    [% IF !current_authority_searches && !previous_authority_searches %]
        <p>Your authority search history is empty.</p>
    [% END %]
[% END %]

[% INCLUDE 'opac-bottom.inc' %]

[% BLOCK toolbar_controls %]
    [%# Identical controls shown at the top of each table of search history %]
    <div class="selections-toolbar toolbar">
        <div class="check_control">
            <a class="CheckAll btn btn-link btn-sm" href="#">Select all</a>
            <a class="CheckNone btn btn-link btn-sm" href="#">Clear all</a>
        </div>
        <div class="links">
            <span class="selections">Select searches to: </span>
            <a href="#" class="removeitems disabled btn btn-sm btn-link"><i class="fa fa-trash-can" aria-hidden="true"></i> Delete</a>
        </div>
    </div>
[% END %]

[% BLOCK jsinclude %]
    [% INCLUDE 'datatables.inc' %]
    <script>
        $(document).ready(function () {
            // We show table ordered by descending dates by default
            // (so that the more recent query is shown first)
            let history_tables = $(".historyt").kohaTable({
                order: [[1, "desc"]],
                dom: '<"top"<"table_entries"><"table_controls"fB>>t',
                columnDefs: [{ className: "dtr-control", orderable: false, targets: -1 }],
                language: {
                    search: "_INPUT_",
                    searchPlaceholder: _("Search"),
                },
                responsive: {
                    details: { type: "column", target: -1 },
                },
            });

            $('a[data-bs-toggle="tab"]').on("shown.bs.tab", function (e) {
                history_tables.DataTable().responsive.recalc();
            });

            $(".CheckNone").click(function (e) {
                e.preventDefault();
                var form = $(this).parents("form").get(0);
                $(form).find("input:checkbox").prop("checked", false);
                enableCheckboxActions(form);
            });
            $(".CheckAll").click(function (e) {
                e.preventDefault();
                var form = $(this).parents("form").get(0);
                $(form).find("input:checkbox").prop("checked", true);
                enableCheckboxActions(form);
            });

            $("input:checkbox").click(function () {
                var form = $(this).parents("form").get(0);
                enableCheckboxActions(form);
            });

            $(".removeitems,.remove-selected").click(function (e) {
                e.preventDefault();
                var form = $(this).parents("form").get(0);
                var ids = $(form).find("input:checkbox:checked");
                if ($(ids).length < 1) {
                    return false;
                }
                if (confirm(_("Are you sure you want to delete selected search history entries?"))) {
                    $(form).submit();
                }
                return false;
            });
            $(".remove-selected").addClass("disabled");
        });

        function enableCheckboxActions(form) {
            // Enable/disable controls if checkboxes are checked
            var checkedBoxes = $(form).find("input:checkbox:checked");
            if ($(checkedBoxes).size()) {
                $(form).find(".selections").html(_("With selected searches: "));
                $(form).find(".selections-toolbar .links a").removeClass("disabled");
                $(form).find(".remove-selected").removeClass("disabled");
            } else {
                $(form).find(".selections").html(_("Select searches to: "));
                $(form).find(".selections-toolbar .links a").addClass("disabled");
                $(form).find(".remove-selected").addClass("disabled");
            }
        }
    </script>
[% END %]
