  [% META title="Images &amp; Data Files" %]
<!-- BEGIN [% template.name %] -->

  [% # PROCESS dumper.tt dump = contents %]
  [% # PROCESS dumper.tt dump = request %]
  [% # PROCESS dumper.tt dump = c.query %]

  [% INCLUDE record/patient_summary.tt %]

  <h3 class="header">Image &amp; data files</h3>

  [% request_is_authorised
    = request.status_option.description.match('authorised|complete') %]

  [% UNLESS c.is_lab_staff %]
      <p class="indent warning">
        [% IF request_is_authorised; 'DATA FILES ARE FOR LABORATORY USE ONLY';
            ELSE; 'CONTENTS VIEW IS RESTRICTED ON UNAUTHORISED REQUEST'; END %]
      </p>
  [% END %]

  [% service_centre = c.cfg('settings').item('_centre'); # service_centre %]

  [% # images ===============================================================
    # lab-staff, or request needs to be authorised:
    IF contents.images.size AND ( c.is_lab_staff OR request_is_authorised );
  %]
      <p class="title">Images:</p>
  	  [% FOREACH file IN contents.images;
          parts = [ request.year, mini_dir, request.request_number ];
          file_src = parts.join('/');
      %]
        <div class="figure">
          <div class="photo">
            <a class="image" title="[% file %]" href="/files/[%
                  service_centre %]/[% file_src %]/[% file | uri %]">
                <img src="/files/[% service_centre %]/[% file_src %]/[% file | uri %]"
                    alt="" width="100" height="100" />
            </a>
		  </div>

          [% IF c.user_can('upload_data_files') %]
            <p>
                [ <a href="[% app_url %]/image/delete/[% request.id %]/[% file | uri %]"
                    alt="delete" title="delete [% file %]"><span class="red">x</span>
                </a> ]
            </p>
          [% END %]
		</div>
    [% END %]
    <br class="clearboth" />
  [% END %]

  [% # files ==================================================================
    IF contents.files.size %]
      <p class="title">Data files:</p>
      [% FOREACH file IN contents.files;
        # non-lab-staff can see only request-form for unauthorised requests:
        UNLESS c.is_lab_staff OR request_is_authorised;
            NEXT UNLESS file.match('request_form.pdf$');
        END;
        # show unrestricted only unless permissions allow:
        NEXT UNLESS unrestricted.item(file) OR c.user_can('upload_data_files');
        parts = [ request.year, mini_dir, request.request_number ];
        file_src = parts.join('/');
      %]
        <p class="indent">
          <a href="/files/[% service_centre %]/[% file_src %]/[% file | uri %]"
		     target="_blank">[% file %]</a>
          [% IF c.user_can('upload_data_files');
            NEXT IF file.match('request_form') AND NOT c.user_can('delete_record');
          %]
          [ <a href="[% app_url %]/image/delete/[%request.id %]/[% file | uri %]"
            alt="delete" title="delete file"><span class="red">x</span>
          </a> ]
        [% END %]
      </p>
    [% END %]
  [% END %]

  [% IF confirm_delete %]
	<form method="post" enctype="multipart/form-data"
        action="[% app_url %]/image/delete/[% request.id %]/[% marked_file | uri %]">
    <p class="warning">
      Confirm you wish to permanently delete [% marked_file %]:
      <input type="checkbox" name="confirm_delete" value="1" />
      [% site.html.submit %]
    </p>
  </form>
  [% END %]

  [% IF c.user_can('upload_data_files') %]
	<form method="post" action="[% app_url %]/image/update/[% request.id %]"
        enctype="multipart/form-data">
	[% IF confirm_overwrite %]
	  <p class="warning">
		File already exists - confirm overwrite:
		<input type="checkbox" name="confirm_overwrite" value="1" />
	  </p>
		[TODO: re-display path - not available in query()]
	[% END %]

	[% UNLESS request.id.match('_') # disabled for pre_registration requests %]
	<p class="title">
	  [% div_name = 'new'; INCLUDE site/snippets/toggleview.tt %]
		  Upload new file
	  </p>
	[% END %]

	<div class="item[% confirm_overwrite ? 'visible' : 'hidden' %]" id="[% div_name %]">
      <p class="indent">
        <input type="file" name="image_file" size="30" />
        [% site.html.submit %]
      </p>
	</div>

	</form>
  [% END %]

  <p>[% site.html.back_button %]</p>
<!-- END [% template.name %] -->
