from django.shortcuts import render, get_object_or_404
from .models import *
from django.db.models import Q



# LISTING
def items(request, *args, **kwargs):
    products = Item.objects.filter(type='Product').order_by('-id')
    services = Item.objects.filter(type='Service').order_by('-id')
    context = {
        'products': products,
        'services': services,

    }
    return render(request, 'listing_pages/items.html', context) 


def orders(request, *args, **kwargs):
    orders = Order.objects.all().filter(orderstatus__isnull=False).order_by('-id')
    context = {
        'orders': orders,
    }
    return render(request, 'listing_pages/orders.html', context) 


def invoices(request, *args, **kwargs):
    invoices = Invoice.objects.all().order_by('-id')

    context = {
        'invoices': invoices,
    }

    return render(request, 'listing_pages/invoices.html', context) 



def payment_methods(request):
    methods = PaymentType.objects.all().order_by('-id')

    context = {
        'methods': methods,
    }

    return render(request, 'listing_pages/payment-methods.html', context)




#DETAILS
def invoice_details(request, order_id):
    order = get_object_or_404(Order, id=order_id)

    context = {
        'order' : order,
    }

    return render(request, 'details_templates/invoice-details.html', context) 


def order_details(request, order_id):
    order = get_object_or_404(Order, order_id=order_id)
    order_items = OrderItem.objects.filter(order=order).order_by('-id')
    order_item_ids = order_items.values_list('item_id', flat=True)

    payments = OrderPayment.objects.filter(order = order).order_by('-id')

    context = {
        'order' : order,
        'order_items' : order_items,
        'payments' : payments,
    }

    return render(request, 'details_templates/order-details.html', context) 



def payment_details(request, order_id):
    order = get_object_or_404(Order, id=order_id)
    payments = OrderPayment.objects.filter(order = order).order_by('-id')

    context = {
        'payments' : payments,
    }

    return render(request, 'details_templates/payment-details.html', context) 


