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


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

var content_filter_url = "{% url ldt.ldt_utils.views.contentsfilter filter='__FILTER__' %}";
var project_filter_url = "{% url ldt.ldt_utils.views.projectsfilter 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 %}";


function init_events(base_node) {

	init_events_all(base_node, "{% url ldt.ldt_utils.views.popup_embed %}", content_filter_url, project_filter_url, publish_project_url, unpublish_project_url, get_group_projects_url);
	
}

	
var global_csrf_token = "{{ csrf_token }}";

$(document).ready(function(){
	
	input_list_init = [
		{'input_selector':"#searchcontentsinput", 'container_selector':"#contentslistcontainer", 'url':content_filter_url},
		{'input_selector':"#searchprojectsinput", 'container_selector':"#projectslistcontainer", 'url':project_filter_url}
	];
	
	searchFieldInit(input_list_init);
	
	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">
	{% if groups and groups.all|length > 0 %}
	<div class="span-12" id="contentsdiv">
	    <div class="span-12 last titlediv" >
			<div class="span-8 projectscontentstitle">{% trans "My groups" %}</div>
		</div>
		<div class="span-12 last projectscontentsdiv" id="contentslistcontainer">
		<div class="projectscontentsheader groupsheader 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 }}">
			    <td>{{ group.name }}</td><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 class="span-4 last searchfielddiv" id="search_div" style='display:none'>
			    <div class="searchfield rounded"><input id="searchprojectsinput" class="searchfieldinput searchfieldinputbase" value="{% trans 'search' %}" type="text" /><img id="projectsajaxloader" class="searchajaxloader" src="{{LDT_MEDIA_PREFIX}}/img/ajax-loader-16x16.gif" alt="loader"/><img id="projecsclear" class="searchclear" src="{{LDT_MEDIA_PREFIX}}img/clear-left.png"/></div>
			</div>
		</div>
		<div class="span-12 last projectscontentsdiv" id="projectslistcontainer">
		{% include "ldt/ldt_utils/partial/projectslist.html" %}
		</div>
	</div>
	{% else %}
	<p>{% trans "You do not belong to any group." %}</p>
    {% endif %}
</div>
{% endblock %}

