| {% extends 'layout.html' %}
| {% from '_macros/_navigation.html' import navigation_tabs %}
include ../../../../pillar/src/templates/mixins/components

| {% set title = 'dashboard' %}

| {% block og %}
meta(property="og:title", content="Dashboard")
meta(name="twitter:title", content="Blender Cloud")

meta(property="og:url", content="https://cloud.blender.org/{{ request.path }}")
meta(property="og:type", content="website")

meta(property="og:image", content="{{ url_for('static', filename='assets/img/backgrounds/cloud_services_oti.jpg')}}")
meta(name="twitter:image", content="{{ url_for('static', filename='assets/img/backgrounds/cloud_services_oti.jpg')}}")
| {% endblock %}

| {% block page_title %}
| {{current_user.full_name}}
| {% endblock %}

| {% block css %}
| {{ super() }}
style.
	.deleted-projects-toggle {
		z-index: 10;
		position: absolute;
		right: 0;
		font-size: 20px;
		padding: 3px;
		text-shadow: 0 0 2px white;
	}
	.deleted-projects-toggle .show-deleted {
		color: #aaa;
	}
	.deleted-projects-toggle .hide-deleted {
		color: #bbb;
	}
| {% endblock %}

| {% block navigation_tabs %}
| {{ navigation_tabs(title) }}
| {% endblock navigation_tabs %}


| {% block body %}
.dashboard-container
	section.dashboard-main
		section#projects.bg-white
			+nav-secondary()(id='sub-nav-tabs__list')
				+nav-secondary-link(data-tab-toggle='own_projects', class="active")
					span Own Projects
					| {% if projects_user|length != 0 %}
					.text-muted ({{ projects_user|length }})
					| {% endif %}

				+nav-secondary-link(data-tab-toggle='shared')
					span Shared with me
					| {% if projects_shared|length != 0 %}
					.text-muted ({{ projects_shared|length }})
					| {% endif %}

				| {% if current_user.has_cap('subscriber') %}
				+nav-secondary-link(
						id="project-create",
						data-url="{{ url_for('projects.create') }}",
						href="{{ url_for('projects.create') }}")
						i.pi-plus
						span Create New Project
				| {% elif current_user.has_cap('can-renew-subscription') %}
				+nav-secondary-link(
						id="project-create",
						data-url="{{ url_for('projects.create') }}",
						href="/renew",
						target="_blank")
						| #[i.pi-heart-filled.text-danger] Resubscribe to Create a Project
				| {% endif %}

			nav.nav-tabs__tab.active#own_projects
				.deleted-projects-toggle
					| {% if show_deleted_projects %}
					a.hide-deleted(href="{{ request.base_url }}", title='Hide deleted projects')
						i.pi-trash
					| {% else %}
					a.show-deleted(href="{{ request.base_url }}?deleted=1", title='Show deleted projects')
						i.pi-trash
					| {% endif %}

				ul.projects__list
					| {% for project in projects_deleted %}
					li.projects__list-item.deleted
						span.projects__list-thumbnail
							| {% if project.picture_square %}
							img(src="{{ project.picture_square.thumbnail('s', api=api) }}")
							| {% else %}
							i.pi-blender-cloud
							| {% endif %}
						.projects__list-details
							span.title {{ project.name }}
							ul.meta
								li.status.deleted Deleted
								li.edit
									a(href="javascript:undelete_project('{{ project._id }}')") Restore project
					| {% else %}
					| {% if show_deleted_projects %}
					li.projects__list-item.deleted You have no recenly deleted projects. Deleted projects can be restored within a month after deletion.
					| {% endif %}
					| {% endfor %}

					| {% for project in projects_user %}
					li.projects__list-item(
						data-url="{{ url_for('projects.view', project_url=project.url) }}")
						a.projects__list-thumbnail(
							href="{{ url_for('projects.view', project_url=project.url) }}")
							| {% if project.picture_square %}
							img(src="{{ project.picture_square.thumbnail('s', api=api) }}")
							| {% else %}
							i.pi-blender-cloud
							| {% endif %}
						.projects__list-details
							a.title(href="{{ url_for('projects.view', project_url=project.url) }}")
								| {{ project.name }}

							ul.meta
								li.status(
									class="{{ project.is_private | yesno('private,public,') }}",
									title="{{ project.is_private | yesno('Private Project,Public Project,') }}")
										| {{ project.is_private | yesno('Private,Public,') }}
								li.when(title="{{ project._created }}") {{ project._created | pretty_date }}
								li.edit
									a(href="{{ url_for('projects.edit', project_url=project.url) }}") Edit
								| {% if project.status == 'pending' and current_user.has_cap('view-pending-nodes') %}
								li.pending Not Published
								| {% endif %}
					| {% else %}
					| {% if current_user.has_cap('subscriber') %}
					li.projects__list-item(data-url="{{ url_for('projects.create') }}")
						a.projects__list-thumbnail
							i.pi-plus
						.projects__list-details
							a.title(href="{{ url_for('projects.create') }}")
								| Create a project to get started!
					| {% elif current_user.has_cap('can-renew-subscription') %}
					li.projects__list-item(data-url="https://store.blender.org/renew-my-subscription.php")
						a.projects__list-thumbnail
							i.pi-plus
						.projects__list-details
							a.title(href="https://store.blender.org/renew-my-subscription.php")
								| Renew your Blender Cloud subscription to create your own projects!
					| {% else %}
					li.projects__list-item(data-url="/join")
						a.projects__list-thumbnail
							i.pi-plus
						.projects__list-details
							a.title(href="/join")
								| Join Blender Cloud to create your own projects!
					| {% endif %}
					| {% endfor %}

			section.nav-tabs__tab#shared(style='display: none')
				ul.projects__list
					| {% if projects_shared %}
					| {% for project in projects_shared %}
					li.projects__list-item(
						data-url="{{ url_for('projects.view', project_url=project.url) }}")
						a.projects__list-thumbnail(
							href="{{ url_for('projects.view', project_url=project.url) }}")
							| {% if project.picture_square %}
							img(src="{{ project.picture_square.thumbnail('s', api=api) }}")
							| {% else %}
							i.pi-blender-cloud
							| {% endif %}
						.projects__list-details
							a.title(href="{{ url_for('projects.view', project_url=project.url) }}")
								| {{ project.name }}

							ul.meta
								li.status(
									class="{{ project.is_private | yesno('private,public,') }}",
									title="{{ project.is_private | yesno('Private Project,Public Project,') }}")
										| {{ project.is_private | yesno('Private,Public,') }}
								li.when {{ project._created | pretty_date }}
								li.who by {{ project.user.full_name }}
								li.edit
									a(href="{{ url_for('projects.edit', project_url=project.url) }}") Edit
								| {% if project.status == 'pending' and current_user.has_cap('view-pending-nodes') %}
								li.pending Not Published
								| {% endif %}

								li.leave
									span.user-remove-prompt
										| Leave Project

									span.user-remove
										| Are you sure?
										span.user-remove-confirm(
											user-id="{{ current_user.objectid }}",
											project-url="{{url_for('projects.sharing', project_url=project.url)}}")
											i.pi-check
											| Yes, leave
										span.user-remove-cancel
											i.pi-cancel
											| No, cancel

					| {% endfor %}
					| {% else %}
					li.projects__list-item
						a.projects__list-thumbnail
							i.pi-heart-broken
						.projects__list-details
							.title
								| No projects shared with you... yet!
					| {% endif %}
