    [% META title="FluidX XTR-96 Plate Reader Import" %]
<!-- BEGIN [% template.name %] -->
    [% # INCLUDE dumper.tt dump = data %]
    [% # INCLUDE dumper.tt dump = data.storage %]

    <h2 class="header">FluidX XTR-96 plate reader data import</h2>

    <script type="text/javascript">
        function submitForm(s) {
            s.value = "Scanning plate, please wait ...";
            return true;
        }
    </script>

    [% BLOCK form_header %]action="[% app_url %]/storage/read_xtr_96" name="do_scan"
        id="do_scan" onsubmit="return submitForm(this.scan);" method="post"[% END %]

    [% IF data.size; # INCLUDE dumper.tt dump = data;
        import(data); # provides 'plate_data', 'storage' & 'plateId' keys
        missing_id = '<span class="error">RACK ID MISSING</span>';
        USE format; concat =
            # format('%s%02d'); # xtr-96
            format('%s%s'); # intellicode
        # plate wells, A1-D6 for 24-well, A1-F8 for 48-well, A1-H12 for 96-well:
        alpha_min = 'A';  numbr_min = 1;
        alpha_max = well_max.alpha_max;
        numbr_max = well_max.numbr_max;
    %]
        [% IF storage.have_missing; # INCLUDE dumper.tt dump = storage.have_missing %]
            <p class="indent error">Warning: unknown vial(s) present - either
                uncatalogued or already removed. Requires resolution before
                import/export.
            </p>
        [% END %]

        <table class="indent">
            <tr>
                <th colspan="12">Plate ID :: [% plateId || missing_id %]</th>
            </tr>
            [% FOREACH row IN [alpha_min .. alpha_max] %]
                <tr>
                    [% FOREACH col IN [numbr_min .. numbr_max]; # INCLUDE dumper.tt dump = [row, col];
                        cell = concat(row,col); # ie format('%s%02d')
                        # highlight if vialId NOT already in catalog:
                        IF storage.rack_data.item(plate_data.${cell});
                            class = 'xtr-96-vial';
                        ELSIF plate_data.${cell}.match('No Tube');
                            class = 'bold';
                        ELSE; class = 'red'; END; # probably 'No Read'
                    %]
                        <td>
                            <div class="xtr-96-cell">[% cell %]</div>
                            <div class="[% class %]">[% plate_data.${cell} %]</div>
                        </td>
                    [% END %]
                </tr>
            [% END %]
            <tr class="borderless">
                <td align="right" colspan="12">
                  <form [% INCLUDE form_header %]>
                    <input type="hidden" name="type" value="[% c.query.param('type') %]" />
                    <a class="scan-btn reset-btn" href="[% app_url %]/storage/read_xtr_96">Reset</a>
                    <input type="submit" class="scan-btn" name="scan" value="RESCAN" />
                  </form>
                </td>
            </tr>
        </table>

        [% IF plateId AND NOT storage.have_missing; # will need to fix & re-scan %]
            [% storage_rack_dfv_js %]
            <form action="[% app_url %]/storage/import_xtr_96_data" method="post"
                [% storage_rack_onsubmit %] name="import-data" id="import-data">
                <div class="bold indent">
                    [% IF storage_locations %]
                        <span class="warning">Select storage location:</span>
                        [% FOR sl IN storage_locations %]
                            <div class="indent">
                                <input type="radio" name="storage_location"
                                    value="[% sl %]" /> [% sl %]
                            </div>
                        [% END %]
                        <div class="indent"><!-- pass value of zero to pass js validation -->
                            <input type="radio" name="storage_location" value="0" />
                            No storage
                        </div>
                        [%# site.html.reset # not required - mandatory field %]
                    [% ELSE %]
                        <span class="warning">Storage location:</span>
                        <input type="text" name="storage_location" size="30" />
                    [% END %]
                </div>
                <p class="indent">
                    <span class="info">
                        [% rack_in_use = storage.storage_rack.is_active %]
                        Rack [% rack_in_use ? 'exists' : 'does not exist' %]
                            in storage:
                    </span>
                    [%
                        hash = {
                            name  => rack_in_use ? 'export' : 'import',
                            value => rack_in_use ? 'EXPORT ALL' : 'IMPORT',
                        };
                    %]
                    <input type="submit" class="[% hash.name %]-btn"
                        name="[% hash.name %]" value="[% hash.value %]" />
                    [% IF rack_in_use %]
                        <input type="hidden" name="_plateId" value="[% plateId %]" />
                        <span style="margin-left: 15px">
                            <input type="submit" name="void_plate" class="red"
                                value="VOID PLATE ..." />
                        </span>
                    [% END %]
                    [% IF storage.have_empty %]
                        <span class="warning">
                            empty co-ordinates detected, ensure this is correct
                            before continuing!!
                        </span>
                    [% END %]
                </p>
            </form>
        [% END %]
    [% ELSIF can_scan; %]
      <form [% INCLUDE form_header; %]>
        [% IF c.cfg('settings').test_harness # .t can't run submitForm js function %]
            <input type="hidden" name="scan" value="1" />
        [% END %]
        <div class="indent">
            <span class=" info">Plate size:</span>
            <input type="radio" name="type" value="96"
                [% 'checked' UNLESS c.query.param('type').match('48') %] />96-well
            <input type="radio" name="type" value="48"
                [% 'checked' IF c.query.param('type').match('48') %] />48-well
        </div>
        <div class="indent warning">Ensure rack is mounted on plate reader .....
            <input type="submit" class="button scan-btn" name="scan" value="GO" />
        </div>
      </form>
    [% ELSE %]
        <p class="indent info">Cannot activate plate reader at
            <span class="error">[% xtr_96_addr %]</span> from remote location
            <span class="error">[% remote_addr %]</span>
        </p>
    [% END %]
<!-- END [% template.name %] -->
