<html>
{% load i18n %}
{% load common_filters %}
{% load common_tags %}


{% comment %}
<!--
  Copyright (C) 2011 University of Dundee & Open Microscopy Environment.
  All rights reserved.

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
{% endcomment %}


<body>


<div id='activities_content'>
    <div id="sizeOfJobs" style="display:none">{{ sizeOfJobs }}</div>
    <div id="inprogress" style="display:none">{{ inprogress }}</div>
    <div id="new_results" style="display:none">{{ new_results }}</div>
    {% if new_errors %}<div id="new_errors" style="display:none">True</div>{% endif %}
    <div id="failure" style="display:none">{{ failure }}</div>

    <table id="jobsTable" width="100%" cellpadding="6" cellspacing="0" border="0">

        <tbody>
            {% ifequal sizeOfJobs 0 %}
                <tr><td><span>No Activities</span></td></tr>
            {% endifequal %}






            {% for j in jobs %}
                <!-- Chgrp jobs -->
                {% ifequal j.job_type "chgrp" %}
                    <tr id="{{ j.id }}" class="{% if j.new %}new_result{% endif %}{% ifequal j.status 'in progress' %} in_progress{% endifequal %}" >
                        <td width="25px">
                            {% ifequal j.status "in progress" %}<img class="icon" src="{% static "webgateway/img/spinner.gif" %}" />
                            {% else %}
                                {% if j.error %}
                                    <img alt="Failed to run script properly" src="{% static "webgateway/img/failed.png" %}" />
                                {% else %}
                                    <img class="icon" src="{% static "webgateway/img/success.png" %}" />
                                {% endif %}
                            {% endifequal %}
                        </td>
                        <td class="script_description" colspan='2'>
                            <span class="activity_title">
                                Move to Group '{{ j.group }}'
                            </span>
                            <span class="message">
                                {% ifequal j.status "in progress" %}
                                    Moving {{ j.dtype }}{{ j.obj_ids|pluralize }} to Group '{{ j.group }}'...
                                {% else %}
                                    {% ifequal j.status "finished" %}
                                        {{ j.dtype }}{{ j.obj_ids|pluralize }} moved to Group '{{ j.group }}'
                                        <a href="{% url 'webindex' %}?show={% for o in j.obj_ids %}{{ j.dtype|lower }}-{{ o }}{% if not forloop.last %}|{% endif %}{% endfor %}">
                                            Show {{ j.dtype }}{{ j.obj_ids|pluralize }}
                                        </a>
                                    {% endifequal %}
                                    {% if j.report %}
                                        <div class="chgrp_error">
                                            {{ j.report|truncateafter:"30" }}
                                            <a href="#" title="Click to Open in new Window"> More...</a>
                                            <div style="display:none">{{ j.report|linebreaksbr }}</div>
                                        </div>
                                    {% endif %}
                                {% endifequal %}
                            </span>
                        </td>
                    </tr>
                {% endifequal %}

                <!-- Chown jobs -->
                {% ifequal j.job_type "chown" %}
                    <tr id="{{ j.id }}" class="{% if j.new %}new_result{% endif %}{% ifequal j.status 'in progress' %} in_progress{% endifequal %}" >
                        <td width="25px">
                            {% ifequal j.status "in progress" %}<img class="icon" src="{% static "webgateway/img/spinner.gif" %}" />
                            {% else %}
                                {% if j.error %}
                                    <img alt="Chown failed" src="{% static "webgateway/img/failed.png" %}" />
                                {% else %}
                                    <img class="icon" src="{% static "webgateway/img/success.png" %}" />
                                {% endif %}
                            {% endifequal %}
                        </td>
                        <td class="script_description" colspan='2'>
                            <span class="activity_title">
                                Change Owner: '{{ j.owner }}'
                            </span>
                            <span class="message">
                                {% ifequal j.status "in progress" %}
                                    Moving {{ j.dtype }}{{ j.obj_ids|pluralize }} to Owner {{ j.owner }}...
                                {% else %}
                                    {% ifequal j.status "finished" %}
                                        {{ j.dtype }}{{ j.obj_ids|pluralize }} moved to Owner {{ j.owner }}.
                                        <a href="{% url 'webindex' %}?show={% for o in j.obj_ids %}{{ j.dtype|lower }}-{{ o }}{% if not forloop.last %}|{% endif %}{% endfor %}">
                                            Show {{ j.dtype }}{{ j.obj_ids|pluralize }}
                                        </a>
                                    {% endifequal %}
                                    {% if j.report %}
                                        <div class="chgrp_error">
                                            {{ j.report|truncateafter:"30" }}
                                            <a href="#" title="Click to Open in new Window"> More...</a>
                                            <div style="display:none">{{ j.report|linebreaksbr }}</div>
                                        </div>
                                    {% endif %}
                                {% endifequal %}
                            </span>
                        </td>
                    </tr>
                {% endifequal %}

                <!-- sending_email jobs -->
                {% ifequal j.job_type "send_email" %}
                    <tr id="{{ j.id }}" class="{% if j.new %}new_result{% endif %}{% ifequal j.status 'in progress' %} in_progress{% endifequal %}" >
                        <td width="25px">
                            {% ifequal j.status "in progress" %}<img class="icon" src="{% static "webgateway/img/spinner.gif" %}" />
                            {% else %}
                                {% if j.error %}
                                    <img alt="Failed to send email" src="{% static "webgateway/img/failed.png" %}" />
                                {% else %}
                                    <img class="icon" src="{% static "webgateway/img/success.png" %}" />
                                {% endif %}
                            {% endifequal %}
                        </td>
                        <td class="script_description" colspan='2'>
                            <span class="activity_title">
                                Send email
                            </span>
                            <span class="message">
                                {% ifequal j.status "in progress" %}
                                    Sending admin email...
                                {% else %}
                                    {% ifequal j.status "finished" %}
                                        Sent {{ j.rsp.success }} {% plural j.rsp.success 'email' 'emails' %} of {{ j.rsp.total }}
                                    {% endifequal %}
                                    {% if j.report %}
                                        <div class="chgrp_error">
                                            <a href="#" title="Click to Open in new Window"> More...</a>
                                            <div style="display:none">
                                                <p>{{ j.report.error }}</p>
                                                {% if j.report.invalidusers %}
                                                <p>Invalid users:</p>
                                                {% for u in j.report.invalidusers %}
                                                    User: {{ u }}</br>
                                                {% endfor %}
                                                {% endif %}
                                                {% if j.report.invalidemails %}
                                                <p>Invalid emails:</p>
                                                {% for u in j.report.invalidemails %}
                                                    Email: {{ u }}</br>
                                                {% endfor %}
                                                {% endif %}
                                            </div>
                                        </div>
                                    {% endif %}
                                {% endifequal %}
                            </span>
                        </td>
                    </tr>
                {% endifequal %}

                <!-- Delete jobs -->
                {% ifequal j.job_type "delete" %}
                    <tr id="{{ j.id }}" class="{% if j.new %}new_result{% endif %}{% ifequal j.status 'in progress' %} in_progress{% endifequal %}">
                        <td class="activities_state">
                            {% ifequal j.status "in progress" %}
                                <img alt="Deleting" src="{% static "webgateway/img/spinner.gif" %}" />
                            {% else %}
                                {% if j.error %}
                                    <div class='script_error' title="{{ j.error }}">
                                        <img src="{% static "webgateway/img/failed.png" %}" />
                                    </div>
                                {% else %}
                                    <img alt="Success" src="{% static "webgateway/img/success.png" %}" />
                                {% endif %}
                            {% endifequal %}
                        </td>
                        <td class="script_description" colspan="2">
                            {% if j.delmany %}
                                <span class="activity_title">
                                    Delete {{ j.delmany }} {{ j.dtype }}s
                                </span>
                                <span class="message">
                                {% for iid in j.did %}
                                    {% ifequal j.status "failed" %}
                                        {{ j.dtype }} {{ iid }},
                                    {% else %}
                                        {{ j.dtype }} {{ iid }},
                                    {% endifequal %}
                                {% endfor %}
                                </span>
                            {% else %}
                                <span class="activity_title">
                                    Delete {{ j.dtype }}
                                </span>
                                <span class="message">
                                {% ifequal j.status "failed" %}
                                    {{ j.dtype }} {{ j.did }}
                                {% else %}
                                    {{ j.dtype }} {{ j.did }}
                                {% endifequal %}
                                </span>
                            {% endif %}
                            <div class="results">
                                {% if j.dreport %}Error: ({{ j.dreport }}){% endif %}
                            </div>
                        </td>
                    </tr>
                {% endifequal %}




                <!-- Status -->
                {% ifequal j.job_type "script" %}
                    <tr id="{{ j.id }}" class="script{% if j.new %} new_result{% endif %}{% ifequal j.status 'in progress' %} in_progress{% endifequal %}">
						
						<!-- State -->
                        <td class="activities_state">
                            {% ifequal j.status "in progress" %}
								<img alt="Running Script" src="{% static "webgateway/img/spinner.gif" %}" />
                            
							{% else %} 
								{% if j.error %} 
                                <div class='script_error' title="{{ j.error }}">
                                    <img alt="Failed to run script properly" src="{% static "webgateway/img/failed.png" %}" />
								
									
                                    {% comment %}
                                    <!-- Don't allow submitting now. TODO: launch submit page -->
                                    <img src="{% static "webclient/image/info16.png" %}"/>
                                    <input type='submit' title="Send Error as Feeback to the OME team" jobKey="{{ j.key }}"
                                    {% if j.error_sent %}
                                        value='Thank you' disabled='true'
                                    {% else %}
                                        value='Submit Feedback'
                                    {% endif %} />
                                    {% endcomment %}
									
                                </div>
								{% else %}
								<img alt="Success" src="{% static "webgateway/img/success.png" %}" />
	                            {% endif %}
								
                            {% endifequal %}
                        </td>

                        <!-- General Script Info -->
                        <td class="script_description">

                            <!-- Show buttons for certain script results... -->
                            <ul class="menu_btns silver">

                                <!-- check all results for Images, File Annotations etc -->
                                {% for k,v in j.results.items %}
                                    {% if v.type and v.id %}
                                        <!-- Browser to File, Image, Dataset etc in tree -->
                                        {% if v.browse_url %}
                                            <li class="btn_folder">
                                                <a href='{{ v.browse_url }}' class='browse' title="Navigate to {{ v.type }} in the main window">
                                                    Go to {% ifequal v.type "FileAnnotation" %} Attachment {% else %} {{ v.type }} {% endifequal %}
                                                </a>
                                            </li>
                                        {% endif %}

                                        {% ifequal v.type "FileAnnotation" %}
                                            <!-- Download -->
                                            <li class="btn_download">
                                                <a href="{% url 'download_annotation' v.id %}" title="Download File">Download</a>
                                            </li>

                                            {% if v.fileId %}
                                                <!-- View File Type -->
                                                <li class="btn_filetype">
                                                    <a href="#" onClick="return OME.openPopup('{% url 'get_original_file' v.fileId %}');"
                                                        title="Show file in new window">View {{ v.fileType }}</a>
                                                </li>
                                            {% endif %}
                                        {% else %}

                                            {% ifequal v.type "Image" %}
                                                <!-- View Image -->
                                                <li class="btn_view">
                                                    <a href="#" onClick="return window.open('{% url 'web_image_viewer' v.id %}', '_blank');"
                                                    title="Open Image in Viewer">View Image</a>
                                                </li>
                                            {% endifequal %}
                                        {% endifequal %}
                                    {% elif v.type %}
                                        <!-- Show action button for URL link -->
                                        {% ifequal v.type "URL" %}
                                            <li class="btn_url">
                                                <a href='#' onClick="return window.open('{{ v.href }}', '{{ v.target|default:'_blank' }}');" title='{{ v.title }}'></a>
                                            </li>
                                        {% endifequal %}
                                    {% endif %}
                                {% endfor %}

                                <!-- show any stderr or stdout on the job itself -->
                                {% if j.stderr %}
                                    <!-- View error -->
                                    <li class="btn_view_error">
                                        <a href="#" onClick="return OME.openPopup('{% url 'get_original_file' j.stderr %}');"
                                        title="Show error produced by the script">Error</a>
                                    </li>
                                {% endif %}
                                {% if j.stdout %}
                                    <!-- More Information -->
                                    <li class="btn_info">
                                        <a href="#" onClick="return OME.openPopup('{% url 'get_original_file' j.stdout %}');"
                                        title="Show additional info generated by the script">Info</a>
                                    </li>
                                {% endif %}
                            </ul>

                            {% ifequal j.status 'no processor available' %}

                            <span class="activity_title">No Processor Available</span>
                            <span class="message">Please try again later.</span>

                            {% else %}
                            <span class="activity_title">{{ j.job_name }}</span>
                            <span class="message">{{ j.Message }}</span>
                            {% endifequal %}

                            <!-- Show any results not handled above -->
                            {% for k,v in j.results.items %}
                                <div>
                                    {% if v.type and v.id %}
                                        <!-- These should be handled above in buttons list -->
                                    {% elif v.type == "URL" %}
                                        <!-- These should be handled above in buttons list -->
                                    {% else %}
                                        <!-- Handle rtypes (already unwrapped) -->
                                        <span title="Script Result: '{{ k }}'">
                                            <strong>{{ k }}:</strong> {{ v }}
                                        </span>
                                    {% endif %}
                                </div>
                            {% endfor %}

                        <!-- </td> -->
						
						
						<!-- I need to put this back in -->
						<!--{{ j.start_time|date:"H:i" }}-->
						
						<!-- Out until there is a better way of displaying -->
						<!--<span class="status">{{ j.status }}</span>-->
						
						
						
						
						<!-- Result of script -->
                        <!-- <td class="results" style="position:relative"> -->
							
                        </td>
						
						
						
						
						<!-- Time -->
                    </tr>
                {% endifequal %}
            {% endfor %}
        </tbody>
    </table>

</div>

</body>
</html>