| {% endblock %}


| {% block footer_scripts %}
script.
	$(document).ready(function() {

		$('li.projects__list-item').click(function(e){
			url = $(this).data('url');
			if (typeof url === 'undefined') return;

			window.location.href = url;
			if (console) console.log(url);

			$(this).addClass('active');
			$(this).find('.projects__list-thumbnail i')
				.removeAttr('class')
				.addClass('pi-spin spin');
		});

		// Tabs behavior
		var $nav_tabs_list = $('#sub-nav-tabs__list');
		var $nav_tabs = $nav_tabs_list.find('a.nav-link');
		$nav_tabs.on('click', function(e){
			e.preventDefault();

			$nav_tabs.removeClass('active');
			$(this).addClass('active');

			$('.nav-tabs__tab').hide();
			$('#' + $(this).attr('data-tab-toggle')).show();
		});

		// Leave project
		var $projects_list = $('ul.projects__list');
		$projects_list.find('span.user-remove-prompt').on('click', function(e){
			e.stopPropagation();
			e.preventDefault();

			$(this).next().show();
			$(this).hide();
		});

		$projects_list.find('span.user-remove-cancel').on('click', function(e){
			e.stopPropagation();
			e.preventDefault();

			$(this).parent().prev().show();
			$(this).parent().hide();
		});

		$projects_list.find('span.user-remove-confirm').on('click', function(e){
			e.stopPropagation();
			e.preventDefault();
			var parent = $(this).closest('.projects__list-item');

			function removeUser(userId, projectUrl){
				$.post(projectUrl, {user_id: userId, action: 'remove'})
					.done(function (data) {
						parent.remove();
					});
			}

			removeUser($(this).attr('user-id'), $(this).attr('project-url'));
		});

		hopToTop(); // Display jump to top button
	});


	var patch_url = '{{ url_for('projects.patch.patch_project', project_id='PROJECTID') }}';
	function undelete_project(project_id) {
		console.log('undeleting project', project_id);
		$.ajax({
			url: patch_url.replace('PROJECTID', project_id),
			method: 'PATCH',
			data: JSON.stringify({'op': 'undelete'}),
			contentType: 'application/json'
		})
		.done(function(data, textStatus, jqXHR) {
			location.href = jqXHR.getResponseHeader('Location');
		})
		.fail(function(err) {
			toastr.error(xhrErrorResponseMessage(err), 'Undeletion failed');
		})
	}
| {% endblock %}
