from django.shortcuts import render,redirect
from django.http import HttpResponse
from CrimeReportingSystem.forms import UserRegistrationForm,MyProfileForm,ChangepassForm,ComplaintForm
from CrimeReportingSystem.models import MyProfile

# Create your views here.
def home(request):
	return render(request,'html/home.html')

def aboutus(request):
	return render(request,'html/aboutus.html')

def contactus(r):
    return render(r,'html/contactus.html')

# def login(r):
    # return render(r,'html/login.html')
 
def register(r):
	if r.method == "POST":
		p=UserRegistrationForm(r.POST)
		if p.is_valid():
			p.save()
			return redirect('/login')

	p=UserRegistrationForm()
	return render(r,'html/register.html',{'u':p})

def profile(r):
	return render(r,'html/profile.html')

def dashboard(r):
	return render(r,'html/dashboard.html')

def changepass(request):
 	if request.method=="POST":
 		c=ChangepassForm(user=request.user,data=request.POST)
 		if c.is_valid():
 			c.save()
 			return redirect('/login')

 	c=ChangepassForm(user=request)
 	return render(request,'html/changepassword.html',{'t':c})

def complaint(req):
	if req.method=="POST":
		data=ComplaintForm(req.POST)
		if data.is_valid():
			subject='Confirmation_Complaint'
			body="thank you for complaint"+req.POST['p_name']
			receiver=req.POST['p_email']
			sender=settings.EMAIL_HOST_USER
			send_mail(subject,body,sender,[receiver])
			data.save()
			messages.success(req,"Successfully sent to your mail "+receiver)
			return redirect('/')
	form=ComplaintForm()
	return render(req,'html/complaint.html',{'c':form})


	
def crud(request):
	if request.method=="POST":
		c=UserRegistrationForm(request.POST)
		if c.is_valid():
			c.save()
			return render(request,'html/actions.html',{'o':c})
	c=UserRegistrationForm()
	return render(request,'html/actions.html',{'o':c})


def deletedata(req,id):
 	c=UserRegistration.objects.get(id=id)
 	c.delete()
 	return redirect('/crud')
