[% META title = 'Resources &raquo; Referrers & Referral Sources' %]
<!-- BEGIN [% template.name %] -->
    [% SET skip_wrappers = 1 IF c.stash.skip_wrapper # shared between resource & popup %]

    [% # INCLUDE dumper.tt dump = data_files %]

    [% IF ! location_type %]
        <h2>Referrers &amp; Referral Sources</h2>
        <p class="info">Choose a resource type to search the <span class="acronym"
			title="Connecting for Health">CfH</span> data files:</p>
    [% END %]

    [% BLOCK row;
		query_param = c.query.param('lookup') || c.query.param('type');
		class = query_param.match(t) ? 'highlight' : 'normal'; %]

        [ <a href="[% app_url %]/referral-source[% IF c.stash.skip_wrapper # set flag:
                %]/select[% END %]?lookup=[% t %]">
            <span class="[% class %]">[% t %]</span>
        </a> ]
    [% END %]

	<p>Search:
        [%  FOREACH entry IN data_files.sort;
                INCLUDE row t = entry;
            END;
        %]
	<p>

<!-- // ajax method too slow
    <script type="text/javascript" src="/js/prototype.js"></script>
    <script type="text/javascript" src="/js/autocomplete.js"></script>

<div>
    <textarea rows="4" cols="50" id="display"
        onFocus="javascript: var options = {
                    script: function (string) {
                        return ('[% app_url %]/referral-source/lookup?type=[% location_type %];string='+string);
                    },
                    minchars: 3,
                    maxentries: 20,
                    timeout: 5000,
                }; var xml=new AutoComplete('display',options); return true;"></textarea>
</div>

<span id='xml_info'></span>
-->

[% IF location_type %]
	<h2>CfH data file search:</h2>
	<form method="get" action="[% app_url %]/referral-source/lookup">
		<input type="hidden" name="type" value="[% location_type %]" />
        <input type="hidden" name="skip_wrapper" value="[% c.stash.skip_wrapper %]" />
        <div class="indent">Containing:
			<input type="text" name="string" value="[% c.query.param('string') %]"
                size="40" />
			[% site.html.submit %]
        </div>
        <div class="indent info">
            [ force boundaries using '\b' eg 'york\b' finds 'york' but not 'yorkshire',
            '\bshire' find 'shires healthcare' but not 'lancashire' ]
        </div>
	</form>
[% END %]

[% BLOCK file_update;
	IF file_data;
		age = file_data.age;
	    IF    age >= 6;  class = 'error';
		ELSIF age >= 3;  class = 'warning';
		ELSE;            class = 'info';
		END %]

        <p class="indent">
            <i>source file: [% file_data.filename %].csv; status:
                <span class="[% class %]">updated [% file_data.age %] months ago</span>
            </i>
            [% IF age AND c.user_can('edit_pid'); # otherwise need to use admin function
				subdir = subdirs.item(file_data.filename);
				return = c.query.self_url FILTER url;
                cfg    = c.cfg('settings');
                USE url( app_url _ '/resources/get_data_file',
					_return = return
                    method  = 'url'
					# digital.nhs.uk format:
                    url = cfg.cfh_downloads _ '/' _ subdir _ '/' _ file_data.filename
						_ '/zip/' _ file_data.filename
                );
            %] [ <a href="[% url %]">update file</a> ] [% END %]
    [% END %]
[% END %]

[% IF c.query.param('string') %]
    [% IF matches.size %]
        <p class="bold">Found [% matches.size %] [% location_type %]s matching "[%
                c.query.param('string') %]":</p>
        [% INCLUDE file_update %]

        <table>
            <tr>
                <td>&nbsp;</td>
                <th><span class="acronym" title="national code">Code</span></th>
                <th>Details</th>
            </tr>
            [% FOREACH location IN matches;
                class = location.code.length == 3 AND
                    NOT c.query.param('type').match('trust')
                    ? 'bold' : 'normal' # all trusts have code length 3 %]
            <tr class="[% class %]">
                <td>[% loop.count %]</td>
                <td>
                [% IF class.match('bold') %]
                    <a href="[% app_url %]/referral-source/lookup?type=[% location_type
                            %];string=[% location.code %]">
                        [% location.code %]</a>
                [% ELSE %]
                    [% location.code %]
                [% END %]
                </td>
                <td>
                    [% display = location.display | html; # escape, then add markup:
                        highlight(display) # callback %]
                </td>
            </tr>
            [% END %]
        </table>
    [% ELSE %]
        <p class="bold">No matches found for "[% c.query.param('string') %]"</p>
        [% INCLUDE file_update %]
    [% END %]
[% END %]
<!-- END [% template.name %] -->
