<tal:report
  i18n:domain="senaite.impress"
  define="collection python:view.collection;
          accredited_symbol string:★;
          outofrange_symbol string:⚠;">

  <tal:css define="laboratory view/laboratory;">
    <style type="text/css">
     html, body { font-size: 1em; }
     h1 { font-size: 160%; }
     h2 { font-size: 120%; }
     .colon-after:after { content: ":"; }
     table.noborder td { border: none; }
     table.nopadding td { padding: 0; }
     table td.label { padding-right: 0.3rem; }
     table td.label { white-space: nowrap; }
     @page {
       @bottom-left {
         font-size: 8pt;
         content: '<span tal:omit-tag="" tal:content="laboratory/Name"/>';
       }
       @bottom-center {
         font-size: 8pt;
         <tal:single tal:condition="python:len(collection) == 1">
           content: '<tal:content replace="python:collection[0].getId()"/>';
         </tal:single>
         <tal:multi tal:condition="python:len(collection) > 1">
           content: '<tal:content replace="python:' • '.join(map(lambda m: m.getId(), collection))"/>';
         </tal:multi>
       }
       @bottom-right {
         font-size: 8pt;
         content: "<tal:t i18n:translate=''>Page</tal:t> " counter(page) " <tal:t i18n:translate=''>of</tal:t> " counter(pages);
       }
     }
    </style>
  </tal:css>

  <!-- HEADER -->
  <tal:render condition="python:True">
    <div class="row section-header">
      <div class="col-sm-12">
        <div class="text-right">
          <a tal:attributes="href view/portal_url">
            <img style="max-height: 100px"
                 tal:attributes="src python:view.get_resource_url('logo_print.png')"/>
          </a>
        </div>
      </div>
    </div>
  </tal:render>

  <!-- INFO -->
  <tal:render condition="python:True">
    <div class="row section-info">
      <div class="col-sm-12 py-4 small">
        <!-- Client Info -->
        <table class="col">
          <colgroup>
            <col style="width:50%"/>
            <col style="width:50%"/>
          </colgroup>
          <tr>
            <td class="align-top">
              <!-- Left Table -->
              <table class="table table-sm mr-1">
                <!-- Client Name(s) -->
                <tr>
                  <td class="label" i18n:translate="">Client</td>
                  <td class="field">
                    <tal:by_client repeat="client python:view.group_items_by('Client', collection)">
                      <div tal:content="client/Name"/>
                    </tal:by_client>
                  </td>
                </tr>
                <!-- Contact Name(s) -->
                <tr>
                  <td class="label" i18n:translate="">Contact</td>
                  <td class="field">
                    <tal:by_contact repeat="contact python:view.group_items_by('Contact', collection)">
                      <div tal:content="contact/Fullname"/>
                    </tal:by_contact>
                  </td>
                </tr>
                <!-- Client Reference(s) -->
                <tr>
                  <td class="label" i18n:translate="">Client Reference</td>
                  <td class="field">
                    <tal:by_client_ref repeat="client_ref python:view.group_items_by('ClientReference', collection)">
                      <div tal:content="client_ref"/>
                    </tal:by_client_ref>
                  </td>
                </tr>
                <!-- Client Order(s) -->
                <tr>
                  <td class="label" i18n:translate="">Client Order</td>
                  <td class="field">
                    <tal:by_client_order_num repeat="client_order_num python:view.group_items_by('ClientOrderNumber', collection)">
                      <div tal:content="client_order_num"/>
                    </tal:by_client_order_num>
                  </td>
                </tr>
                <!-- Client Sample ID(s) -->
                <tr>
                  <td class="label" i18n:translate="">Client Sample ID</td>
                  <td class="field">
                    <tal:by_client_sid repeat="client_sid python:view.group_items_by('ClientSampleID', collection)">
                      <div tal:content="client_sid"/>
                    </tal:by_client_sid>
                  </td>
                </tr>
              </table>
            </td>
            <td class="align-top">
              <!-- Right Table -->
              <table class="table table-sm ml-1"
                     tal:define="samples python:map(lambda m: m.Sample, collection)">
                <!-- Sample Type -->
                <tr>
                  <td class="label" i18n:translate="">Sample Type</td>
                  <td class="field">
                    <tal:sampletype define="sampletypes python:map(lambda m: m.SampleTypeTitle, samples)"
                                    repeat="sampletype python:set(sampletypes)">
                      <div tal:content="sampletype"/>
                    </tal:sampletype>
                  </td>
                </tr>
                <!-- Sample Point -->
                <tr>
                  <td class="label" i18n:translate="">Sample Point</td>
                  <td class="field">
                    <tal:samplepoint define="samplepoints python:map(lambda m: m.SamplePointTitle, samples)"
                                     repeat="samplepoint python:set(samplepoints)">
                      <div tal:content="samplepoint"/>
                    </tal:samplepoint>
                  </td>
                </tr>
                <!-- Sample Remarks -->
                <tr>
                  <td class="label" i18n:translate="">Sample Remarks</td>
                  <td class="field">
                    <tal:model repeat="model collection">
                      <div tal:content="model/SamplingDeviationTitle|nothing"/>
                    </tal:model>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
        </table>
      </div>
    </div>
  </tal:render>

  <!-- ALERTS -->
  <tal:render condition="python:True">
    <tal:model repeat="model collection">
      <div class="alert alert-danger" tal:condition="model/is_invalid">
        <h4 class="alert-heading"><span tal:replace="model/getId"/></h4>
        <div i18n:translate="">This Analysis Request has been invalidated due to erroneously published results</div>
        <tal:invalidreport tal:define="child model/ChildAnalysisRequest"
                           tal:condition="child">
          <span i18n:translate="">This Analysis request has been replaced by</span>
          <a tal:attributes="href child/absolute_url"
             tal:content="child/getId"></a>
        </tal:invalidreport>
      </div>

      <div class="alert alert-info" tal:condition="model/is_provisional">
        <h4 class="alert-heading"><span tal:replace="model/getId"/></h4>
        <div i18n:translate="">Provisional report</div>
      </div>
    </tal:model>
  </tal:render>

  <!-- RESULTS -->
  <tal:render condition="python:True"
              define="analyses_by_poc python:view.get_analyses_by_poc(collection);
                      categories_by_poc python:view.get_categories_by_poc(collection)">
    <div class="row section-results">
      <div class="col col-sm-12">
        <h1 i18n:translate="">Results</h1>

        <!-- Point of Captures -->
        <tal:poc tal:repeat="poc analyses_by_poc">
          <h4 tal:content="python:view.points_of_capture.get(poc)"></h4>

          <!-- Results table per PoC -->
          <table class="table table-sm table-condensed small">
            <thead>
              <tr>
                <th></th>
                <tal:ar repeat="model collection">
                  <th colspan="2" class="font-weight-normal text-right">
                    <div class="text-primary"
                         tal:content="model/getId"/>
                  </th>
                  <th></th>
                </tal:ar>
              </tr>
            </thead>
            <tbody>
              <!-- Categories in PoC -->
              <tal:categories_in_poc tal:repeat="category python:view.sort_items(categories_by_poc.get(poc))">
                <tr>
                  <td class="font-weight-bold table-warning"
                      tal:attributes="colspan python:len(collection) * 3 + 1">
                    <span tal:content="category/Title"/>
                  </td>
                </tr>
                <tr tal:define="analyses python:view.get_analyses_by(collection, poc=poc, category=category);
                                analyses_by_title python:set(map(lambda a: a.Title(), analyses))"
                    tal:repeat="analysis_title python:analyses_by_title">
                  <td class="text-secondary">
                    <span tal:content="analysis_title"/>
                  </td>
                  <tal:results repeat="model collection">
                    <tal:analyses tal:define="analyses python:view.get_analyses_by(model, title=analysis_title);">
                      <tal:analysis tal:repeat="analysis analyses">
                        <td class="text-right">
                          <span class="font-weight-light"
                                tal:content="structure python:model.get_formatted_result(analysis)"/>
                        </td>
                        <td class="text-left">
                          <span class="font-italic font-weight-light text-secondary text-nowrap"
                                tal:content="structure python:model.get_formatted_unit(analysis)"/>
                        </td>
                        <td class="text-left">
                          <span tal:condition="python:model.is_out_of_range(analysis)"
                                class="font-weight-light">
                            <span class="outofrange text-danger"
                                  style="font-family:Lucida Console, Courier, monospace;"
                                  tal:content="outofrange_symbol">
                            </span>
                          </span>
                        </td>
                      </tal:analysis>
                      <tal:analysis condition="not:analyses">
                        <td></td>
                        <td></td>
                        <td></td>
                      </tal:analysis>
                    </tal:analyses>
                  </tal:results>
                </tr>
              </tal:categories_in_poc>
            </tbody>
          </table>
        </tal:poc>
      </div>
    </div>
  </tal:render>

  <!--  RESULTS INTERPRETATIONS -->
  <tal:render condition="python:True">
    <tal:model repeat="model collection">
      <div class="row section-resultsinterpretation"
           tal:define="ris python:model.get_resultsinterpretation()"
           tal:condition="ris">
        <div class="col col-sm-12">
          <h1 i18n:translate>Results Interpretation for <span tal:replace="model/getId"/></h1>
          <div tal:condition="ris">
            <tal:ri repeat="ri ris">
              <h4 tal:content="ri/title|nothing">Department</h4>
              <div class="text-info" tal:content="structure ri/richtext|nothing"></div>
            </tal:ri>
          </div>
        </div>
      </div>
    </tal:model>
  </tal:render>

  <!-- QC RESULTS -->
  <tal:render condition="python:True">
    <tal:model repeat="model collection">
      <tal:qc define="qcanalyses python:model.getQCAnalyses(['verified', 'published']);">
        <div class="row section-results" tal:condition="qcanalyses">
          <div class="col col-sm-12">
            <h2 i18n:translate>QC Results for <span tal:replace="model/getId"/></h2>
            <div>
              <h1 i18n:translate="">QC Results</h1>
            </div>
          </div>
        </div>
      </tal:qc>
    </tal:model>
  </tal:render>

  <!--  REMARKS -->
  <tal:render condition="python:True">
    <tal:model repeat="model collection">
      <div class="row section-remarks" tal:condition="model/Remarks">
        <div class="col-sm-12">
          <h2 i18n:translate>Remarks for <span tal:replace="model/getId"/></h2>
          <div class="text-info" tal:content="structure model/Remarks"></div>
        </div>
      </div>
    </tal:model>
  </tal:render>

  <!-- ATTACHMENTS -->
  <tal:render condition="python:True"
              define="attachments_per_row python:2">
    <tal:model repeat="model collection">
      <tal:attachment tal:define="ar_attachments python:model.get_sorted_ar_attachments('r');
                                  an_attachments python:model.get_sorted_an_attachments('r')"
                      tal:condition="python: ar_attachments or an_attachments">

        <h4 i18n:translate="">
          Attachments for <span tal:replace="model/getId"/>
        </h4>

        <!-- AR ATTACHMENTS -->
        <div class="row section-an-attachments" tal:condition="ar_attachments">
          <div class="col-sm-12">
            <table class="table">
              <colgroup>
                <col tal:attributes="style python:'width:{}%'.format(100/attachments_per_row)">
              </colgroup>
              <tr tal:repeat="chunk python:view.group_into_chunks(ar_attachments, attachments_per_row)">
                <td class="align-bottom"
                    style="border:none;padding-left:0;"
                    tal:repeat="attachment chunk">
                  <img class="img-fluid"
                       style="width:100%;"
                       tal:attributes="src string:${attachment/absolute_url}/AttachmentFile"/>
                  <div class="text-secondary">
                    <span tal:content="attachment/AttachmentKeys"/>
                  </div>
                </td>
              </tr>
            </table>
          </div>
        </div>

        <!-- AN ATTACHMENTS -->
        <div class="row section-an-attachments" tal:condition="an_attachments">
          <div class="col-sm-12">
            <table class="table">
              <colgroup>
                <col tal:attributes="style python:'width:{}%'.format(100/attachments_per_row)">
              </colgroup>
              <tr tal:repeat="chunk python:view.group_into_chunks(an_attachments, attachments_per_row)">
                <td class="align-bottom"
                    style="border:none;padding-left:0;"
                    tal:repeat="attachment chunk">
                  <img class="img-fluid"
                       style="width:100%;"
                       tal:attributes="src python:attachment[1].absolute_url() + '/AttachmentFile'"/>
                  <div class="text-secondary">
                    <span tal:content="python:attachment[1].title"/>:
                    <span tal:content="python:attachment[1].AttachmentKeys"/>
                  </div>
                </td>
              </tr>
            </table>
          </div>
        </div>
      </tal:attachment>
    </tal:model>
  </tal:render>

  <!--  SIGNATURES -->
  <tal:render condition="python:True">
    <tal:responsibles define="managers python:set(reduce(lambda a1, a2: a1+a2, map(lambda m: m.managers, collection)))">
      <div class="row section-signatures">
        <div class="col-sm-12">
          <h1 i18n:translate="">Responsibles</h1>
          <table class="table table-sm">
            <tr>
              <tal:manager repeat="manager managers">
                <td style="border:none">
                  <div>
                    <img tal:condition="manager/Signature"
                         tal:attributes="src string:${manager/absolute_url}/Signature" style="height:75px"/>
                  </div>
                  <div class="font-weight-bold">
                    <span tal:content="manager/Salutation"></span>
                    <span tal:condition="manager/Salutation">&nbsp;</span>
                    <span tal:content="manager/Fullname"></span>
                  </div>
                  <div>
                    <span tal:content="manager/JobTitle"></span>
                  </div>
                  <div>
                    <span tal:content="manager/DefaultDepartment/title|nothing"></span>
                  </div>
                </td>
              </tal:manager>
              <tal:reporter define="reporter python:view.current_user" condition="reporter">
                <td style="border:none">
                  <div class="label" i18n:translate="">Published by</div>
                  <div>
                    <span tal:content="reporter/fullname|reporter/username"></span>
                    <tal:email tal:condition="reporter/email|nothing"
                               tal:define="email reporter/email|nothing">
                      (<a tal:content="email"
                          tal:attributes="href string:mailto:${email}"></a>)
                    </tal:email>
                  </div>
                </td>
              </tal:reporter>
            </tr>
          </table>
        </div>
      </div>
    </tal:responsibles>
  </tal:render>

  <!-- DISCREETER -->
  <tal:render condition="python:True"
              define="laboratory python:view.laboratory;">
    <div class="row section-discreeter">
      <div class="col-sm-12 text-muted font-weight-light small">
        <div class="discreeter-outofrange">
          <span class="outofrange text-danger"
                style="font-family:Lucida Console, Courier, monospace;"
                tal:content="outofrange_symbol">
          </span>
          <span i18n:translate="">Result out of client specified range.</span>
        </div>
        <div class="discreeter-not-invoiced"
             tal:condition="python:all(map(lambda m: m.InvoiceExclude, collection))"
             i18n:translate="">
          Not invoiced
        </div>
        <div class="discreeter-methods"
             tal:condition="laboratory/LaboratoryAccredited">
          <span class="accredited-symbol text-success"
                style="font-family:Lucida Console, Courier, monospace;"
                tal:content="accredited_symbol">
          </span>
          <span class="" i18n:translate="">
            Methods included in the
            <tal:block replace="laboratory/AccreditationBody" i18n:name="accreditation_body"/>
            schedule of Accreditation for this Laboratory. Analysis remarks are not
            accredited
          </span>
        </div>
        <div class="discreeter-disclaimer"
             i18n:translate="">
          Analysis results relate only to the samples tested.
        </div>
        <div class="discreeter-copyright"
             i18n:translate="">
          This document shall not be reproduced except in full, without the written approval of
          <tal:block replace="laboratory/title" i18n:name="name_lab"/>
        </div>
        <div tal:define="confidence_level laboratory/Confidence"
             tal:condition="confidence_level" i18n:translate="">
          Test results are at a <tal:block replace="confidence_level" i18n:name="lab_confidence"/>% confidence level
        </div>
      </div>
    </div>
  </tal:render>

</tal:report>
