<!-- BEGIN [% template.name %] -->
    [% META title = 'BCR ABL data' %]

    [% # PROCESS dumper.tt dump = data %]

    [% BLOCK missing_ct_result %]
        <span class="error">ERROR!!</span>
    [% END %]

    <h2>
        BCR ABL ratio data summary
        [% IF data.size; '[' _ data.size _ ']'; END %]
    </h2>

    [% IF data.size %]
    <form action="[% app_url %]/data-import/do_bcr_abl" method="post" name="bcr_abl"
        id="bcr_abl">
    <table class="indent">
        <tr>
            <td class="borderless"></td>
            <th scope="col">LabNo</th>
            <th scope="col">Name</th>
            <th scope="col">BCR ABL</th>
			<th scope="col">CT</th>
            <th scope="col">ABL</th>
			<th scope="col">CT</th>
            <th scope="col">Result</th>
            <td class="borderless" colspan="2"></td>
        </tr>
        [% FOREACH entry IN data.keys.sort;
                # PROCESS dumper.tt dump = data.item(entry).copy_number;
            patient = data.item(entry).request_data.patient_case.patient;

            ABL = 0;
            FOREACH i IN data.item(entry).copy_number.ABL; ABL = ABL + i; END;

			ABL_VALS = data.item(entry).copy_number.ABL.join(' + ');
			ABL_CT_DATA = data.item(entry).CT.ABL;

			ABL_CT = ABL_CT_DATA.join(' / ');
			ABL_CT_A = ABL_CT_DATA.0 || 0; # to avoid: "isn't numeric in subtraction"
			ABL_CT_B = ABL_CT_DATA.1 || 0;
			ABL_CT_DIFF = ABL_CT_A - ABL_CT_B;

            BCR_ABL = 0;
            FOREACH i IN data.item(entry).copy_number.${'BCR ABL'};
                BCR_ABL = BCR_ABL + i;
            END;

			BCR_ABL_VALS = data.item(entry).copy_number.${'BCR ABL'}.join(' + ');
			BCR_ABL_CT_DATA = data.item(entry).CT.${'BCR ABL'};

			BCR_ABL_CT = BCR_ABL_CT_DATA.join(' / ');
			BCR_ABL_CT_A = BCR_ABL_CT_DATA.0 || 0; # to avoid: "isn't numeric in subtraction"
			BCR_ABL_CT_B = BCR_ABL_CT_DATA.1 || 0;
			BCR_ABL_CT_DIFF = BCR_ABL_CT_A - BCR_ABL_CT_B;

            # reset defaults for loop:
            class  = 'normal';
            status = 'normal';
            ratio  = 0;
        %]
        <tr>
            <td>[% loop.count %]</td>
            <td>[% entry %]</td>
            [% IF
                patient.last_name != data.item(entry).sample_name;
                    status = 'error';
                ELSIF data.item(entry).error;
                    status = 'warning';
                END;
            %]
            <td class="[% status %]">
                [% data.item(entry).sample_name | upper %]
                [% IF status == 'error' %]
                    [[% patient.last_name | upper %]]
                [% END %]
            </td>

            [% class = BCR_ABL AND BCR_ABL <= 10 ? 'red' : 'normal' %]
            <td class="[% class %]">
                [% BCR_ABL %] [% IF BCR_ABL %]([% BCR_ABL_VALS %])[% END %]
            </td>

			[% class = BCR_ABL_CT
                ? ( BCR_ABL_CT_DIFF > 1.5 OR BCR_ABL_CT_DIFF < -1.5 )
                    ? 'red' : 'normal'
                    : 'input_error';
            %]
			<td class="[% class %]">
                [%  IF BCR_ABL_CT; BCR_ABL_CT;
                    ELSE; PROCESS missing_ct_result; status = 'error'; # disable checkbox
                    END %]
            </td>

            [% class = ABL < 10000 ? 'blue' :
                ABL >= 10000 AND ABL < 20000 ? 'warning' : 'normal' %]
            <td class="[% class %]">
                [% ABL %] [% IF ABL %]([% ABL_VALS %])[% END %]
            </td>

			[% class = ABL_CT
                ? ( ABL_CT_DIFF > 1.5 OR ABL_CT_DIFF < -1.5 )
                    ? 'red' : 'normal'
                    : 'input_error';
            %]
			<td class="[% class %]">
                [%  IF ABL_CT; ABL_CT;
                    ELSE; PROCESS missing_ct_result; status = 'error'; # disable checkbox
                    END %]
            </td>

            <td>
                [%
                    messages = c.messages('file_upload').bcr_abl;

                    IF ABL < 10000;
                        messages.poor_quality.summary;
                    ELSIF BCR_ABL; # ie not zero (0.58 conversion factor introduced 26/7/17)
                        ratio = ( BCR_ABL / ABL ) * 0.58 * 100 | format('%.3f');
                        ratio _ '%';
                    ELSE; # no bcr abl
                        expression = 100 / ABL | format('%.4f');
                        '<' _ expression | html;
                    END;
                %]
            </td>
            <td>
                [% result = [];
                    result.push(entry); # labno for template summary
                    result.push(data.item(entry).request_data.id);
                    result.push(ABL);
                    result.push(BCR_ABL);
                    result.push(ratio);
                %]
                <input type="checkbox" name="result" value="[% result.join('::') %]"
                    [% UNLESS status == "normal"; 'disabled'; END %] />
            </td>
            <td class="borderless">
                [% previous_result = data.item(entry).existing_result.results_summary;
                    IF previous_result;
                %]
                    <span class="warning">WARNING:</span>
                    <span class="acronym" title="[% previous_result %]">
                        has previous result
                    </span>
                [% END %]
            </td>
        </tr>
        [% END %]
        <tr>
            <td colspan="6" align="right" class="borderless">
                <input type="button" name="CheckAll" value="Select all"
                  onClick="checkAll(document.bcr_abl.result)">
                <input type="button" name="UnCheckAll" value="Clear all"
                  onClick="uncheckAll(document.bcr_abl.result)">
                [% site.html.submit %]
            </td>
        </tr>
    </table>
    </form>
    [% ELSE %]
        <p class="error">Error: unable to extract any data from data file(s). Check:
            <ol id="id">
                <li>data file is an xls file, or a zip file containing at least 1 xls file</li>
                <li>data file contains ABL & BCR ABL data</li>
                <li>the 'Experiment File Name' field contains the string 'Q922'</li>
                <li>the 'Sample Name' field contains the LabNo & Name data</li>
            </ol>
        </p>
        <p class="indent">[% site.html.back_button %]</p>
    [% END %]
<!-- END [% template.name %] -->
