{% extends 'base_simpleui.html' %}

{% block title %}index{% endblock %}

{% block head %}
    <style>
    html, body {
        height: 100%;
    }

    html {
        display: table;
        margin: auto;
    }

    body {
        vertical-align: middle;
    }
    </style>
{% endblock %}

{% block link %}{% endblock %}


{% block body %}
<p>browser >>> scrapydweb@{{ scrapydweb_url }} >>>
    <a target="_blank" href="http://{{ SCRAPYD_SERVERS[node-1] }}">scrapyd@http://{{ SCRAPYD_SERVERS[node-1] }}</a>
</p>

{% if not HIDE_SCRAPYD_ITEMS %}
<h2>
    <a class="button" style="width: 125px; height: 21px;"
    href="{{ url_for('items.items', node=node, ui=ui) }}" >
    Items directory
    </a>
</h2>
{% endif %}

<h2>
    <a class="button" style="width: 125px; height: 21px;"
    href="{{ url_for('logs.logs', node=node, ui=ui) }}" >
    Logs directory
    </a>
</h2>

<form action="{{ url_for('parse.upload', node=node, ui=ui) }}" method="post" enctype="multipart/form-data">
    <label class="fileContainer">
        <input id="file" type="file" name="file" accept=".log, .txt" />
        Select
    </label>

    <input id="uploadFile" placeholder="a log or txt file" disabled="disabled" style="margin-left: 5px; color: black;" />
    <input type="button" value="Upload and Parse" onclick="uploadLogfile();" />
    <a href="{{ url_for('parse.uploaded', node=node, filename='demo.txt', ui=ui) }}">demo</a>
</form>

<br>

<table border="1">
    <tr>
        <th>Project</th>
        <th>Spider</th>
        {% if SHOW_DASHBOARD_JOB_COLUMN %}
        <th>Job</th>
        {% endif %}

        <th>PID</th>
        <th>Start</th>
        <th>Runtime</th>
        <th>UTF8</th>
        <th>Stats</th>
        {% if (not HIDE_SCRAPYD_ITEMS) and running_rows and running_rows[0]['url_items'] %}
        <th>Items</th>
        {% endif %}

        <th>Finish</th>
        <!-- <th>Log</th> -->
        <th>Action</th>
        <th>FORCESTOP</th>
    </tr>


    <tr>
        <th colspan="{{ colspan }}" style="background-color: #ddd;">Pending</th>
    </tr>
{% for row in pending_rows %}
    <tr>
        <td>{{ row['project'] }}</td>
        <td>{{ row['spider'] }}</td>
    </tr>
{% endfor %}


    <tr>
        <th colspan="{{ colspan }}" style="background-color: #ddd;">Running</th>
    </tr>
{% for row in running_rows %}
    <tr>
        <td>{{ row['project'] }}</td>
        <td>{{ row['spider'] }}</td>
        {% if SHOW_DASHBOARD_JOB_COLUMN %}
        <td>{{ row['job'] }}</td>
        {% endif %}

        <td>{{ row['pid'] }}</td>
        <td>{{ row['start'] }}</td>
        <td>{{ row['runtime'] }}</td>
        <td>
            <a href="{{ url_for('log.log', node=node, opt='utf8', project=row['project'], spider=row['spider'], job=row['job'], ui=ui) }}"
            onclick="showLoader();">
            UTF8
            </a>
        </td>
        <td>
            <a href="{{ url_for('log.log', node=node, opt='stats', project=row['project'], spider=row['spider'], job=row['job'], ui=ui) }}"
            onclick="showLoader();">
            Stats
            </a>
        </td>

    {% if (not HIDE_SCRAPYD_ITEMS) and row['url_items'] %}
        <td>
            <a href="{{ row['url_items'] }}"
            target="_blank">
            Items
            </a>
        </td>
    {% endif %}

        <td>{{ row['finish'] }}</td>
        <td>
            <a id="{{ row['job'] }}" class="button" style="background: orange;"
            onclick="myFunction('{{ row['job'] }}')"
            href="{{ url_for('api.api', node=node, opt='stop', project=row['project'], version_spider_job=row['job']) }}">
            STOP
            </a>
        </td>
        <td>
            <a id="{{ row['job'] }}force" class="button" style="background: red;"
            onclick="myFunction('{{ row['job'] }}force')"
            href="{{ url_for('api.api', node=node, opt='forcestop', project=row['project'], version_spider_job=row['job']) }}">
            FORCESTOP
            </a>
        </td>
    </tr>
{% endfor %}


    <tr>
        <th colspan="{{ colspan }}" style="background-color: #ddd;">Finished</th>
    </tr>
{% for row in finished_rows %}
    <tr>
        <td>{{ row['project'] }}</td>
        <td>{{ row['spider'] }}</td>
        {% if SHOW_DASHBOARD_JOB_COLUMN %}
        <td>{{ row['job'] }}</td>
        {% endif %}

        <td>{{ row['pid'] }}</td>
        <td>{{ row['start'] }}</td>
        <td>{{ row['runtime'] }}</td>
        <td>
            <a href="{{ url_for('log.log', node=node, opt='utf8', project=row['project'], spider=row['spider'], job=row['job'], ui=ui) }}"
            onclick="showLoader();">
            UTF8
            </a>
        </td>
        <td>
            <a href="{{ url_for('log.log', node=node, opt='stats', project=row['project'], spider=row['spider'], job=row['job'], ui=ui) }}"
            onclick="showLoader();">
            Stats
            </a>
        </td>

    {% if (not HIDE_SCRAPYD_ITEMS) and row['url_items'] %}
        <td>
            <a href="{{ row['url_items'] }}"
            target="_blank">
            Items
            </a>
        </td>
    {% endif %}

        <td>{{ row['finish'] }}</td>
        <td>
            <a id="{{ row['job'] }}" class="button" style="background: #8BC34A;"
            onclick="myFunction('{{ row['job'] }}')"
            href="{{ url_for('api.api', node=node, opt='start', project=row['project'], version_spider_job=row['spider']) }}">
            START
            </a>
        </td>
    </tr>
{% endfor %}
</table>


<script>
my$('#file').onchange = function () {
    my$('#uploadFile').value = this.value;
};

function myFunction(id) {
    my$('#'+id).style['background-color']='#008CBA';
    showLoader();
}
</script>
{% endblock %}
