# from django.shortcuts import render
from django.contrib.auth import authenticate, login
from django.http import HttpResponse, Http404
from django.template import loader
from django.shortcuts import render, get_object_or_404, get_list_or_404, redirect

from .models import Bill


def index(request):
    if request.user.is_authenticated:
        bills_list = Bill.objects.order_by('-due')
        # bills_list = get_list_or_404(Bill)
        template = loader.get_template('bills/index.html')
        context = {'bills_list': bills_list, }
        print(bills_list)
        return HttpResponse(template.render(context, request))
    else:
        # return redirect(api.index)
        return render(request, "index.html")
    # return render(request, 'bills/index.html', context)


def detail(request, bill_id):
    bill = get_object_or_404(Bill, pk=bill_id)
    return render(request, 'bills/detail.html', {'bill': bill})

# view to retriev a users bills


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