{% extends 'server_control_panel/base.html' %}

{% load i18n static localcosmos_tags %}

{% block content %}
	<div class="container">

		<h2>{% trans 'Server info' %}</h2>

		<div class="row mb-4">
			<div class="col-12">
				Server version: {{ server_version }}
			</div>
			<div class="col-12">
				<div class="card">
					<div class="card-body">
						<strong>API URL: <a href="{{ request.scheme }}://{{ request.get_host }}{% url 'api_home' %}">{{ request.scheme }}://{{ request.get_host }}{% url 'api_home' %}</a></strong>
						<div class="alert alert-danger mt-2">{% trans 'Use this url in the app kit on localcosmos.org to connect your app to this server.' %}</div>
					</div>
				</div>
			</div>
		</div>

		<h2>{% trans 'Installed apps' %}</h2>

		<div class="row">
			{% if apps %}
				{% for app in apps %}
					<div id="ctype-{{ app|ctype_id }}-{{ app.id }}" class="col-12 col-md-6 col-lg-4">
						<div class="card">
							<div class="card-header">
								<small>{% trans 'App' %}</small>
								{% if localcosmos_private %}
									<div class="dropdown float-right d-inline-block">
										<button class="btn btn-xs btn-outline-secondary dropdown-toggle" type="button"  data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button>
										<div class="dropdown-menu">
											<a class="dropdown-item" href="{% url 'scp:edit_app' app.uid %}">{% trans 'Edit' %}</a>
											<a class="dropdown-item" href="{% url 'scp:update_app' app.uid %}">{% trans 'Update' %}</a>
											<a class="dropdown-item xhr" data-url="{% url 'scp:uninstall_app' app.id %}" ajax-target="ModalContent">{% trans 'Uninstall' %}</a>
										</div>
									</div>
								{% endif %}
							</div>
							<div class="card-body">
								<h5 class="card-title">{{ app }}</h5>
								
								<div class="mb-2">
									<small><strong>UID: {{ app.uid }}</strong></small>
								</div>

								{% if app.published_version %}
									<span class="badge badge-info">{% trans 'version' %}: {{ app.published_version }}</span>
									<p class="card-text">
										<div>
											<a href="{% if localcosmos_private %}{% else %}{{ request.scheme }}://{% endif %}{{ app.get_admin_url }}" class="btn btn-sm btn-outline-primary">{% trans 'App admin' %}</a> 
										</div>
										<hr>
										<div>
											{% trans 'Web app' %}:
											{% if app.get_url %}
												{% if localcosmos_private %}
													<a href="{{ app.get_url }}" target="_blank">{{ app.get_url }}</a>
												{% else %}
													<a href="{{ request.scheme }}://{{ app.get_url }}" target="_blank">{{ request.scheme }}://{{ app.get_url }}</a>
												{% endif %}
											{% else %}
												<span class="badge badge-danger">{% trans 'not available' %}</span>
											{% endif %}
										</div>
										<hr>
										<div>
											{% trans 'API status' %}: <span class="api-status" data-api-status-url="{% url 'scp:app_api_status' app.uid %}"></span>
										</div>
										{% if localcosmos_private %}
										{% else %}
											<hr>
											<div>
												{% trans 'Android .apk' %}:
												{% if app.apk_url %}
													<a href="{{ request.scheme }}://{{ request.get_host }}{{ app.apk_url }}" download>{% trans 'Download' %}</a>
												{% else %}
													<span class="badge badge-danger">{% trans 'not available' %}</span>
												{% endif %}
											</div>
											<div>
												{% trans 'iOS .ipa' %}:
												{% if app.ipa_url %}
													<a href="{{ request.scheme }}://{{ request.get_host }}{{ app.ipa_url }}" download>{% trans 'Download' %}</a>
												{% else %}
													<span class="badge badge-danger">{% trans 'not available' %}</span>
												{% endif %}
											</div>
										{% endif %}
									</p>
								{% else %}
									<span class="badge badge-info">{% trans 'draft' %}</span>
									<p class="card-text mt-2">
										<small>{% trans 'More information will be avaibale when this app has been published.' %}</small>
									</p>
								{% endif %}
							</div>
						</div>
					</div>
				{% endfor %}
			{% else %}
				<div class="col-12">
					{% trans 'You have not installed any apps yet.' %}
				</div>
			{% endif %}
		</div>
		<hr>
		<div class="row mt-3">
			<div class="col-12">
				<a href="{% url 'scp:install_app' %}" class="btn btn-outline-primary">{% trans 'Install new app' %}</a>
			</div>
		</div>
	</div>
{% endblock %}

{% block extra_script %}
	<script>
		var apps = document.getElementsByClassName("api-status");

		each(apps, function(app, iterate){
			var url = app.getAttribute("data-api-status-url");
			$.get(url, function(html){
				$(app).html(html);
			});

			iterate();
		});
	</script>
{% endblock %}
