<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    {% set page_title = 'File Report' %}
    <title>{{ page_title }}</title>

    {% set bloom_mod = 'dewey' %}

    <link rel="stylesheet" type="text/css" href="{{ style.skin_css }}">
    <link rel="stylesheet" type="text/css" href="/static/style.css">
    <script src="/static/legacy/action_buttons.js"></script>

    <style>
        .floating-button {
            position: fixed;
            bottom: 20px;
            right: 20px;
            background-color: #008CBA; /* Blue background */
            color: white; /* White text */
            border: none;
            padding: 15px;
            border-radius: 50%;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            cursor: pointer;
        }
    </style>

    <script>
        function downloadTableAsTSV() {
            const table = document.querySelector('table');
            if (!table) {
                alert('No table found!');
                return;
            }

            let tsv = [];
            const rows = table.querySelectorAll('tr');

            for (const row of rows) {
                const cols = row.querySelectorAll('td, th');
                const rowData = [];
                for (const col of cols) {
                    rowData.push(col.innerText);
                }
                tsv.push(rowData.join('\t'));
            }

            const tsvFile = new Blob([tsv.join('\n')], { type: 'text/tsv' });
            const downloadLink = document.createElement('a');
            const now = new Date();
            const timestamp = now.getFullYear() + "-" +
                              (now.getMonth() + 1).toString().padStart(2, '0') + "-" +
                              now.getDate().toString().padStart(2, '0') + "_" +
                              now.getHours().toString().padStart(2, '0') + "-" +
                              now.getMinutes().toString().padStart(2, '0') + "-" +
                              now.getSeconds().toString().padStart(2, '0');
            downloadLink.download = `dewey_file_create_log_${timestamp}.tsv`;
            downloadLink.href = window.URL.createObjectURL(tsvFile);
            downloadLink.style.display = 'none';
            document.body.appendChild(downloadLink);
            downloadLink.click();
            document.body.removeChild(downloadLink);
        }
    </script>
</head>
<body>
            
    {% include 'legacy/bloom_header.html' %}

    <h1>Create File Report</h1>
    <hr>
    <table>
        <thead>
            <tr>
                <th>Identifier</th>
                <th>Status</th>
                <th>Original File Name/URL</th>
                <th>S3 URI</th>
            </tr>
        </thead>
        <tbody>
            {% for result in results %}
            <tr>
                <td><a target=newxx href="euid_details?euid={{ result.identifier }}">{{ result.identifier }}</a></td>
                <td>{{ result.status }}</td>
                <td>{{ result.original }}</td>
                <td>{{ result.current_s3_uri }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    <hr>
    <a href="/dewey">Back to Create File Form</a>

    <button class="floating-button" onclick="downloadTableAsTSV()">⬇️</button>
</body>
</html>
