from django.shortcuts import render
from .models import Event, News, Gallary, Video, Notification
from academics.models import Ranks
from alumini.models import alumni_corner
from placement.models import placement_updates

# Create your views here.

def index(request):

    # event data starts here
    event_dates = Event.objects.all()[::-1][:5]
    data = {}
    month = {'01' : 'JAN', '02' : 'FEB' , '03' : 'MAR', '04' : 'APR', '05' : 'MAY' , '06' : 'JUN', '07' : 'JUL',
            '08' : 'AUG', '09' : 'SEP', '10' : 'OCT', '11' : 'NOV', '12' : 'DEC'}
    
    # print(event_dates)
    dates = []
    for date in event_dates:
        current_date = str(date.event_date)
        current_venue = date.venue
        start_time = date.start
        end_time = date.end

        # print(type(current_date))
        res = current_date.split('-')
        # print(res)
        # data['year'] = res[0]
        data['month'] = month[res[1]]
        data['date'] = res[2]
        data['venue'] = current_venue
        data['start'] = start_time
        data['end'] = end_time
        data['event'] = date.event_name
        dates.append(data)
        data = {}
        # print(date.event_date.split('-'))
        # print(dates)
    
    # event data ends here

    # news data starts here
    news_data = {}
    result_news = []

    news = News.objects.all()

    for data in news:
        news_data['title'] = data.title
        news_data['description'] = data.description
        news_data['detail'] = data.detail
        # print(data.title,data.description,data.detail)
        result_news.append(news_data)
        news_data = {}

    # print(result_news)
    
    video_data = Video.objects.all().order_by('sno')
    notification = Notification.objects.all().order_by('-start_date')
    alumni_corner_events = alumni_corner.objects.all().order_by('-start_date')
    placement_updates_data = placement_updates.objects.all().order_by('-start_date')

    ranks_data=Ranks.objects.all().order_by('sno')
    setyr=set()
    setct=set()
    for record in ranks_data:
        setyr.add(record.year_range)
        setct.add(record.course_type)
        
    content = {'dates':dates, 'result_news':result_news, 'video_data':video_data, 
            'alumni_corner_events': alumni_corner_events, 'ranks_data':ranks_data,
	    'placement_updates_data': placement_updates_data, 'notification':notification,
	    'setyr':sorted(setyr, reverse=True), 'setct':sorted(setct, reverse=True)}

    return render(request,'index.html',content)

def home(request):
    print("comes here")
    return render(request,'bietHome.html')


def gallery(request):
    images = Gallary.objects.all()
    for i in images:
        print(i)
    return render(request,'gallery.html',{'images' : images})

def allevent(request):
    event_dates = Event.objects.all()[::-1]
    data = {}
    month = {'01' : 'JAN', '02' : 'FEB' , '03' : 'MAR', '04' : 'APR', '05' : 'MAY' , '06' : 'JUN', '07' : 'JUL',
            '08' : 'AUG', '09' : 'SEP', '10' : 'OCT', '11' : 'NOV', '12' : 'DEC'}
    
    # print(event_dates)
    dates = []
    for date in event_dates:
        current_date = str(date.event_date)
        current_venue = date.venue
        start_time = date.start
        end_time = date.end

        # print(type(current_date))
        res = current_date.split('-')
        # print(res)
        # data['year'] = res[0]
        data['month'] = month[res[1]]
        data['date'] = res[2]
        data['venue'] = current_venue
        data['start'] = start_time
        data['end'] = end_time
        data['event'] = date.event_name
        dates.append(data)
        data = {}
    # print(dates)
    return render(request,'allEvent.html',{'dates' : dates})

def notification(request):
    notification = Notification.objects.all().order_by('-start_date')
    
    return render(request,'notification.html',{'notification' : notification})

def developers(request):
    return render(request,'developers.html')
