[% META title = 'Haematological Malignancy Research Network &raquo; Potential New Malignancies' %]

<!-- BEGIN [% template.name %] -->
    [% # PROCESS dumper.tt dump = cases %]
    [% # PROCESS dumper.tt dump = request_counts %]

    <p class="title">
        [% USE date(format = '%d.%m.%Y', locale = 'en_GB') %]
        Potential new malignancies [[% cases.size %]] for dates
        [% c.query.param('date_from') || date_from.dmy('.') %] to
        [% c.query.param('date_to')   || date.format %]        
        [% site.html.back_button %]
    </p>
    
    [% IF cases.size %]
    <table id="hmrn">
        <tr>
            <th scope="col">LabNo</th>
            <th scope="col">Name</th>
            <th scope="col">DoB</th>
            <th scope="col">Referrer</th>
            <th scope="col">Source</th>
            <th scope="col">AuthDate</th>
            <th scope="col">Unit Number</th>
            <th scope="col">NHS&nbsp;Number</th>
            <th scope="col">Diagnosis</th>
        </tr>
        [% FOREACH data IN cases; # PROCESS dumper.tt dump = data;
            patient = data.patient_case.patient;
            # calculate age:
            authorisation_date = data.request_history.0 # for duplicate actions
                ? data.request_history.0.time : data.request_history.time;
            age = calculate_age(patient.dob, authorisation_date);
        %]
        <tr>
            <td>
                [% this_link = "request_id=${data.id};format=templates" %]
                <a href="[% app_url %]/hmrn/new_diagnoses?[% this_link %]">
                    [% INCLUDE site/lab_number.tt want_url = 0 # link to tt not record %]
                </a>
            </td>
            <td>
                [%
                    nn = request_counts.item(patient.id) - 1; # always have 1 for current request
                    this_link = nn
                        ? "patient_id=${patient.id}"
                        : "id=${data.id}";
                %]
                <a href="[% app_url %]/search/do_search?[% this_link %];_return=1">
                    [% patient.last_name   | upper   %],
                    [% patient.first_name  | ucfirst %]
                    [% patient.middle_name | ucfirst %]
                    [% "[+$nn]" IF nn %]
                </a>
            </td>
            [% class = age < 18 ? 'red' : 'normal' %]
            <td class="[% class %]">[% patient.dob.dmy('.') %]</td>
            <td>[% data.referrer_department.referrer.name %]</td>
            <td>
                [% INCLUDE site/display_name.tt
                    location = data.patient_case.referral_source.display_name %]
            </td>
            <td>[% authorisation_date.dmy('.') %]</td>
            <td>[% data.patient_case.unit_number || site.html.grey_null %]</td>
            <td>[% INCLUDE site/nhs_number.tt nhs_number = patient.nhs_number %]</td>
            <td>
                [% IF data.request_report.status == 'new' %]
                    <strong>[% data.request_report.diagnosis.name %]</strong>
                [% ELSE %]
                    [% data.request_report.diagnosis.name %]
                    [% IF data.request_report.status == 'relapse' %][R][% END %]
                [% END %]
            </td>
        </tr>
        [% END %]
    </table>
    [% END %]
    
    [% IF known_previous.size; # PROCESS dumper.tt dump = known_previous.0.current.as_tree %]
        [% BLOCK previous_diagnosis %]
            [% PROCESS site/lab_number.tt want_url = 1, # data in correct format
                query_modifiers = [ '_return=1' ] %]
            [% this_link = "request_id=${data.id};format=templates;force_tmpl=1" %]
            <a href="[% app_url %]/hmrn/new_diagnoses?[% this_link %]">
                <img src="/images/text.gif" title="force template" />
            </a>
            [% data.patient_case.patient.last_name  | upper %],
            [% data.patient_case.patient.first_name | ucfirst %] ::
            [% data.request_report.diagnosis.name %]
        [% END %]
        
        <p class="header" style="page-break-before: always">
            Known previous diagnoses (dissimilar ICDO3 in red)
        </p>
        
        <div style="font-size: smaller; font-family: monospace">
        [% FOREACH case IN known_previous; # PROCESS dumper.tt dump = case.previous;
            # are any of previous icdo3's different to current:
            icdo_diff = []; # list to hold different icdo3's 
            FOREACH entry IN case.previous;
                previous_icdo3 = entry.value;                
                IF case.current.request_report.diagnosis.icdo3 != previous_icdo3;
                    icdo_diff.push(1); # just needs to have content to trigger effect
                END;
            END;
        %]            
           [% IF icdo_diff.size %]
            <div class="red">
                [% INCLUDE previous_diagnosis data = case.current %]
                [
                  [% FOREACH entry IN case.previous;
                    diagnosis = entry.key;
                    icdo3     = entry.value;
                    NEXT IF diagnosis == case.current.request_report.diagnosis.name;
                  %]
                    [% diagnosis %]
                    ([% icdo3 %])
                 [% END %]
                ]
            </div>
            [% ELSE %]
            <div>[% INCLUDE previous_diagnosis data = case.current %]</div>
            [% END %]
        [% END %]
        </div>
    [% END %]
    
    [% IF NOT cases.size AND NOT known_previous.size %]
        <p class="info">No records found for specified duration</p>
    [% END %]
<!-- END [% template.name %] -->
