[% USE raw %]
[% USE Asset %]
[% PROCESS 'i18n.inc' %]
[% SET footerjs = 1 %]
[% INCLUDE 'doc-head-open.inc' %]
<title>[% FILTER collapse %]
    [% IF ( op == 'edit_form' ) %]
        [% t("Rules") | html %] &rsaquo;
    [% END %]
    [% t("Automatic item modifications by age") | html %] &rsaquo;
    [% t("Cataloging") | html %] &rsaquo;
    [% t("Koha") | html %]
[% END %]</title>
[% INCLUDE 'doc-head-close.inc' %]
</head>

<body id="tools_automatic_item_modification_by_age" 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 == 'edit_form' ) %]
                [% WRAPPER breadcrumb_item %]
                    <a href="/cgi-bin/koha/tools/automatic_item_modification_by_age.pl">Automatic item modifications by age</a>
                [% END %]
                [% WRAPPER breadcrumb_item bc_active= 1 %]
                    <span>Rules</span>
                [% END %]
            [% ELSE %]
                [% WRAPPER breadcrumb_item bc_active= 1 %]
                    <span>Automatic item modifications by age</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' %]

        [% IF ( op == 'edit_form' ) %]
          <form method="post" id="rules_form" action="/cgi-bin/koha/tools/automatic_item_modification_by_age.pl">
            <h1>Rules for automatic item modifications by age</h1>
            <div id="toolbar" class="btn-toolbar">
                <div class="btn-group">
                    <button class="btn btn-default add_rule"><i class="fa fa-plus"></i> Add rule</button>
                </div>
                <div class="btn-group">
                    <button type="submit" id="save_rules" class="btn btn-default"><i class="fa fa-save"></i> Save</button>
                </div>
                <div class="btn-group">
                    <a class="btn btn-default" href="/cgi-bin/koha/tools/automatic_item_modification_by_age.pl"><i class="fa fa-times"></i> Cancel</a>
                </div>
            </div>
        [% ELSE %]
            <h1>Automatic item modifications by age</h1>
            [% IF ( rules ) %]
                <div id="toolbar" class="btn-toolbar">
                    <a class="btn btn-default" id="newentry" href="/cgi-bin/koha/tools/automatic_item_modification_by_age.pl?op=edit_form"><i class="fa-solid fa-pencil" aria-hidden="true"></i> Edit rules</a>
                </div>
            [% ELSE %]
                <div id="toolbar" class="btn-toolbar">
                    <a class="btn btn-default" id="newentry" href="/cgi-bin/koha/tools/automatic_item_modification_by_age.pl?op=edit_form"><i class="fa fa-plus"></i> Add rules</a>
                </div>
            [% END %]
        [% END %]

        [% FOR message IN messages %]
          [% IF message.type == "error" %]
            <div class="dialog alert">
          [% END %]
          [% IF message.code == "unable_to_load_configuration" %]
            An error occurs: Unable to load the configuration.
          [% END %]
          </div>
        [% END %]

        [% IF op == 'edit_form' %]
            <div id="edit_rules">
                <div id="rules">
                [% FOR rule IN rules %]
                  [% SET id = loop.count %]
                  <fieldset class="rule">
                    <legend>Rule <span class="rulecount">[% loop.count | html %]</span> <a href="#" class="remove_rule"><i class="fa fa-trash-can"></i> Remove this rule</a></legend>
                    <input type="hidden" name="unique_id" value="[% loop.count | html %]" /> <!-- FIXME on update, the unique_id should be filled -->
                    <div class="age">
                      <h5>Age in days</h5>
                      <input class="age" type="text" inputmode="numeric" pattern="[0-9]*"  value="[% rule.age | html %]" name="age_[% id | html %]" />
                      <h5>Age field</h5>
                          <div class="blocks">
                              <select name="agefield_[% id | html %]">
                                  <option value="">Choose an age field</option>
                                  [% FOR field IN agefields.sort %]
                                    [% IF rule.agefield == field %]
                                     <option value="[% field | html %]" selected="selected">[% field | html %]</option>
                                    [% ELSE %]
                                     <option value="[% field | html %]">[% field | html %]</option>
                                    [% END %]
                                  [% END %]
                              </select>
                              <span class="hint">If not set then items.dateaccessioned will be used</span>
                          </div>
                    </div>
                    <div class="blocks">
                      <h5>Conditions</h5>
                      [% FOR condition IN rule.conditions %]
                        <div class="block">
                          <select name="condition_field_[% id | html %]">
                            <option value="">Choose a field name</option>
                            [% FOR field IN condition_fields.sort %]
                              [% IF condition.field == field %]
                                <option value="[% field | html %]" selected="selected">[% field | html %]</option>
                              [% ELSE %]
                                <option value="[% field | html %]">[% field | html %]</option>
                              [% END %]
                            [% END %]
                          </select>
                          =
                          <input type="text" value="[% condition.value | html %]" name="condition_value_[% id | html %]" />
                          <a class="add_block" href="#"><i class="fa fa-plus"></i> Add a condition</a>
                          <a class="remove_block" href="#"><i class="fa fa-trash-can"></i> Remove condition</a>
                        </div>
                      [% END %]
                    </div>
                    <div class="blocks">
                      <h5>Substitutions</h5>
                      [% FOR substitution IN rule.substitutions %]
                        <div class="block">
                          <select class="required" required="required" name="substitution_field_[% id | html %]">
                            <option value="">Choose a field name</option>
                            [% FOR field IN substitution_fields.sort %]
                              [% IF substitution.field == field %]
                                <option value="[% field | html %]" selected="selected">[% field | html %]</option>
                              [% ELSE %]
                                <option value="[% field | html %]">[% field | html %]</option>
                              [% END %]
                            [% END %]
                          </select>
                          =
                          <input type="text" value="[% substitution.value | html %]" name="substitution_value_[% id | html %]" />
                          <a class="add_block" href="#"><i class="fa fa-plus"></i> Add a substitution</a>
                          <a class="remove_block" href="#"><i class="fa fa-trash-can"></i> Remove substitution</a>
                          <span class="required">A field name is required</span>
                        </div>
                      [% END %]
                    </div>
                  </fieldset>
                [% END %]
                </div>
                <input type="hidden" name="op" value="update" />
            </div>
          </form>

        <div id="norules" class="dialog message">
          There are no rules defined.
        </div>

          <fieldset id="new_rule">
            <legend>Rule <span class="rulecount"></span> <a href="#" class="remove_rule"><i class="fa fa-trash-can"></i> Remove this rule</a></legend>
            <input type="hidden" name="unique_id" />
            <div class="age">
              <h5>Age in days</h5>
              <input class="age" type="text" inputmode="numeric" pattern="[0-9]*" value="" name="age" />
              <h5>Age field</h5>
              <div class="block">
                  <select name="agefield">
                      <option value="">Choose an age field</option>
                      [% FOR field IN agefields.sort %]
                           <option value="[% field | html %]">[% field | html %]</option>
                      [% END %]
                  </select>
                  <span class="hint">If not set then items.dateaccessioned will be used</span>
              </div>
            </div>
            <div class="blocks">
              <h5>Conditions</h5>
              <div class="block">
                <select name="condition_field">
                  <option value="">Choose a field name</option>
                  [% FOR field IN condition_fields.sort %]
                    <option value="[% field | html %]">[% field | html %]</option>
                  [% END %]
                </select>
                =
                <input type="text" value="" name="condition_value" />
                <a class="add_block" href="#"><i class="fa fa-plus"></i> Add a condition</a>
                <a class="remove_block" href="#"><i class="fa fa-trash-can"></i> Remove condition</a>
              </div>
            </div>
            <div class="blocks">
              <h5>Substitutions</h5>
              <div class="block">
                <select required="required" class="required" name="substitution_field">
                  <option value="">Choose a field name</option>
                  [% FOR field IN substitution_fields.sort %]
                    <option value="[% field | html %]">[% field | html %]</option>
                  [% END %]
                </select>
                =
                <input type="text" value="" name="substitution_value" />
                <a class="add_block" href="#"><i class="fa fa-plus"></i> Add a substitution</a>
                <a class="remove_block" href="#"><i class="fa fa-trash-can"></i> Remove substitution</a>
                <span class="required">A field name is required</span>
              </div>
            </div>
          </fieldset>
        [% ELSIF rules %]
            <div>
                <h4>List of rules</h4>

                <div class="page-section">
                    <table id="rulest">
                        <thead>
                          <tr>
                               <th>Age</th>
                                <th>Age field</th>
                                <th>Conditions</th>
                                <th>Substitutions</th>
                            </tr>
                        </thead>
                        <tbody>
                            [% FOR rule IN rules %]
                                <tr>
                                    <td>
                                        [% IF rule.age.defined and rule.age.length > 0 %]
                                            <span>[% rule.age | html %] days</span>
                                        [% ELSE %]
                                            <span>There is no age for this rule.</span>
                                        [% END %]
                                    </td>
                                    <td>
                                        [% IF rule.agefield %]
                                            [% rule.agefield | html %]
                                        [% ELSE %]
                                            <!-- In case old rules do not contain an agefield value display that items.dateaccessioned will be used -->
                                            items.dateaccessioned
                                        [% END %]
                                    </td>
                                    <td>
                                        [% FOR condition IN rule.conditions %]
                                            [% IF condition.field %]
                                                <div class="block">
                                                [% condition.field | html %] = [% condition.value | html %]
                                                </div>
                                            [% ELSE %]
                                                There is no condition for this rule.
                                            [% END %]
                                        [% END %]
                                    </td>
                                    <td>
                                        [% FOR substitution IN rule.substitutions %]
                                            <div class="block">
                                                [% substitution.field | html %] = [% substitution.value | html %]
                                            </div>
                                        [% END %]
                                    </td>
                                </tr>
                            [% END %]
                        </tbody>
                    </table>
                </div> <!-- /.page-section -->
            </div>
        [% ELSE %]
            <div class="dialog message">
                There are no rules defined.
            </div>
        [% 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 %]
    [% Asset.js("lib/hc-sticky.js") | $raw %]
    [% Asset.js("js/automatic_item_modification_by_age.js") | $raw %]
    [% IF op == 'edit_form' %]
        <script>
            var Sticky;
            $(document).ready(function() {
                  [% IF ( op == 'edit_form' ) %]
                Sticky = $("#toolbar");
                Sticky.hcSticky({
                    stickTo: "main",
                    stickyClass: "floating"
                });
                  [% END %]
                  [% IF rules.size > 0 %]
                      $("#norules").hide();
                  [% ELSE %]
                      $("#rules").show();
                  [% END %]
            });
        </script>
    [% END %]
[% END %]

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