from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from .models import TodoList, Item
from .forms import CreateNewList

# Create your views here.
def index(response, id):
    ls = TodoList.objects.get(id=id)
    # return HttpResponse("<h1>%s</h1>" %ls.name)
    # return render(response, "main/base.html", {"name": ls.name})
    # return render(response, "main/base.html", {})
    return render(response, "main/list.html", {"ls": ls})


def home(response):
    # return render(response, "main/home.html", {"name": "Just testing"})
    return render(response, "main/home.html", {})


def create(response):
    if response.method == "POST":
        form = CreateNewList(response.POST)


        if form.is_valid():
            n = form.cleaned_data["name"]
            t = TodoList(name=n)
            t.save()

        return HttpResponseRedirect("/%i" %t.id)
    else:
        form = CreateNewList()
    return render(response, "main/create.html", {"form": form})
