[% PROCESS _header.tt js="plugins/${plugin_name}/reports-${fileversion}.js" %]
[% PROCESS _message.tt %]
[% PROCESS _blocks.tt %]
[% USE date %]

[% PROCESS _infobox.tt %]

[% IF !puppeteer %]
<div class="card red alert top-center w-[550px]">
  <div class="head">
    <h3>Missing Converter!</h3>
  </div>
  <div class="body">
    Puppeteer is missing, no pdfs can be generated.<br>
    <i class="uil uil-external-link-alt"></i> <a class="link" href="http://www.thruk.org/faq.html#_puppeteer" target="_blank">please read the FAQ.</a>
  </div>
</div>
[% END %]

<div class="card w-full mt-5 min-w-[800px]">
  <div class="head p-0 justify-between border-0">
    <div class="mx-2">
      [% PROCESS _table_search_field.tt ids=["reportsTable"] %]
    </div>
    <div class="grow"></div>
    [% IF !filtered %]
    <span class="tabs min-w-[140px] w-52 text-center js-tabs" id="view1" onclick="reports_view('my')    ; return false;"><a class="block" href="#">My Reports</a></span>
    <span class="tabs min-w-[140px] w-52 text-center js-tabs" id="view2" onclick="reports_view('public'); return false;"><a class="block" href="#">Shared Reports</a></span>
    <span class="tabs min-w-[140px] w-52 text-center js-tabs" id="view3" onclick="reports_view('all')   ; return false;"><a class="block" href="#">All  Reports</a></span>
    [% ELSE %]
      <a href="reports2.cgi" class="button">Show all reports</a>
    [% END %]
    <div class="grow"></div>
    <a href="reports2.cgi?action=edit&amp;report=new" class="button green w-40 mx-2">create new report</a>
  </div>
  <div class="mainTable [% IF main_table_full %]fullsize[% END %]"  style="display: none;">
    <table class="mainTable js-striped" id="reportsTable">
      <thead>
        <tr>
          <th class="w-56">Name</th>
          <th class="min-w-[250px]">Description</th>
          <th class="text-center w-20">Shared</th>
          <th class="text-center w-28 js-usercol">Owner</th>
          <th class="text-center w-28">Mail</th>
          <th class="text-center w-48">Schedule</th>
          <th class="text-center w-28">Last Time Run</th>
          <th class="text-center w-48">Duration</th>
          <th class="w-6"></th>
          <th class="w-6"></th>
          <th class="w-[60px]"></th>
          <th class="w-6"></th>
        </tr>
      </thead>
      <tbody>
        [% FOREACH r = reports %]
        [% name = r.name; IF (r.defined("var") && r.var.defined("name")); name = r.var.name; END %]
        [% desc = r.desc; IF (r.defined("var") && r.var.defined("desc")); desc = r.var.desc; END %]
        <tr class="report[% r.nr +%] [% IF r.user == remote_user %] my[% END %][% IF r.is_public %] public[% ELSE %] other[% END %]">
          <td class="truncate w-1/4 max-w-xs" title="[% name | html %]">[% name | html %]</td>
          <td class="truncate w-1/4 max-w-xs">[% IF desc.length > 50 %]<span title="[% desc | html %]">[% desc.substr(0,50) | html %]...</span>[% ELSE %][% desc | html %][% END %]</td>
          <td class="text-center">[% IF r.is_public %]yes[% ELSE %]no[% END %]</td>
          <td class="text-center js-usercol">[% r.user | html %]</td>
          <td class="text-center">[% IF ( r.to || r.cc ) && r.send_types.size > 0 %]yes[% ELSE %]no[% END %]</td>
          <td class="text-center">[% IF r.send_types.size == 0 %]never[% ELSE %][% FOREACH st = r.send_types %][% format_cronentry(c, st) %]<br>[% END %][% END %]</td>
          <td class="text-center">[% IF r.var.start_time %][% date_format(c, r.var.start_time) %][% ELSE %]never[% END %]</td>
          <td class="text-center">
            [% IF r.var.exists('is_waiting') && r.var.is_waiting %]
              queued
            [% ELSIF r.failed %]
              <span class="textALERT">failed</span>
            [% ELSE %]
              [% IF r.var.start_time %]
                [% IF r.var.is_running > 0 %][% end_time = date.now %][% ELSE %][% end_time = r.var.end_time %][% END %]
                [% job_time = end_time - r.var.start_time %]
                [% IF r.var.exists('job_data') %]
                      [% job_message = ""; IF r.var.job_data.defined('message'); job_message = r.var.job_data.message; END %]
                      [% PROCESS _progressbar.tt
                                  pb_options = {
                                      percent             => r.var.job_data.percent,
                                      time                => job_time,
                                      job_id              => r.var.job,
                                      id                  => "progressbar" _ r.nr,
                                      lineheight          =>  19,
                                      width               => 160,
                                      height              =>  20,
                                      percent_per_second  =>  0,
                                      show_message        =>  1,
                                      message             =>  job_message,
                                      hide_cancel_btn     =>  1,
                                  }
                      %]
                [% ELSE %]
                  [% IF c.check_user_roles('admin') %]
                    <span class="clickable" onclick="overcard({ 'bodyEl': 'report_profile[% loop.index %]', 'caption': 'report profile' }); load_overcard_content('report_profile[% loop.index %]', 'reports2.cgi?report=[% r.nr %]&amp;action=profile&view=html');">
                    [% duration(job_time, 3) %]
                    </span>
                    <div id='report_profile[% loop.index %]' class='p-2 min-w-[800px] flex hidden'></div>
                  [% ELSE %]
                      [% duration(job_time, 3) %]
                  [% END %]
                [% END %]
              [% END %]
            [% END %]
          </td>
          <td class="text-center">
            <form action="reports2.cgi" method="POST">
              <div class="flexrow flex-nowrap gap-1">
                <input type="hidden" name="report" value="[% r.nr %]">
                <input type="hidden" name="CSRFtoken" value="[% get_user_token(c) %]">
                [% IF r.var.is_running != 0 || (r.var.exists('is_waiting') && r.var.is_waiting) %]
                  <a href="#" onclick="return send_form_in_background_and_reload(this, {action: 'cancel'})"><i class="fa-solid fa-circle-xmark text-sm hover-red-text" title='Cancel Report'></i></a>
                [% ELSE %]
                  <a href="#" onclick="return send_form_in_background_and_reload(this, {action: 'update'})"><i class="fa-solid fa-arrows-rotate text-sm" title='Regenerate Report'></i></a>
                  [% IF debug %]
                    <a href="#" onclick="return send_form_in_background_and_reload(this, {action: 'update', debug: 1})"><i class="fa-solid fa-wand-magic-sparkles text-sm" title='Regenerate Report in Debug Mode'></i></a>
                    [% IF r.var.exists("debug_file") %]
                      <a href="reports2.cgi?report=[% r.nr %]&amp;action=download_debug"><i class="fa-solid fa-file-circle-plus text-sm" title='Download Debug Report'></i></a>
                    [% END %]
                  [% END %]
                [% END %]
              </div>
            </form>
          </td>
          <td class="text-center">
            [% IF ! r.readonly %]
              <a href="reports2.cgi?report=[% r.nr %]&amp;action=edit"><i class="fa-solid fa-pencil text-sm" title='Change Report'></i></a>
            [% END %]
          </td>
          <td class="text-center">
            <div class="flexrow flex-nowrap gap-1">
              [% IF r.var.file_exists && r.var.is_running == 0 %]
                [% PROCESS _reports_icon.tt r=r %]
                [% IF r.var.ctype == 'html2pdf' %]
                  <a href="reports2.cgi?report=[% r.nr %]&amp;refreshreport=0&amp;html=1" target="_blank"><i class="fa-solid fa-file-circle-check text-sm" title='View Report Preview'></i></a>
                [% ELSE %]
                  <div></div>
                [% END %]
                <a href="reports2.cgi?report=[% r.nr %]&amp;refreshreport=0"><i class="[% icon %] text-sm" title="View Report"></i></a>
                <a href="reports2.cgi?report=[% r.nr %]&amp;action=email"><i class="fa-solid fa-envelope text-sm" title="Send Report by Mail"></i></a>
                [% IF r.var.exists("json_file") %]
                <a href="reports2.cgi?report=[% r.nr %]&amp;action=download_json"><i class="fa-solid fa-file-code text-sm" title='Download JSON raw data'></i></a>
                [% END %]
              [% END %]
            </div>
          </td>
          <td class="text-center">
            [% IF ! r.readonly %]
              <form action="reports2.cgi" method="POST">
                <input type="hidden" name="report" value="[% r.nr %]">
                <input type="hidden" name="CSRFtoken" value="[% get_user_token(c) %]">
                <a href="#" onclick="if(confirm('really remove?')) { return send_form_in_background_and_reload(this, {action: 'remove'}) }; return false;"><i class="fa-solid fa-trash text-sm" title='Delete Report'></i></a>
              </form>
            [% END %]
          </td>
        </tr>
        [% IF r.failed %]
          <tr class="report[% r.nr +%] js-skip-count [% IF r.user == remote_user %] my[% ELSIF r.is_public %] public[% ELSE %] other[% END %]">
            <td></td>
            <td colspan=9 class="textALERT">
              <span [% IF r.exists('long_error') && r.long_error != "" %]class="clickable" onClick="toggleElement('long_error_[% r.nr %]', 'long_error_icon_[% r.nr %]'); return false;"[% END %]>
                [% IF r.exists('long_error') && r.long_error != "" %]
                  <i class="uil uil-arrow-from-top" id='long_error_icon_[% r.nr %]' title="show long error output"></i>
                [% END %]
                [% escape_html(r.error) | nl2br %]
              </span>
              [% IF r.exists('long_error') && r.long_error != "" %]
                <div id='long_error_[% r.nr %]' class="truncate max-w-[70%]" style="display:none;">
                [% escape_html(r.long_error) | nl2br %]
              </div>
              [% END %]
            </td>
          </tr>
        [% END %]
        [% IF r.exists("var") && r.var.exists("opt_errors") %]
          <tr class="report[% r.nr +%] js-skip-count [% IF r.user == remote_user %] my[% ELSIF r.is_public %] public[% ELSE %] other[% END %]">
            <td></td>
            <td colspan=9 class="textALERT">
              Error(s) in report options:
              <ul>
              [% FOREACH e = r.var.opt_errors %]
                <li>[% e | html %]
              [% END %]
              </ul>
            </td>
          </tr>
        [% END %]
        [% END %]
        [% IF reports.size == 0 %]
          <tr class="js-skip-count">
            <td class="text-center textHINT" colspan="10">no reports defined yet</td>
          </tr>
        [% END %]
      </tbody>
    </table>
  </div>
  <div class="mainTableFooter">
    <div class="flex-1 self-center">
      [% PROCESS _pager_total_items.tt pager = { total_items => reports.size } %]
    </div>
  </div>
</div>

<script>
<!--
  jQuery(document).ready(function() {
    var typ = get_hash(1);
    if(typ == undefined) { typ = 'my' }
    if(typ == "")        { typ = 'my' }
    jQuery("DIV.mainTable").css("display", "");
    reports_view(typ);
    [% IF highlight %]
    jQuery('TR.report[% escape_quotes(highlight) +%] TD').effect('highlight', {}, 2000, function() {
      reports_view(typ);
    });
    if(typ == 'my') {
      // filter again, otherwise the owner cell would be displayed after hilighting
      reports_view(typ);
    }
    [%+ END +%]
    table_search("table_search_input", ["reportsTable"], 1);
  });
-->
</script>

[% PROCESS _footer.tt %]
