from django.shortcuts import render
from django.template import loader

from django.urls import reverse
from django.utils import timezone
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required

from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth.models import User

from core.views import gen_page_base, gen_page_sys, get_get_value, get_data_value, update_data_value
from .models import *
from .api import link_user_to_app

@login_required(login_url='core_login')
def index(request):
	template = loader.get_template('gest_user_index.html')
	page = gen_page_base()

	get_search = request.GET.get('get_search', '')
	get_id = request.GET.get('get_id', '')
	page.lastapta = get_data_value('dernier-apta')
	
	
	if get_search :
		print(get_search)
		page.search = get_search
	if get_id :
		print(get_id)
		page.item_id = get_id
	
	page.p_adresse = reverse('parc_info_taches_show_list')
	page.p_titre = "Listing des utilisateurs"
	page.p_contenu = "Listing des utilisateurs"
	page.p_right = "Les Outils :"
	
	html = template.render({
			'page': page,
			'user': request.user,
		}, request)
		
	return HttpResponse(html)

def user_get(request, id):
	template = loader.get_template('page.html')
	page = gen_page_base()
	page.p_adresse = reverse('parc_info_taches_show_list')
	page.p_titre = "Détail d'un utilisateur"
	page.p_contenu = "Détail d'un utilisateur"
	page.p_right = ""
	page.p_include = "gest_user_api_get.html"
	
	item = GES_User.objects.get(id = id)
	link_user_to_app(item.id, 'update')

	item.apps = GES_APPLink.objects.filter(GES_APPLink_User = item.id)
	
	html = template.render({
			'page': page,
			'item':item,
			'user': request.user,
		}, request)
		
	return HttpResponse(html)

def user_edit(request, id):

	get_search = request.GET.get('get_search', '')

	template = loader.get_template('base.html')
	page = gen_page_base()

	page.p_adresse = reverse('gest_user_index')
	page.p_titre = "Modification d'un utilisateur"
	page.p_contenu = "Modification d'un utilisateur"
	page.p_right = "&nbsp"
	page.p_include = "gest_user_api_edit.html"
	if id == None or id == '0':
		item = GES_User()
	else :
		item = GES_User.objects.get(id = id)
	print(item)
	if request.method == 'POST':
		form = GES_User_form(request.POST, instance=item)
		form.url = "gest_user_edit"
		if form.is_valid():
			form.save()
			get_value = "?get_id=" + str(item.id)
			if get_search:
				get_value = get_value + '&get_search=' + get_search
			return HttpResponseRedirect(reverse('gest_user_index')+get_value)
	else:
		form = GES_User_form(instance=item)
		form.url = "gest_user_edit"

	html = template.render({
			'form': form,
			'page': page,
			'item': item,
		}, request)
		
	return HttpResponse(html)

def app_edit(request, id):

	get_search = request.GET.get('get_search', '')

	template = loader.get_template('base.html')
	page = gen_page_base()

	page.p_adresse = reverse('gest_user_index')
	page.p_titre = "Modification d'un utilisateur"
	page.p_contenu = "Modification d'un utilisateur"
	page.p_right = "&nbsp"
	page.p_include = "gest_user_api_edit.html"
	if id == None or id == '0':
		item = GES_APPLink()
	else :
		item = GES_APPLink.objects.get(id = id)
	if request.method == 'POST':
		form = GES_APPLink_form(request.POST, instance=item)
		form.url = "gest_app_edit"
		if form.is_valid():
			form.save()
			if item.GES_APPLink_APP.GESAP_Nom == "Apta":
				print("hoo un apta")
				item = GES_APPLink.objects.get(id = id)
				data = get_data_value("dernier-apta")
				if item.GES_APPLink_Sup > data :
					print ("need update compteur")
					data = update_data_value("dernier-apta", item.GES_APPLink_Sup)

			get_value = "?get_id=" + str(item.GES_APPLink_User.id)
			if get_search:
				get_value = get_value + '&get_search=' + get_search
			return HttpResponseRedirect(reverse('gest_user_index')+get_value)
	else:
		form = GES_APPLink_form(instance=item)
		form.url = "gest_app_edit"

	html = template.render({
			'form': form,
			'page': page,
			'item': item,
		}, request)
		
	return HttpResponse(html)