{% extends "base.html" %}

{% block header %}
{% if active %}
<span style="display: inline-block; width: 100%; color: white; background-color: green; text-align: center; vertical-align: center; font-family: Arial, Helvetica, sans-serif;">Service Running</span>
{% else %}
<span style="display: inline-block; width: 100%; color: white; background-color: red; text-align: center; vertical-align: center; font-family: Arial, Helvetica, sans-serif;">Service Stopped</span>
{% endif %}
    <table width="100%">
        <tbody>
        <tr style="background-color: black;">
            <td style="background-color: black;">
                <a href="http://github.com/akuker/RASCSI">
                    <h1>RaSCSI - 68kmla Edition</h1>
                </a>
            </td>
        </tr>
        </tbody>
    </table>
{% endblock %}

    {% block content %}
    <h2>Current RaSCSI Configuration</h2>
    <form action="/config/load" method="post">
        <select name="name" >
            {% for config in config_files %}
                <option value="{{config}}">{{config.replace(".csv", '')}}</option>
            {% endfor %}
        </select>
        <input type="submit" value="Load" />
    </form>
    <form action="/config/save" method="post">
        <input name="name" placeholder="default">
        <input type="submit" value="Save" />
    </form>
    <form action="/scsi/detach_all" method="post" onsubmit="return confirm('Detach all SCSI Devices?')">
        <input type="submit" value="Detach All" />
    </form>

    <table cellpadding="3" border="black">
        <tbody>
        <tr>
            <td><b>ID</b></td>
            <td><b>Type</b></td>
            <td><b>File</b></td>
            <td><b>Action</b></td>
        </tr>
        {% for device in devices %}
        <tr>
            {% if device.id != "7" %}
            <td style="text-align:center">{{device.id}}</td>
            <td style="text-align:center">{{device.type}}</td>
            <td>{{device.file}}</td>
            <td>
                {% if device.type == "SCCD" and device.file != "NO MEDIA" %}
                <form action="/scsi/eject" method="post" onsubmit="return confirm('Eject Disk?')">
                    <input type="hidden" name="scsi_id" value="{{device.id}}">
                    <input type="submit" value="Eject" />
                </form>
                {% else %}
                <form action="/scsi/detach" method="post" onsubmit="return confirm('Detach Disk?')">
                    <input type="hidden" name="scsi_id" value="{{device.id}}">
                    <input type="submit" value="Detach" />
                </form>
                {% endif %}
            </td>
            {% else %}
            <td style="text-align:center">{{device.id}}</td>
            <td style="text-align:center">-</td>
            <td>Host Machine</td>
            <td>-</td>
            {% endif %}
        </tr>
        {% endfor %}
        </tbody>
    </table>

    <hr/>
    <h2>Image File Management</h2>
    <table cellpadding="3" border="black">
        <tbody>
        <tr>
            <td><b>File</b></td>
            <td><b>Size</b></td>
            <td><b>Actions</b></td>
        </tr>
        {% for file in files %}
        <tr>
            <td>{{file[0].replace(base_dir, '')}}</td>
            <td style="text-align:center">
                <form action="/files/download" method="post">
                    <input type="hidden" name="image" value="{{file[0].replace(base_dir, '')}}">
                    <input type="submit" value="{{file[1]}} &#8595;" />
                </form>
            </td>
            <td>
                <form action="/scsi/attach" method="post">
                    <input type="hidden" name="file_name" value="{{file[0]}}">
                    <select name="scsi_id">
                        {% for id in scsi_ids %}
                        <option value="{{id}}">{{id}}</option>
                        {% endfor %}
                    </select>
                    <input type="submit" value="Attach" />
                </form>
                <form action="/files/delete" method="post" onsubmit="return confirm('Delete file?')">
                    <input type="hidden" name="image" value="{{file[0].replace(base_dir, '')}}">
                    <input type="submit" value="Delete" />
                </form>
                {% if file[0].endswith('.zip') or file[0].endswith('.ZIP') %}
                <form action="/files/unzip" method="post">
                    <input type="hidden" name="image" value="{{file[0].replace(base_dir, '')}}">
                    <input type="submit" value="Unzip" />
                </form>
                {% endif %}
            </td>
        </tr>
        {% endfor %}
        </tbody>
    </table>

    <hr/>
    <h2>Attach Ethernet Adapter</h2>
    <p>Emulates a SCSI DaynaPORT Ethernet Adapter. Host drivers required.</p>
    <table style="border: none">
        <tr style="border: none">
            <td style="border: none; vertical-align:top;">
                <form action="/daynaport/attach" method="post">
                    <select name="scsi_id">
                        {% for id in scsi_ids %}
                        <option value="{{id}}">{{id}}</option>
                        {% endfor %}
                    </select>
                    <input type="submit" value="Attach" />
                </form>
            </td>
        </tr>
        <tr style="border: none">
            <td style="border: none; vertical-align:top;">
                {% if bridge_configured %}
                <small>Bridge is currently configured!</small>
                {% else %}
                <form action="/daynaport/setup" method="post">
                    <input type="submit" value="Create Bridge" />
                </form>
                {% endif %}
            </td>
        </tr>
    </table>

    <hr/>
    <h2>Upload File</h2>
    <p>Uploads file to <tt>{{base_dir}}</tt>. Max file size is set to {{max_file_size / 1024 /1024 }}MB</p>
    <table style="border: none">
            <tr style="border: none">
                <td style="border: none; vertical-align:top;">
                <form id="uploadForm" action="/files/upload/" onchange="fileSelect(event)" method="post" enctype="multipart/form-data">
                    <label for="file">File:</label>
                    <input type="file" name="file"/>
                    <input type="submit" value="Upload" />
                </form>
            </td>
        </tr>
    </table>
