
from django.http import HttpResponse, HttpResponseRedirect
#from django.template import loader
from django.shortcuts import render, redirect
#from django.utils import timezone
from django.urls import reverse
from django.contrib.auth import authenticate,login # import form
from django.contrib.auth.models import User,auth
from django.contrib import messages
from django.contrib.auth.decorators import login_required

# Create your views here.

# index 
def home(req):
    return render(req,'home.html')

def index(req):
    return render(req,'index.html')

def contact(req):
    return render(req,'contact.html')

def menu(req):
    return render(req,'menu.html')

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

def userlogin(request):
    username = request.POST['username']
    password = request.POST['password']
    user = auth.authenticate(request,username=username,password=password)
    if user is not None :
       auth.login(request, user)
       return HttpResponseRedirect(reverse('index'))
    else:
        return redirect('login')
        
def logout(request):
    auth.logout(request)
    return redirect('/')

# Register 
def member(request):
    return render(request,"account/member.html")


def register(request):
    if request.method == "POST":
        # POST 
        username = request.POST['username']
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        email = request.POST['email']
        password1 = request.POST['password1']
        password2 = request.POST['password2']

        if password1 == password2 :
            if User.objects.filter(username=username).exists():
                messages.info(request,"user ใช้ไปแล้ว")
                return redirect('todos:register')
            elif User.objects.filter(email=email).exists():
                messages.info(request,"Email ใช้ไปแล้ว")
                return redirect('todos:register')
            else:
                myuser = User.objects.create_user(username=username,first_name=first_name,
                last_name=last_name,email=email,password=password1)
                myuser.save()
            messages.success(request,'Your Accout has been sucessfuly created.')


            return redirect('member')
        else:
            messages.success(request,'Password ไม่ถุกต้อง')
            return render(request,'account/register.html')

    return render(request,'account/register.html')


# help 
def help(request):
    return render(request,'help.html')


## Admin 
def admin1(request):
    return render(request,'admin/Ad_login.html')


def Adminlogin(request:HttpResponse):
    username = request.POST['username']
    password = request.POST['password']
    user = auth.authenticate(request,username=username,password=password)
    if user is not None :
       auth.login(request, user)
       return HttpResponseRedirect(reverse('myadmin'))
    else:
        return redirect('admin1')
        
def adlogout(request):
    auth.logout(request)
    return redirect('/')

@login_required
def myadmin(request: HttpResponse):
    return render(request,'dashboard/dashboard.html')