	[% META title="Report" %]
<!-- BEGIN [% template.name %] -->
  [%
    # handles initial report, (optional) authorisation and diagnosis revision:

    # 'authorisation_request' tt_param blocks loading of diagnosis, revision and
    # morphology/comment fields, and previous diagnoses list, and
    # disables all text, radio & checkbox fields - only diagnosis_id required

    # 'pre-authorisation-check' query param sets 'revision' field to optional
  %]

    [% # PROCESS dumper.tt dump = # data.as_tree
      # c.session.param('UserProfile');
      # c.stash.errs;
      # history.reported;
      # data.request_report.diagnosis;
      # previous_cases;
      # result_summaries;
      # unresulted_tests;
      # raw_lab_test_data;
      # lab_section_notes;
      # previous_diagnoses_data;
      # request_options;
      # unrecognised_clinical_details;
      # unrecognised_comment_words;
      # unrecognised_biopsy_site_words;
      # highlighted_comment;
      # diagnosis_change_options;
      # unrecognised;
      # specimen_map;
      # outreach.demographics;
      # special_formatting;
      # url_with_path
      # outstanding_tests;
    %]

  [% # define some data structures:
        # c.debug('##############'); # to reveal any db calls from templates
    patient = data.patient_case.patient; # not as_tree - preserve datetime objects
    request_data = data.as_tree; # preserve 'data' in case it's overwritten in PROCESS's
        # PROCESS dumper.tt dump = request_data;
    screen = request_data.request_initial_screen.screen.description;
    is_outreach = specimen_map.item(data.id).sample_code.join(' ').match('CMP')
        AND screen.lower.match('outreach') AND c.cfg('settings').have_outreach;
    is_genomics = c.cfg('settings').item('_centre').match('^genomics$');
    report_data = draft_report_data || request_data.request_report; # save multiple lookups if undefined
        # PROCESS dumper.tt dump = report_data;
    this_user = c.session.param('UserProfile');
  %]

  [% PROCESS site/popup.tt height=280, width=800, top=280 %]

  [% # PROCESS site/js/jquery.tt # already loaded in html_wrapper %]
  <script type="text/javascript" src="/js/jquery.textarea-expander.js"></script>
  <script type="text/javascript" src="/speller/spellChecker.js"></script>
  <script type="text/javascript" src="/js/changeText.js"></script>
  [% INCLUDE site/js/openSpellChecker.tt form_name = 'reportForm' %]

  <script type="text/javascript">
    function load_add_word_result (uri, elem) {
      $(elem).load(uri);
    }
  </script>

  [%
    IF authorisation_request;
      this_form_action = 'pre_authorisation_check';
      this_onsubmit = pre_authorisation_check_onsubmit;
      this_dfv_js   = pre_authorisation_check_dfv_js;
    ELSE;
      this_form_action = 'update_report';
      this_onsubmit = report_onsubmit;
      this_dfv_js   = report_dfv_js;
    END;
  %]

  [% IF dfv_errors %]
    <p class="error">[% c.cfg('msg').dfv_errors %]</p>
  [% END %]

  [% this_dfv_js # set above %]

  [% PROCESS record/content_header.tt %]

  [% INCLUDE site/previous_requests.tt # don't use PROCESS or 'data' gets clobbered %]

  [% patient_notes = patient.patient_note || outreach.demographics.patient_notes %]
  <form method="post" name="patientNotes" id="patientNotes"
      action="[% app_url %]/patient/patient_notes/[% request_data.id %]/[% patient.id %]">
    <p class="title">
      Patient notes: <textarea name="patient_notes" rows="1" cols="70"
        style="vertical-align: middle" class="expand20-200"
        id="patient_notes">[% patient_notes.detail %]</textarea>
      <input type="hidden" name="fwd_to" value="[% c.query.url(path_info=1) %]" />
      <input class="button" type="submit" value="Update" />
    </p>
  </form>

  [%
    PROCESS report/test_summary.tt;
	# PROCESS report/result_summary.tt IF result_summaries.size # combined with test_summary.tt
   %]

  [% IF lab_section_notes.size
    AND c.cfg('settings').report_display_notes.match('yes');
      # create id => section_name map:
      FOREACH section IN raw_lab_test_data.all_lab_sections;
      # PROCESS dumper.tt dump = section.as_tree;
        lab_sections.${section.id} = section.section_name;
      END; # PROCESS dumper.tt dump = lab_sections; %]
    <p class="title">Section notes:
      [% div_name = 'notes'; INCLUDE site/snippets/toggleview.tt %]
    </p>

    <div id="[% div_name %]" class="itemhidden">
      [% FOREACH section IN lab_section_notes; %]
        <p class="indent">
          <span class="title">[% lab_sections.item(section.key) %]:</span>
          [% section.value %]
        </p>
      [% END %]
    </div>
  [% END %]

  [% IF outstanding_tests.size; # PROCESS dumper.tt dump = outstanding_tests
      have_outstanding_tests = 1 # flag for later %]
    <p class="classname">
      <span class="highlight"><strong>outstanding tests</strong></span>
    </p>

    <div class="indent">
    [% FOREACH section IN outstanding_tests.keys %]
      <p class="indent">
        <span class="title">[% section %]:</span>
        [% outstanding_tests.item(section).join(', ') %]
      </p>
    [% END %]
    </div>
  [% END %]

  [% IF unresulted_tests.size; # PROCESS dumper.tt dump = outstanding_tests
      have_outstanding_results = 1 # flag for later %]
    <p class="classname">
      <span class="highlight"><strong>outstanding results</strong></span>
    </p>

    <div class="indent">
        <ul class="warning">
        [% FOREACH pair IN unresulted_tests %]
            <li>[% pair.key %]: [% pair.value %]</li>
        [% END %]
        </ul>
    </div>
  [% END %]

    <!-- BEGIN chart -->
    [% PROCESS site/make_chart.tt format_info = special_formatting %]
    <!-- END chart -->

  [% IF is_outreach; # PROCESS dumper.tt dump = outreach %]
    [% INCLUDE report/outreach.tt %]
    <p class="title">
        [% img_name = incomplete_dataset() ? 'question' : 'tick' %]
        <img src="/images/[% img_name %].gif" alt="" />
        Report [% div_name = 'report'; INCLUDE site/snippets/toggleview.tt %]
    </p>

    [% class = url_with_path.match('update_report') ? 'itemshow' : 'itemhidden' %]
    <div id="[% div_name %]" class="[% class %]">
        <p class="indent">
          <img src="[% app_url %]/chart/outreach_common/[% data.patient_case.patient_id %]" />
		  [% IF outreach.requested_lab_tests.grep('rq_cml').size %]
            <img src="[% app_url %]/chart/imatinib/[% data.id %]?outreach=1" />
          [% ELSE %]
            <img src="[% app_url %]/chart/outreach_others/[% data.patient_case.patient_id %]" />
          [% END %]
        </p>
        [% INCLUDE outreach/data_summary.tt; # PROCESS dumper.tt dump = outreach %]
        [% INCLUDE outreach/questionnaire_summary.tt; %]
        <h5>Report table:</h5>
        [% INCLUDE report/report.tt %]
    </div>
  [% ELSE; # add css file for biopsy_site - not used by Outreach reports:
      addcss = [ 'multiple-select.css' ] %]
    <p class="section_header">Report</p>
    [% INCLUDE report/report.tt %]
  [% END %]
<!-- END [% template.name %] -->
