{% extends "ldt/ldt_utils/workspace.html" %}
{% load i18n %}
{% load guardian_tags %}

{% block js_declaration %}
{{block.super}}
<script type="text/javascript">

var content_filter_url = "{% url ldt.ldt_utils.views.contents_filter filter='__FILTER__' %}";
var project_filter_url = "{% url ldt.ldt_utils.views.projects_filter filter='__FILTER__' is_owner='false' status='0' id_group='__ID_GROUP__' %}";
var publish_project_url = "{% url ldt.ldt_utils.views.publish '__PROJECT_ID__' 'false' %}";
var unpublish_project_url = "{% url ldt.ldt_utils.views.unpublish '__PROJECT_ID__' 'false' %}";
var get_group_projects_url = "{% url ldt.ldt_utils.views.get_group_projects %}";

	
var global_csrf_token = "{{ csrf_token }}";

$(document).ready(function(){
	
	init_events(document);
	
    $(".update_group_projects").click(function(){
        //alert("group id = " + $(this).attr('id') + ", url = " + get_group_projects_url);
        id_group = $(this).attr('id');
        // Remove the icons from all the lines
        $(".next_icon").hide();
        // Display the icon from the good line
        $(".next_icon",this).show();
        // Show the search textfield and update the id_group in the hidden input
        $('#search_div').show();
        $('#id_group').val(id_group);
        $('#searchprojectsinput').val("");
        $(".searchclear",$('#search_div')).hide();
    	// Send the request to update the projects list
        $.ajax({
            url: get_group_projects_url,
            type: 'POST',
            data: {csrfmiddlewaretoken:global_csrf_token,
                   id_group:id_group
                   },
            success: function(msg, textStatus, XMLHttpRequest) {
                $('#projectslistcontainer').html(msg);
                init_events(document);
            },
    		error: function(jqXHR, textStatus, errorThrown) {
    			resp = $.parseJSON(jqXHR.responseText);
    			alert(resp.message);
    		}
        });
    });

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

{% block content %}
<div class="span-24 last" id="allcontentsdiv">
	<div class="span-12" id="contentsdiv">
	    <div class="span-12 last titlediv" >
			<div class="span-8 projectscontentstitle">{% trans "My groups" %}</div>
			<div class="span-4 last searchfielddiv" >
				<a class="create_group" href="{% url ldt.ldt_utils.views.create_group %}"><img class='icon_title' src="{{LDT_MEDIA_PREFIX}}img/film_add.png" alt="{% trans 'Create content' %}" title="{% trans 'Create group' %}" /></a>
			</div>
		</div>
		<div class="span-12 last projectscontentsdiv" id="contentslistcontainer">
		<div class="projectscontentsheader projectcontentsheadertitle span-12 last" id="contentsheader">
		{% trans "Click on the line to see the group's projects" %}
		</div>
		<input type='hidden' name='id_group' id='id_group' value='' />
		<table class="projectscontentstable">
	    <tbody class="projectscontentsbody">
		{% for group in groups.all %}
			<tr class="imageline {% cycle 'projectscontentsoddline' 'projectscontentsevenline'%} update_group_projects" id="{{ group.id }}">
			{% get_obj_perms request.user for group as "group_perms" %}
			
			{% if "change_group" in group_perms %}
			    <td class="cellimg"><a class="create_group" title="Change this group" href="{% url ldt.ldt_utils.views.update_group group_id=group.id%}">c</a></td>
			   <td><b>{{ group.name }}</b></td>
			{% else %}
			    <td class="cellimg"></td><td>{{ group.name }}</td>
			{% endif %}
			<td class="align_right"><img src="{{LDT_MEDIA_PREFIX}}/img/control_play.png" class="next_icon"/></td>
			
			</tr>
		{% endfor %}
		</tbody>
		</table>
		</div>
	</div>
	<div class="span-12 last" id="projectsdiv">
		<div class="span-12 last titlediv" >
			<div class="span-8 projectscontentstitle" >{% trans "The group's projects" %}</div>
		</div>
		<div class="span-12 last projectscontentsdiv" id="projectslistcontainer">
		{% include "ldt/ldt_utils/partial/projectslist.html" %}
		</div>
	</div>
</div>
{% endblock %}

