<!-- BEGIN report/report.tt -->
    [% # PROCESS dumper.tt dump = history.reported.time %]
    
    [% 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">
    
    [% 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">
	   <tr>
        <td class="label">General 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>

      <tr>
        <td class="label" style="vertical-align: top">Clinical details:</td>
        <td class="content">				 
       		<textarea name="clinical_details" rows="1" cols="70" [% site.html.onfocus %]
            class="expand20-200" [% site.html.disabled IF authorisation_request %]
            id="clinical_details">[% report_data.clinical_details %]</textarea>
          [% error_clinical_details %]
          
          [% IF unrecognised_clinical_details_words.size %]
            [% PROCESS report/highlight.tt
              this_text  = highlighted_clinical_details
              this_field = 'clinical_details' %]
              
            [% PROCESS report/speller.tt list = unrecognised_clinical_details_words %]
          [% END %]
		</td>
      </tr>
      
      [% IF want_gross_description OR request_data.request_gross_description %]
      <tr>
        <td class="label" style="vertical-align: top">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 %]
            >[% request_data.request_gross_description.detail %]</textarea>
          [% error_gross_description %]
		</td>
      </tr>
      [% END %]
      
      <tr>
        <td class="label">Specimen quality:</td>
        <td class="content">
          [% FOREACH option IN ['good', 'adequate', 'poor'];
            quality_match = report_data.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>
      
    [% UNLESS authorisation_request %]
      <tr>
        <td class="label" style="vertical-align: top">Morphology/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 %]
      
      <tr>
        <td class="label" style="vertical-align: top">
          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>
      
    [%
        IF is_outreach;
            INCLUDE outreach/followup.tt;
        ELSE;
    %]
      <tr>
        <td class="label">Request status:</td>
        <td class="content">
          [% 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 %]
				[% UNLESS previous_cases.this_id.size %]
          <span class="red">** unknown patient details **</span>
				[% END %]
		</td>
      </tr>
    [% END %]
      
    [% 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 %]

      <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>
      
      [% 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 %]

      [% IF have_outstanding_tests %]
        <tr>
          <td colspan="2" class="borderless">
          <input type="hidden" name="_have_outstanding_tests" value="1" />
          <span 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 %]
          </span>          
          </td>
        </tr>
      [% 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') %]
        <tr>
          <td colspan="2" class="borderless">
            <span class="indent"><!--  style="float: right" -->
              <span class="normal">CONFIRM FINAL DIAGNOSIS:</span>          
              <input type="checkbox" name="final_diagnosis" value="1" />
            </span>
			[% # error__diagnosis_id # don't need %]
          </td>
        </tr>
      [% END %]

      [% 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 %]
    
      [% 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 %]
          [% ELSE %]
            <input type="submit" value="Preview" style="color: #008000" />
          [% END %]
          [% # site.html.reset # not wanted !!%]
        </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">Morphology/comment 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" src="#" 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 -->