from django.shortcuts import render
from users.models import User
from django import forms
from users.forms import ContactForm , RegisterForm
from django.contrib import messages 
from django.contrib.auth import authenticate, login as auth_login , logout as auth_logout
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect

def index(request):
	if not request.user.is_authenticated():
		if request.method == 'POST':
			form = ContactForm(request.POST)
			if form.is_valid():
				cd = form.cleaned_data
			else:
				messages.error(request, "Error")	
		
		else:
			form = ContactForm(label_suffix='')
		return render(request,'users/index.html',{'form': form,'username':request.user})
	else:
		return HttpResponseRedirect("/hotels/")
		
		
def login(request):
	name = request.POST['e_mail']
	passs = request.POST['password']
	user = authenticate(username=name, password=passs)
	if user is not None:
		if user.is_active:
			auth_login(request,user) 
			return HttpResponseRedirect("/hotels/")    
		else:
			return render(request,'users/index.html',{'form': "disabled ",'username':request.user})
	else:
		return render(request,'users/index.html',{'form': name,'username':request.user})
	return render(request,'users/index.html',{'form': name,'username':request.user})
	
	
def logout(request):
	auth_logout(request)
	return render(request,'users/index.html',{'form': "logged out",'username':request.user})
	
def register(request):
	if request.method == 'POST':
			form = RegisterForm(request.POST)
			if form.is_valid():
				cd = form.cleaned_data
				name = request.POST['user']
				passs = request.POST['password']
				email = request.POST['e_mail']
				try:
					u = User.objects.get(username=name)
					return render(request,'users/register.html',{'form': "sorry this user is already exist",'username':request.user})
				except:
					user = User.objects.create_user(email, name , passs)
					user.save()
					return render(request,'users/register.html',{'form': "added successfully",'username':request.user})
			else:
				messages.error(request, "Error")	
				return render(request,'feedbacks/add.html',{'form': form,'message':" !",'username':request.user})
	else:
			form = RegisterForm(label_suffix='')
	return render(request,'users/register.html',{'form': form,'username':request.user})
	
		