<script>
    function fileSelect(e) {
        document.getElementById("uploadForm").setAttribute('action', "/files/upload/" + e.target.files[0].name)
        console.log(e.target.files[0].name);
    }
</script>

    <hr/>

    <h2>Download File from Web</h2>
    <p>Given a URL, download that file to the <tt>{{base_dir}}</tt></p>
    <table style="border: none">
            <tr style="border: none">
                <td style="border: none; vertical-align:top;">
                <form action="/files/download_image" method="post">
                    <label for="url">URL:</label>
                    <input type="text" placeholder="URL" name="url" />
                    <input type="submit" value="Download" />
                </form>
            </td>
        </tr>
    </table>

    <hr/>

    <h2>Download File from web and create HFS CD</h2>
    <p>Given a URL this will download a file, create a HFS iso, and mount it on the device id given.</p>
    <table style="border: none">
            <tr style="border: none">
                <td style="border: none; vertical-align:top;">
                <label for="scsi_id">SCSI ID:</label>
                <form action="/files/download_to_iso" method="post">
                    <select name="scsi_id">
                        {% for id in scsi_ids %}
                        <option value="{{id}}">{{id}}</option>
                        {% endfor %}
                    </select>
                    <label for="url">URL:</label>
                    <input type="text" placeholder="URL" name="url" />
                    <input type="submit" value="Download and Mount ISO" />
                </form>
            </td>
        </tr>
    </table>

    <hr/>
    <h2>Create Empty Disk Image File</h2>
    <table style="border: none">
            <tr style="border: none">
                <td style="border: none; vertical-align:top;">
                <form action="/files/create" method="post">
                    <label for="file_name">File Name:</label>
                    <input type="text" placeholder="File name" name="file_name"/>
                    <label for="type">Type:</label>
                    <select name="type">
                        <option value="hda">SCSI Hard Disk image (APPLE GENUINE)</option>
                        <option value="hdn">SCSI Hard Disk image (NEC GENUINE)</option>
                        <option value="hdi">SCSI Hard Disk image (Anex86 HD image)</option>
                        <option value="nhd">SCSI Hard Disk image (T98Next HD image)</option>
                        <option value="hds">SCSI Hard Disk image (Generic - recommended for Atari computers)</option>
                        <option value="hdr">SCSI Removable Media Disk image (Generic)</option>
                        <option value="hdf">SASI Hard Disk image (XM6 SASI HD image - typically only used with X68000)</option>
                    </select>
                    <label for="size">Size(MB):</label>
                    <input type="number" placeholder="Size(MB)" name="size"/>
                    <input type="submit" value="Create" />
                </form>
            </td>
        </tr>
    </table>

    <hr/>
    <h2>Raspberry Pi Operations</h2>
    <table style="border: none">
            <tr style="border: none">
                <td style="border: none; vertical-align:top;">
                <form action="/pi/reboot" method="post" onsubmit="return confirm('Reboot Pi?')">
                    <input type="submit" value="Reboot Raspberry Pi" />
                </form>
            </td>
            <td style="border: none; vertical-align:top;">
                <form action="/pi/shutdown" method="post" onsubmit="return confirm('Shutdown Pi?')">
                    <input type="submit" value="Shut Down Raspberry Pi" />
                </form>
            </td>

            <td style="border: none; vertical-align:top;">
                <form action="/rascsi/restart" method="post" onsubmit="return confirm('Restart RaSCSI?')">
                    <input type="submit" value="Restart RaSCSI Service" />
                </form>
            </td>
        </tr>
    </table>
{% endblock %}

{% block footer %}
    <center><tt>{{version}}</tt></center>
    <center><a href="/logs">Logs</a></center>
{% endblock %}
