[% USE raw %]
[% USE Asset %]
[% PROCESS 'i18n.inc' %]
[% SET footerjs = 1 %]
[% PROCESS 'authorities-search-results.inc' %]
[% INCLUDE 'doc-head-open.inc' %]
<title>[% FILTER collapse %]
    [% IF op == 'list' %]
        [% t("Confirm selection") | html %] &rsaquo;
    [% END %]
    [% IF op == 'enqueued' %]
        [% t("Job enqueued") | html %] &rsaquo;
    [% END %]
    [% t("Batch record deletion") | html %] &rsaquo;
    [% t("Cataloging") | html %] &rsaquo;
    [% t("Koha") | html %]
[% END %]</title>
[% INCLUDE 'doc-head-close.inc' %]
</head>

<body id="tools_batch_delete_records" class="tools">
[% WRAPPER 'header.inc' %]
    [% INCLUDE 'cat-search.inc' %]
[% END %]

[% WRAPPER 'sub-header.inc' %]
    [% WRAPPER breadcrumbs %]
        [% WRAPPER breadcrumb_item %]
            <a href="/cgi-bin/koha/cataloguing/cataloging-home.pl">Cataloging</a>
        [% END %]
        [% IF op == 'list' || op == 'enqueued' %]
            [% WRAPPER breadcrumb_item %]
                <a href="/cgi-bin/koha/tools/batch_delete_records.pl">Batch record deletion</a>
            [% END %]
        [% END %]
        [% IF op == 'list' %]
            [% WRAPPER breadcrumb_item bc_active= 1 %]
                [% t("Confirm selection") | html %]
            [% END %]
        [% ELSIF OP == 'enqueued' %]
            [% WRAPPER breadcrumb_item bc_active= 1 %]
                [% t("Job enqueued") | html %]
            [% END %]
        [% ELSE %]
            [% WRAPPER breadcrumb_item bc_active= 1 %]
                <span>Batch record deletion</span>
            [% END %]
        [% END %]
    [% END #/ WRAPPER breadcrumbs %]
[% END #/ WRAPPER sub-header.inc %]

<div class="main container-fluid">
    <div class="row">
        <div class="col-sm-10 col-sm-push-2">
            <main>
                [% INCLUDE 'messages.inc' %]

  <h1>Batch record deletion</h1>
  [% FOREACH message IN messages %]
    [% IF message.type == 'success' %]
      <div class="dialog message">
    [% ELSIF message.type == 'warning' %]
      <div class="dialog alert">
    [% ELSIF message.type == 'error' %]
      <div class="dialog alert" style="margin:auto;">
    [% END %]
    [% IF message.code == 'biblio_not_exists' %]
      <span>The biblionumber [% message.biblionumber | html %] does not exist in the database.</span>
    [% ELSIF message.code == 'authority_not_exists' %]
      <span>The authority ID [% message.authid | html %] does not exist in the database.</span>
    [% ELSIF message.code == 'cannot_enqueue_job' %]
        <span>Cannot enqueue this job.</span>
    [% ELSIF message.code == 'biblio_not_exists' %]
        <span>Bibliographic record [% message.biblionumber | html %] does not exist in the database.</span>
    [% ELSIF message.code == 'authority_not_exists' %]
        <span>Authority record [% message.authid | html %] does not exist in the database.</span>
    [% END %]

    [% IF message.error %]
      <span>(The error was: [% message.error | html %], see the Koha log file for more information).</span>
    [% END %]
    </div>
  [% END %]
  [% IF op == 'form' %]
    <form method="post" enctype="multipart/form-data" action="/cgi-bin/koha/tools/batch_delete_records.pl" id="record_ids_selection">
      <fieldset class="rows">
        <legend>Record type</legend>
        <ol>
          <li><label for="biblio_type">Bibliographic: </label><input type="radio" name="recordtype" value="biblio" id="biblio_type" checked="checked" /></li>
          <li><label for="authority_type">Authorities: </label><input type="radio" name="recordtype" value="authority" id="authority_type" /></li>
        </ol>
      </fieldset>

        <br class="clear" />

        [% WRAPPER tabs id= "batch_del_form" %]
            [% WRAPPER tabs_nav %]
                [% WRAPPER tab_item tabname= "uploadfile_tab" bt_active= 1 %] <span>Upload a file</span> [% END %]
                [% IF lists.count %]
                    [% WRAPPER tab_item tabname= "shelves_tab" %] <span>Select a list of records</span> [% END %]
                [% END %]
                [% WRAPPER tab_item tabname= "enterlist_tab" %] <span>Enter a list of record numbers</span> [% END %]
            [% END # /WRAPPER tabs_nav %]

            [% WRAPPER tab_panels %]
                [% WRAPPER tab_panel tabname="uploadfile_tab" bt_active= 1 %]
                    <fieldset class="rows">
                        <ol>
                        <li><label for="uploadfile">File: </label> <input type="file" id="uploadfile" name="uploadfile" /></li>
                        </ol>
                    </fieldset>
                [% END # /WRAPPER tab_panel %]

                [% IF lists.count %]
                    [% WRAPPER tab_panel tabname="shelves_tab" %]
                        <fieldset class="rows">
                            <ol>
                                <li>
                                    <label for="shelf_number">List: </label>
                                    <select name="shelf_number" id="shelf_number">
                                        <option value="">Select a list</option>
                                        [% FOREACH list IN lists %]
                                            <option value="[% list.shelfnumber | html %]">[% list.shelfname | html %]</option>
                                        [% END %]
                                    </select>
                                </li>
                            </ol>
                        </fieldset>
                    [% END # /WRAPPER tab_panel %]
                [% END # /IF lists.count %]

                [% WRAPPER tab_panel tabname="enterlist_tab" %]
                    <fieldset class="rows">
                        <ol>
                            <li>
                                <label for="recordnumber_list">IDs: </label>
                                <textarea rows="10" cols="30" id="recordnumber_list" name="recordnumber_list"></textarea>
                                <div class="hint">Enter a list of biblionumbers or authority IDs, one per line.</div>
                            </li>
                        </ol>
                    </fieldset>
                [% END # /WRAPPER tab_panel %]
            [% END # /WRAPPER tab_panels %]
        [% END # /WRAPPER tabs %]

      <fieldset class="action">
        <input type="hidden" name="op" value="list" />
        <input type="submit" class="btn btn-primary" value="Continue" />
        <a class="cancel" href="/cgi-bin/koha/cataloguing/cataloging-home.pl">Cancel</a>
      </fieldset>
    </form>
  [% ELSIF op == 'list' %]
    [% IF records %]
      [% IF recordtype == 'biblio' %]
        <div class="btn-toolbar selections-toolbar">
          <a id="selectall" href="#"><i class="fa fa-check"></i> Select all</a>
          | <a id="clearall" href="#"><i class="fa fa-times"></i> Clear all</a>
          | <a id="selectwithoutitems" href="#">Select without items</a>
          | <a id="selectnotreserved" href="#">Select without holds</a>
          | <a id="selectwithoutsubscriptions" href="#">Select without subscriptions</a>
        </div>

        <div class="page-section">
        <form action="/cgi-bin/koha/tools/batch_delete_records.pl" method="post" id="selectrecords">
          <table id="biblios" class="records">
            <thead>
              <tr>
                <th></th>
                <th>Biblionumber</th>
                <th>Title</th>
                <th>Items</th>
                <th>Holds</th>
                <th>Checkouts</th>
                <th>Subscriptions</th>
              </tr>
            </thead>
            <tbody>
              [% FOR biblio IN records %]
                <tr>
                  <td><input type="checkbox" name="record_id" id="record_id_[% biblio.biblionumber | html %]" value="[% biblio.biblionumber | html %]" data-items="[% biblio.itemnumbers.size | html %]" data-issues="[% biblio.issues_count | html %]" data-reserves="[% biblio.holds_count | html %]" data-subscriptions="[% biblio.subscriptions_count | html %]" /></td>
                  <td><label for="record_id_[% biblio.biblionumber | html %]">[% biblio.biblionumber | html %]</label></td>
                  <td>[% INCLUDE 'biblio-title.inc' link = 1 %]</td>
                  <td><a href="/cgi-bin/koha/catalogue/moredetail.pl?biblionumber=[% biblio.biblionumber | uri %]">[% biblio.itemnumbers.size | html %]</a></td>
                  <td><a href="/cgi-bin/koha/reserve/request.pl?biblionumber=[% biblio.biblionumber | uri %]">[% biblio.holds_count | html %]</a></td>
                  <td><a href="/cgi-bin/koha/catalogue/issuehistory.pl?biblionumber=[% biblio.biblionumber | uri %]">[% biblio.issues_count | html %]</a></td>
                  <td><a href="/cgi-bin/koha/serials/serials-search.pl?searched=1&biblionumber=[% biblio.biblionumber | uri %]">[% biblio.subscriptions_count | html %]</a></td>
                </tr>
              [% END %]
            </tbody>
          </table>
          <div class="note"><i class="fa fa-exclamation"></i> Reminder: this action will delete all selected bibliographic records, attached subscriptions, existing holds, and attached items!</div>
      [% ELSE %]
        <div class="btn-toolbar selections-toolbar">
          <a id="selectall" href="#"><i class="fa fa-check"></i> Select all</a>
          | <a id="clearall" href="#"><i class="fa fa-times"></i> Clear all</a>
          | <a id="clearlinkedtobiblio" href="#">Clear used authorities</a>
        </div>
        <form action="/cgi-bin/koha/tools/batch_delete_records.pl" method="post" id="selectauths">
          <table id="authorities" class="records">
            <thead>
              <tr>
                <th></th>
                <th>AuthID</th>
                <th>Summary</th>
                <th>Used in</th>
              </tr>
            </thead>
            <tbody>
              [% FOR authority IN records %]
                <tr>
                  <td><input type="checkbox" name="record_id" value="[% authority.authid | html %]" data-usage="[% authority.count_usage | html %]" /></td>
                  <td><a href="/cgi-bin/koha/authorities/detail.pl?authid=[% authority.authid | uri %]">[% authority.authid | html %]</a></td>
                  <td>[% PROCESS authresult summary=authority.summary authid=authority.authid %]</td>
                  <td><a href="/cgi-bin/koha/catalogue/search.pl?type=intranet&amp;op=do_search&amp;idx=an,phr&amp;q=[% authority.authid | uri %]">[% authority.count_usage | html %] record(s)</a></td>
                </tr>
              [% END %]
            </tbody>
          </table>
          <div class="note"><i class="fa fa-exclamation"></i> Reminder: this action will delete all selected authorities!</div>
      [% END %]
        <fieldset class="action">
          <input type="hidden" name="op" value="delete" />
          <input type="hidden" name="recordtype" value="[% recordtype | html %]" />
          <input type="submit" class="btn btn-primary" value="Delete selected records" />
          <a class="cancel" href="/cgi-bin/koha/tools/batch_delete_records.pl">Cancel</a>
        </fieldset>
      </form>
      </div>
    [% ELSE %]
      There are no record IDs defined.
    [% END %]
  [% ELSIF op == 'enqueued' %]
    <div class="dialog message">
        <h1>The job has been enqueued!</h1>
        <p>It will be processed as soon as possible.</p>
      <p><a href="/cgi-bin/koha/admin/background_jobs.pl?op=view&id=[% job_id | uri %]" title="View detail of the enqueued job">View detail of the enqueued job</a>
      | <a href="/cgi-bin/koha/tools/batch_delete_records.pl" title="New batch record deletion">New batch record deletion</a></p>
    </div>
  [% ELSE %]
    No action defined for the template.
  [% END %]

            </main>
        </div> <!-- /.col-sm-10.col-sm-push-2 -->

        <div class="col-sm-2 col-sm-pull-10">
            <aside>
                [% INCLUDE 'cat-menu.inc' %]
            </aside>
        </div> <!-- /.col-sm-2.col-sm-pull-10 -->
     </div> <!-- /.row -->

[% MACRO jsinclude BLOCK %]
    [% INCLUDE 'datatables.inc' %]
    <script>
        var MSG_CANNOT_BE_DELETED = _("This record cannot be deleted, at least one item is currently checked out.");
        $(document).ready(function() {

            $("input[type='radio']").click(function() {
                if ($(this).attr('id') == 'authority_type') {
                    $("a[href='#shelves_tab_panel']").parent().hide();
                } else if ($(this).attr('id') == 'biblio_type') {
                    $("a[href='#shelves_tab_panel']").parent().show();
                }
            });

            $("#selectall").click(function(e){
                e.preventDefault();
                $(".records input[type='checkbox']:not(:disabled)").each(function(){
                    $(this).prop("checked", true);
                });
            });

            $("#clearall").click(function(e){
                e.preventDefault();
                $(".records input[type='checkbox']:not(:disabled)").each(function(){
                    $(this).prop("checked", false);
                });
            });

            $("#selectwithoutitems").click(function(e){
                e.preventDefault();
                $(".records input[type='checkbox']:not(:disabled)").each(function(){
                    if( $(this).data("items") == 0 ){
                        $(this).prop("checked", true );
                    } else {
                        $(this).prop("checked", false );
                    }
                });
            });

            $("#selectnotreserved").click(function(e){
                e.preventDefault();
                $(".records input[type='checkbox']:not(:disabled)").each(function(){
                    if( $(this).data("reserves") == 0 ){
                        $(this).prop("checked", true );
                    } else {
                        $(this).prop("checked", false );
                    }
                });
            });

            $("#selectwithoutsubscriptions").click(function(e){
                e.preventDefault();
                $(".records input[type='checkbox']:not(:disabled)").each(function(){
                    if( $(this).data("subscriptions") == 0 ){
                        $(this).prop("checked", true );
                    } else {
                        $(this).prop("checked", false );
                    }
                });
            });

            $("#clearlinkedtobiblio").click(function(e){
                e.preventDefault();
                $(".records input[type='checkbox']:not(:disabled)").each(function(){
                    if( $(this).data("usage") == 0 ){
                        $(this).prop("checked", true );
                    } else {
                        $(this).prop("checked", false );
                    }
                });
            });

          $("#selectall").click();

          //Show a red cross if a biblio cannot be deleted
          [% IF recordtype == 'biblio' %]
            $(".records input:checkbox[data-issues!='0']").each(function(){
              $(this).parents('tr').find('td').css('background-color', '#ffff99')
              $(this).replaceWith("<span class='error'><i class='fa fa-times fa-lg'></i></span>")
              $(".records i").attr('title', MSG_CANNOT_BE_DELETED)
            });
          [% END %]

          $("table#biblios").dataTable($.extend(true, {}, dataTablesDefaults, {
            "columnDefs": [
              { "targets": [ 0 ], "orderable": false, "searchable":  false },
              { "targets": [ 3, 4 ], "type": "num-html" }
            ],
            "dom": 't',
            "order": [],
            "paginate": false
          }));

          $("table#authorities").dataTable($.extend(true, {}, dataTablesDefaults, {
            "columnDefs": [
              { "targets": [ 0 ], "orderable": false, "searchable":  false },
              { "targets": [ 3 ], "type": "num-html" }
            ],
            "dom": 't',
            "order": [],
            "paginate": false
          }));

          $("#selectrecords").on("submit",function(){
            var nb_checked = $("#selectrecords").find("input[type='checkbox'][name='record_id']:checked").size();
            if (nb_checked == 0){
              alert(_("No records have been selected."));
              return false;
            }
          });

          $("#selectauths").on("submit",function(){
            var checked = $("#selectauths").find("input[type='checkbox'][name='record_id']:checked").size();
            if (checked == 0) {
              alert(_("No authorities have been selected."));
              return false;
            }
          });

          $("#record_ids_selection").on("submit", function(e){
            var tab = $(this).find('#batch_del_form:active:first a').attr('href');
            if ( tab == '#uploadfile_tab' ) {
                $("#shelf_number").empty('');
                $("#recordnumber_list").val('');
            } else if ( tab == '#shelves_tab' ) {
                $("#uploadfile").val('')
                $("#recordnumber_list").val('');
            } else { // enterlist
                $("#uploadfile").val('')
                $("#shelf_number").empty('');
            }
          });

        });
    </script>
[% END %]

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