
from django.shortcuts import render
from web_admin.models import Contact, NewsEvent,  PhotoGallery, Album
from django.db.models import Max



def home(request):
    template = 'pages/website/home.html'
    context = {
        "home": "active",
        "page_title": "BASETE RURAL DEVELOPMENT WELFARE TRUST | Home",
        "news": NewsEvent.objects.filter(is_deleted=False).order_by('-event_date')[:4],
        "latest_photo": PhotoGallery.objects.filter(is_deleted=False).order_by('-created_at')[:8]
    }
    return render(request, template, context)


def about(request):
    template = 'pages/website/about.html'
    context = {
        "about": "active",
        "page_title": "BASETE RURAL DEVELOPMENT WELFARE TRUST | About Us"
    }
    return render(request, template, context)

def contact(request):
    template = 'pages/website/contact_us.html'
    context = {
        "contact": "active",
        "page_title": "BASETE RURAL DEVELOPMENT WELFARE TRUST | Contact Us",
        'contact_details': Contact.objects.filter(is_deleted=False)
    }
    return render(request, template, context)


def photoGallery(request):
    page_title = "BASETE RURAL DEVELOPMENT WELFARE TRUST | Gallery"
    context = {
        "gallery": "active",
        "page_title": page_title,
        "photo_gallery": "active",
        "photo": False
        }
    latest_album_ids = PhotoGallery.objects.filter(is_deleted=False).values('album_id').annotate(latest_id=Max('id')).order_by('-latest_id')
    context['album_data'] = PhotoGallery.objects.filter(id__in=[entry['latest_id'] for entry in latest_album_ids])
    
    if "submit" in request.POST:
        album_id = request.POST.get("album_id")
        context['photo_data'] = PhotoGallery.objects.filter(album_id=album_id,is_deleted=False).order_by('-id')
        context['album_name'] = Album.objects.values("name").filter(id=album_id).first()
        context['photo'] = True

    template = 'pages/website/photo_gallery.html'
    return render(request, template, context)

def news(request):
    template = 'pages/website/news.html'
    context = {
        "news": "active",
        "page_title": "BASETE RURAL DEVELOPMENT WELFARE TRUST | News & Events",
        "news": NewsEvent.objects.filter(is_deleted=False).order_by('event_date'),
        'current_data':  NewsEvent.objects.filter(is_deleted=False).order_by('event_date')[:1].first()
    }
    if request.method == 'POST':
        if 'view' in request.POST:
            context['current_data'] = NewsEvent.objects.filter(id=request.POST.get('id'), is_deleted=False).first()
    return render(request, template, context)

def siteMap(request):
    template = 'pages/website/site_map.html'
    context = {
        "about": "active",
        "page_title": "BASETE RURAL DEVELOPMENT WELFARE TRUST | Site map"
    }
    return render(request, template, context)

def screenReader(request):
    template = 'pages/website/screen_reader.html'
    context = {
        "about": "active",
        "page_title": "BASETE RURAL DEVELOPMENT WELFARE TRUST | Screen Reader"
    }
    return render(request, template, context)