<!-- BEGIN report/report.tt -->
    [% # PROCESS dumper.tt dump = history.reported.time %]
    [% # PROCESS dumper.tt dump = required_fields %]
	[% # PROCESS dumper.tt dump = has_optional %]
    [% # PROCESS dumper.tt dump = report_data %]
    [% # PROCESS dumper.tt dump = request_data %]
    [% # PROCESS dumper.tt dump = draft_reports %]
	[% # PROCESS dumper.tt dump = draft_report_data %]

	[% BLOCK required_field %]
	  <span class="red">*</span>
	[% END %]

	[% IF draft_reports.size AND NOT history.reported AND NOT is_outreach %]
	  <div class="indent">
		<span class="warning">This request has [% draft_reports.size %]
		  draft report[% 's' IF draft_reports.size > 1 %]:</span>
		<ol class="indent" id="draft-reports">
		  [% FOREACH entry IN draft_reports; # INCLUDE dumper.tt dump = entry.as_tree %]
			<li>
			  [% entry.user.first_name.ucfirst %] [% entry.user.last_name.upper %] ::
			  [% entry.updated_at.strftime('%d.%b.%Y') %]
			  [ <a href="[% app_url %]/report/preview_draft/[% entry.request_id %]/[%
				entry.user_id %]">preview</a> ]
			</li>
		  [% END %]
		</ol>
	  </div>
	[% END %]

    [% IF spelling_checked AND NOT has_unrecognised.size %]
      <p class="info">
        <span class="indent">Spellchecker reports no unrecognised words</span>
      </p>
    [% END %]

    <form action="[% app_url %]/report/[% this_form_action %]/[% request_data.id %]"
      method="post" [% this_onsubmit # set above %] name="reportForm" id="reportForm">

	  <!-- flag to allow deletion of secondary_diagnosis_id if desired: -->
	  <input type ="hidden" name="imported_draft_report"
		value="[% draft_report_data.size # ie 'true' if > 0 %]" />

    [% IF report_data.diagnosis # include diagnosis_id in case amendment: %]
      <input type="hidden" name="_diagnosis_id" value="[% report_data.diagnosis.id %]" />
    [% END %]

    <table class="indent" id="report">
	  <!-- request notes -->
	   <tr>
        <td class="label">Request notes:</td>
        <td class="content">
          <textarea name="general_notes" rows="1" cols="70" class="expand20-200"
            [% site.html.onfocus %] [% site.html.disabled IF authorisation_request %]
              >[% request_data.request_general_note.detail %]</textarea>
        </td>
      </tr>
	  <!-- end request notes -->

	  <!-- request/clinical details -->
	   <tr>
        <td class="label" style="vertical-align: top">
		  [% INCLUDE required_field  IF required_fields.clinical_details %]
		  Request details:
		</td>
        <td class="content">
          <textarea name="clinical_details" rows="1" cols="70" class="expand20-200"
            [% site.html.onfocus %] [% site.html.disabled IF authorisation_request %]
              >[% IF is_outreach AND NOT report_data.clinical_details;
                    'Outreach monitoring'; ELSE; report_data.clinical_details; END;
                %]</textarea>
          [% error_clinical_details %]
        </td>
      </tr>
	  <!-- end request/clinical details -->

      <tr>
        <td colspan="2"><hr /></td>
      </tr>

  <!-- specimen details -->
      <tr>
        <td colspan="2"><span class="info">Specimen details:</span></td>
      </tr>

	<!-- biopsy site -->
      [% IF want_biopsy_site OR report_data.biopsy_site;
		  sites_list = []; # create array of site names for legacy data
		  FOREACH s IN anatomical_sites;
			sites_list.push(s.site_name);
		  END; # INCLUDE dumper.tt dump = anatomical_sites
	  %]
      <tr>
        <td class="label" style="vertical-align: top">
		  [% INCLUDE required_field %]
		  Anatomical site:
		</td>
        <td class="content"> <!--biopsy_site=[% report_data.biopsy_site %]-->
          <input type="hidden" name="_want_biopsy_details" value="1" />

		  [% # get list of biopsy_sites from report_data.biopsy_site field:
			  biopsy_sites = report_data.biopsy_site.split('; '); # will be single entry if no ';'
				# INCLUDE dumper.tt dump = biopsy_sites;
			  # work out if any biopsy site NOT in sites_list:
			  unrecognised_sites = [];
			  FOREACH s IN biopsy_sites; # INCLUDE dumper.tt dump = s;
				unrecognised_sites.push(s) UNLESS sites_list.grep('^' _ s _ '$').size;
			  END; # INCLUDE dumper.tt dump = unrecognised_sites;
			  IF unrecognised_sites.size; # load free-text entry:
		  %]
			[%# this needs to be reversed when legacy cases reported %]
			<textarea name="biopsy_site" rows="1" cols="70" [% site.html.onfocus %]
			  class="expand20-200" [% site.html.disabled IF authorisation_request %]
			  id="biopsy_site">[% report_data.biopsy_site %]</textarea>
			  [% error_biopsy_site %]
			<!--
			  <div style="padding-bottom: 15px">
				[<span class="info">original site description:
				  "[% report_data.biopsy_site %]"</span>]
			  </div>
			-->
		  [% ELSE; # no unrecognised sites - load select menu: %]
			<style>
			  #biopsy-site-select li {
				  padding: 0px;
				  font-size: small;
			  }
			</style>

			<div id="biopsy-site-select">
			  <div class="form-group">
				<select id="biopsySite" name="biopsy_site" multiple="multiple">
				  [% FOREACH site IN anatomical_sites;
					  site_name_RE = '^' _ site.site_name _ '$';
				  %]
					<option [% 'selected' IF biopsy_sites.grep(site_name_RE).size %]
					  value="[% site.site_name %]">[% site.site_name %]
					  [% # '['; site.snomed; ']' # pointless - this is for *primary* site %]
					</option>
				  [% END %]
				</select>
				[% error_biopsy_site; # PROCESS dumper.tt dump = sites_list %]
			  </div>
			</div>

			<script src="/js/jquery.multiple.select.js"></script>
			<script>
				$(function() {
					$('#biopsySite').change(function() {
						console.log($(this).val());
					}).multipleSelect({
						width: 600,
						filter: true,
						selectAll: false,
						multiple: true,
						multipleWidth: 280,
						placeholder: "Select one or more ....",
					});
				});
			</script>
		  [% END %]
          [% IF unrecognised_sites.size %]
            <div class="red">WARNING: unrecognised anatomical site</div>
          [% END %]

          [% IF unrecognised_biopsy_site_words.size;
            PROCESS report/highlight.tt
              this_text  = highlighted_biopsy_site
              this_field = 'biopsy_site';
			PROCESS report/speller.tt list = unrecognised_biopsy_site_words;
			END;
		  %]
		</td>
      </tr>
      [% END %]
	<!-- end biopsy site -->

	<!-- gross description -->
      [% IF want_gross_description; # OR request_data.request_gross_description; why??
		  # can't rely on request_report_view for gross_description as it inner
		  # joins rrd with rsd so using left-join'd rsd as separate table
          request_gross_description = request_data.request_specimen_detail.gross_description;
          draft_gross_description   = draft_report_data.gross_description;
          # need to load original request_data gross description first in case also
          # have a draft gross_description:
          gross_description = request_gross_description OR draft_gross_description;
	  %]
      <tr>
        <td class="label" style="vertical-align: top">
		  [% INCLUDE required_field %]
		  Gross description:
		</td>
        <td class="content">
          <input type="hidden" name="_want_gross_description" value="1" />
       		<textarea name="gross_description" rows="1" cols="70" [% site.html.onfocus %]
             class="expand20-200" [% site.html.disabled IF authorisation_request %]
            id="gross_description">[% gross_description %]</textarea>
          [% error_gross_description %]

          [% # IF both draft & request_data gross_description, and both different:
            IF request_gross_description AND draft_gross_description
               AND request_gross_description != draft_gross_description %]
                <div class="red">DRAFT: [% draft_gross_description %]</div>
          [% END %]

          [% IF unrecognised_gross_description_words.size;
            PROCESS report/highlight.tt
              this_text  = highlighted_gross_description
              this_field = 'gross_description';
            PROCESS report/speller.tt list = unrecognised_gross_description_words;
          END %]
		</td>
      </tr>
      [% END %]
	<!-- end gross description -->

	<!-- morphology -->
	  [% UNLESS authorisation_request %]
      <tr>
        <td class="label" style="vertical-align: top">
		  [% INCLUDE required_field IF required_fields.morphology %]
		  Morphology:
		</td>
        <td class="content">
       		<textarea name="morphology" rows="1" cols="70" [% site.html.onfocus %]
            class="expand20-200" [% site.html.disabled IF authorisation_request %]
            id="morphology">[% report_data.morphology %]</textarea>
          [% error_morphology %]

          [% IF unrecognised_morphology_words.size;
            PROCESS report/highlight.tt
              this_text  = highlighted_morphology
              this_field = 'morphology';
            PROCESS report/speller.tt list = unrecognised_morphology_words;
          END %]
		</td>
      </tr>
	  [% END %]
	<!-- end morphology -->

	<!-- specimen quality -->
      <tr>
        <td class="label">
		  [% INCLUDE required_field IF required_fields.specimen_quality %]
		  Specimen quality:
		</td>
		[%
          specimen_quality = report_data.specimen_quality;
          DEFAULT specimen_quality = 'adequate' IF is_genomics; # maybe make hidden field ??
        %]
        <td class="content">
          [% FOREACH option IN ['good', 'adequate', 'poor'];
            quality_match = specimen_quality.match(option) ? 1 : 0;
		  %]
			<label>
			  <span class="[% quality_match ? 'title' : 'normal' %]">
				[% option | ucfirst %]</span>
			  <input type="radio" name="specimen_quality" value="[% option %]"
                [% site.html.disabled IF authorisation_request %]
                [% 'checked' IF quality_match %] />
			</label>
          <span style="margin-left: 0.5em" class="spacer">&nbsp;</span>
          [% END %]
          [% error_specimen_quality %]
		</td>
      </tr>
	  <!-- end specimen quality -->

  <!-- diagnosis details -->
      <tr>
        <td colspan="2"><hr /></td>
      </tr>
      <tr>
        <td colspan="2"><span class="info">Diagnosis details:</span></td>
      </tr>

      <!-- primary diagnosis -->
      <tr>
        <td class="label" style="vertical-align: top">
		  [% INCLUDE required_field IF required_fields.diagnosis_id %]
		  Primary diagnosis:
		</td>

	  [% IF is_outreach # use select menu %]
        <td class="content">
          <select name="diagnosis_id">
            <option value="">-- select one --</option>
            [% FOREACH category IN diagnoses.keys.sort %]
            <optgroup label="[% category %]">
              [% FOREACH diagnosis IN diagnoses.item(category);
                NEXT UNLESS diagnosis.active.match('yes') OR
                  diagnosis.id == report_data.diagnosis_id # skip inactive unless req'd %]
              <option value="[% diagnosis.id %]" [% 'selected' IF
                  diagnosis.id == report_data.diagnosis_id
                  AND NOT authorisation_request %]>
                [% diagnosis.name %]
              </option>
              [% END %]
            </optgroup>
            [% END %]
          </select>
		[% ELSE;  # use autosuggest
			PROCESS site/autosuggest.tt
			  varname         = 'name'
			  param_id        = 'diagnosis_id'
			  param_name      = 'diagnosis'
			  method_name     = 'get_diagnoses'
			  watermark       = 'no'
              # to recover legacy diagnosis if no longer active:
              optional_params = [ "diagnosis_id=${report_data.diagnosis.id}" ]
		%]
		<td class="search_bar">
		  <input style="width: 500px" type="text" id="diagnosis" name="_diagnosis"
			[% UNLESS authorisation_request # hide diagnosis entry %]
			  value="[% report_data.diagnosis.name %]"
			[% END %] />
		  <input type="hidden" id="diagnosis_id" name="diagnosis_id"
			  value="[% report_data.diagnosis_id %]" />
		[% END %]
          [% error_diagnosis_id %]

          [% IF report_data.diagnosis AND NOT authorisation_request %]
            <span class="title">Revision:</span>
            <select name="option_id">
              <option value="">--</option>
              [% FOREACH option IN diagnosis_change_options;
                NEXT UNLESS option.is_active.match('yes') %]
              <option value="[% option.id %]" title="[% option.description %]">
                    [% option.option_name %]
              </option>
              [% END %]
            </select>
            [% error_revision; error__diagnosis_id # could be either %]
          [% END %]

          [% IF previous_diagnoses.size AND c.user_can('report')
                AND NOT authorisation_request; # PROCESS dumper.tt dump = previous_diagnoses %]
          <p><i>Revisions [[% previous_diagnoses.size %]]:</i>
            [% div_name = 'diagnoses'; INCLUDE site/snippets/toggleview.tt %]
          </p>

          <div id="[% div_name %]" class="itemhidden">
              <ol>
              [% FOREACH d IN previous_diagnoses %]
                <li class="results">
                  [% d.diagnosis.name | html %]
                  [[% d.user.username | upper %]:
                  <span class="acronym" title="[% d.option.description %]">
                    [% d.option.option_name %]
                   </span>]
                  [% d.time.strftime('%a %d.%b.%Y @ %T') %]
                </li>
              [% END %]
            </ol>
          [% END %]
		</td>
      </tr>
	  <!-- end primary diagnosis -->

	<!-- comment -->
    [% UNLESS authorisation_request %]
      <tr>
        <td class="label" style="vertical-align: top">
		  [% INCLUDE required_field IF required_fields.comment %]
		  Comment:
		</td>
        <td class="content">
        	<textarea name="comment" rows="3" cols="70" class="expand20-500"
            id="comment">[% report_data.comment | html %]</textarea>
          [% error_comment %]

          [% IF unrecognised_comment_words.size;
            PROCESS report/highlight.tt
              this_text  = highlighted_comment
              this_field = 'comment';
            PROCESS report/speller.tt list = unrecognised_comment_words;
          END %]
		</td>
      </tr>
    [% END %]
	<!-- end comment -->

	<!-- secondary diagnosis -->
    [% UNLESS is_outreach %]
    <tr>
        <td class="label" style="vertical-align: top">Secondary diagnosis:</td>
        <td class="content search_bar">
		[%	PROCESS site/autosuggest.tt
			  varname         = 'name'
			  param_id        = 'secondary_diagnosis_id'
			  param_name      = 'secondary_diagnosis'
			  method_name     = 'get_diagnoses'
			  watermark       = 'no'
              # to recover legacy diagnosis if no longer active:
              optional_params = [ "diagnosis_id=${report_data.secondary_diagnosis.id}" ]
              no_autosuggest_js = 1; # already loaded above

		  secondary_diagnosis = request_data.request_secondary_diagnosis.diagnosis
			|| report_data.secondary_diagnosis; # latter is from import draft report
		%]
		  <input style="width: 500px" type="text" id="secondary_diagnosis"
			[% UNLESS authorisation_request # hide diagnosis entry %]
			  value="[% secondary_diagnosis.name %]"[% END %]
			  name="_secondary_diagnosis" />
		  <input type="hidden" id="secondary_diagnosis_id" name="secondary_diagnosis_id"
			  value="[% secondary_diagnosis.id %]" />
		  [% # can't delete by clearing text - autosuggest ignores < 2 chars:
			IF secondary_diagnosis %]
			  <span class="bold">DELETE:</span>
			  <input type ="checkbox" name="delete_secondary_diagnosis" value="1" />
		  [% END %]
          [% error_secondary_diagnosis_id %]
		</td>
    </tr>
    [% END %]
	<!-- end secondary diagnosis -->
  <!-- end diagnosis details -->

    <tr>
        <td colspan="2"><hr /></td>
    </tr>

    [%
        IF is_outreach;
            INCLUDE outreach/followup.tt;
		ELSIF is_genomics;
		  '<tr><td><input type="hidden" name="status" value="default" /></td></tr>';
        ELSE;
    %]
	  <!-- request status -->
	[% IF NOT previous_requests.this_id.size AND NOT report_data.status;
	  class = 'bkgd_light_yellow'; unknown_patient = 1;
	%]
	<tr>
	  <td class="[% class %]" colspan="2" align="center">
		<span class="red">** this patient is unknown to HILIS **</span>
	  </td>
	</tr>
	[% ELSE; class = 'normal'; END %]
      <tr>
        <td class="label">
		  [% INCLUDE required_field IF required_fields.status %]
		  Request status:
		</td>
        <td class="[% class %]">
          [% FOREACH option IN [ 'new', 'relapse', 'default' ];
              status_match = report_data.status.match(option) ? 1 : 0 %]
			<label>
              <span class="[% status_match ? 'title' : 'normal' %]">
              [% option | ucfirst %]</span><input type="radio" name="status"
                value="[% option %]" [% 'checked' IF status_match %]
                [% site.html.disabled IF authorisation_request %] />
			  </label>
			<span style="margin-left: 0.5em">&nbsp;</span>
          [% END %]
          [% error_status %]
		  [% IF unknown_patient %]
			<span class="red">&#171; is this a new ICDO diagnosis ?</span>
		  [% END %]
		</td>
      </tr>
	  <!-- end request status -->
    [% END %]

	<!-- other options -->
    [% UNLESS authorisation_request OR is_outreach %]
      <tr>
        <td class="label">Others:</td>
        <td class="content">
          [% IF request_options.teaching.is_active.match('yes') %]
            Teaching case:
                <input type="radio" value="1" name="teaching"
					[% 'checked' IF request_options.teaching.is_selected %]  />
                <img src="/images/tick.gif" />
                <span style="margin-left: 0.5em" class="spacer">&nbsp;</span>
                <input type="radio" value="0" name="teaching"
					[% 'checked' UNLESS request_options.teaching.is_selected %]  />
                <span class="red">X</span>
          [% END %]
        </td>
      </tr>
    [% END %]
	<!-- end other options -->

  	<!-- report by -->
      <tr>
        <td class="label">Report by:</td>
        <td>
          [% IF history.reported.user %]
          <span class="title">
              [%  history.reported.user.first_name | ucfirst; ' ';
                  history.reported.user.last_name  | ucfirst %]
            </span> on <span class="title">
            [% history.reported.time.strftime('%a %d.%b.%Y @ %H:%M') %]
          </span>
         [% ELSE %]
            <span class="grey">
            [% this_user.first_name | ucfirst %]
            [% this_user.last_name  | ucfirst %]
            </span>
          [% END %]
        </td>
      </tr>
  	<!-- end report by -->

	<!-- authorised by -->
      [% IF history.authorised %]
        <tr>
          <td class="label">Authorised by:</td>
          <td>
            <span class="title">
              [%  history.authorised.user.first_name | ucfirst; ' ';
                history.authorised.user.last_name  | ucfirst; %]
            </span> on <span class="title">
              [% history.authorised.time.strftime('%a %d.%b.%Y @ %H:%M') %]
            </span>
          </td>
        </tr>
      [% ELSIF status_options.authorised.is_active.match('yes')
		AND ( c.query.param('pre-authorisation-check') OR self_authorisable ) %]
      <tr>
        <td class="label">Authorise report:</td>
        <td>
        [% IF this_user.username == history.reported.user.username
            AND NOT self_authorisable; disable_submit = 1 %]
          <span class="error">*** self-authorisation not permitted ***</span>
        [% ELSE %]
          <input type="checkbox" name="authorise" value="1" />
          <input type="hidden" name="pre-authorisation-check" value="1" />
        [% END %]
        </td>
      </tr>
      [% END %]
	<!-- end authorised by -->

	<!-- acknowledge outstanding tests -->
      <tr>
        <td colspan="2" class="borderless">
        [% IF have_outstanding_tests %]
          <input type="hidden" name="_have_outstanding_tests" value="1" />
          <div class="indent">
            <span class="normal">ACKNOWLEDGE OUTSTANDING TESTS:</span>
            <input type="checkbox" name="confirm_outstanding_tests" value="1" />
            [% IF error_confirm_outstanding_tests %]
              [% error_confirm_outstanding_tests %]
            [% ELSE %]
              <span class="warning">&#171; required</span>
            [% END %]
          </div>
        [% ELSIF # can't do if outstanding tests
            ( status_options.authorised.is_active.match('yes')
                ? history.authorised.time : history.reported.time )
            AND NOT data.status_option.description.match('complete') %]
            <div class="indent">
              CONFIRM FINAL DIAGNOSIS:
              <input type="checkbox" name="final_diagnosis" value="1" />
              BUT <b>DO NOT</b> ISSUE NEW REPORT:
              <input type="checkbox" name="no_new_report" value="1" />
            </div>
			[% # error__diagnosis_id # don't need %]
        [% END %]
        </td>
      </tr>
	<!-- end acknowledge outstanding tests -->

	<!-- acknowledge outstanding results -->
      [% IF have_outstanding_results %]
        <tr>
          <td colspan="2" class="borderless">
          <input type="hidden" name="_have_outstanding_results" value="1" />
          <span class="indent">
            <span class="normal">ACKNOWLEDGE OUTSTANDING RESULTS:</span>
            <input type="checkbox" name="confirm_outstanding_results" value="1" />
            [% IF error_confirm_outstanding_results %]
              [% error_confirm_outstanding_results %]
            [% ELSE %]
              <span class="warning">&#171; required</span>
            [% END %]
          </span>
          </td>
        </tr>
      [% END %]
	<!-- end acknowledge outstanding results -->

      [% disable_submit = 0 IF # override disabled submit button if:
        is_outreach # is Outreach case
        AND disable_submit # submit button *is* disabled
        AND NOT is_locked # record not locked
        AND c.user_can('report'); # use can report
      %]

      [% UNLESS disable_submit # set above %]
      <tr>
        <td colspan="2" align="center" class="borderless">
            [% IF is_outreach # status row skipped %]
              <input type="hidden" name="status" value="default" />
            [% END %]
          <input type="button" value="Check Spelling" onClick="openSpellChecker();" />
          [% IF spelling_checked # set flag for target rm update_report():
            || c.cfg('settings').require_spell_check.match('no') # not needed
            || authorisation_request # not needed %]
            <input type="hidden" name="_spelling_checked" value="1" />
            [% site.html.submit # seems to be suppressed by multiple-select jquery ??
			  || '<input value="Submit" type="submit" class="button" />' %]
          [% ELSE %]
            <button name="submit" value="preview" type="submit" style="color: #008000">
			  Preview</button>
			[% IF c.cfg('settings').have_draft_report AND NOT history.reported
			  AND NOT is_outreach %]
				<span style="margin-left: 150px">
				  <button name="submit" value="save_draft" type="submit" class="red">
					Save as draft</button>
				</span>
			[% END %]
          [% END %]
        </td>
      </tr>
      [% END %]

    </table>
  </form>

    [% UNLESS authorisation_request OR report_data.comment %]
        <!-- the javascript function to change the source of the frame -->
        <script type="text/javascript">
          function load_comment_template()  {
            var html = document.getElementById('comment_id').contentWindow.document.body.innerHTML;
            document.reportForm.comment.value=document.reportForm.comment.value + html
          }
        </script>

		<!-- set up a frame to hold the information we require -->
        <p class="indent">
          <span class="title">Comment section templates:</span>
		  [% div_name = 'report_tmpl';
			INCLUDE site/snippets/toggleview.tt %]
        </p>

        <div id="[% div_name %]" class="itemhidden">
          <iframe name="comment_frame" class="indent" id="comment_id" width="600"></iframe>

          <!-- set up a form to search for a canned template locally and submit
              to a javascript function -->
          <form name="comment_tmpl" enctype="multipart/form-data"
              method="post" action="[% app_url %]/ajax/load_template"
              target="comment_frame">
            <p class="indent">
              <!-- set up a file type input field which submits the form on change -->
              <input type="file" name="template" size="30" onChange="form.submit()" />
              <input type="button" onclick="load_comment_template();"
                  value="Import Template" />
            </p>
          </form>
        </div>
    [% END %]
<!-- END report/report.tt -->
