from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse
from .models import Customer
from django.db.models import Q
from django.core.paginator import Paginator
# from Item.models import Item, PriceLevel, PriceLevelItem
from .forms import CustomerForm

# def index(request):
#     context={}
#     qs1 = Item.objects.filter(itempricelevel__PL__Name='B2020',).prefetch_related("itempricelevel").values('FullName','SalesPrice', 'itempricelevel__Price')
#     qs2 = Item.objects.exclude(itempricelevel__PL__Name='B2020').prefetch_related("itempricelevel").values( "FullName", "SalesPrice", "AlwaysNull")
#     print(qs2)
#     qs = qs1.union(qs2)
#     print(type(qs))
#     print(qs)
#     context['objects'] = qs
#     return render(request, "Customer/index.html", context=context)

def index(request):
    context={}
    search = request.GET.get("q")
    customers=Customer.objects.all().order_by('CustomerFullName')
    print(customers)
    if search:
        customer = Customer.objects.order_by('CustomerFullName').filter(Q(CustomerName__icontains=search) | Q(CustomerFullName__icontains=search))
    else:
        customer = Customer.objects.order_by('CustomerFullName')
    paginator = Paginator(customer, 25)  # Show 25 contacts per page.
    page_number = request.GET.get("page")
    page_obj = paginator.get_page(page_number)
    # heads = [f.name for f in Customer._meta.get_fields()]
    # print(heads)
    print(customer)
    print(page_obj[0].CustomerName)
    context['objects'] = page_obj
    context['customers'] = customers
    context['addurl'] = reverse('Customer:add_customer')
    return render(request, "Customer/index.html", context=context)

def edit_customer(request, pk):
    print("edit customer")
    print(pk)
    print(request)
    customer = get_object_or_404(Customer, pk=pk)
    form = CustomerForm( request.POST or None, instance=customer)
    if request.method == "GET":
        print("GET")
        if customer:
            # form = CustomerForm(instance=customer)
            return render(request, "Customer/addedit.html", {"objects": [customer,], "form":form})
    elif request.method == "POST":
        print("POST")
        # form = CustomerForm(request.POST, instance=customer)
        if form.is_valid():
            print("form is valid")
            customer=form.save()
            # return redirect(reverse('Customer:edit_customer', kwargs={"pk":pk}))
            return render(request, "Customer/index.html", {"objects": [customer]})
        else:
            print("not valid form")
            return render(request, "Customer/addedit.html", {"objects": [customer,], "form":form})
        
def add_customer(request):
    print("add customer")
    form = CustomerForm(request.POST or None)
    if request.POST:
        # print(form["CustomerName"].value())
        if form.is_valid():
            # print(form.cleaned_data["CustomerName"])
            form.save()
            return redirect(reverse("Customer:index"))
    # print(form["CustomerName"].value())
    return render(request, "Customer/addedit.html", {"form":form})

def delete_customer(request, pk):
    print("delete customer")