from django.contrib.auth.views import LoginView, LogoutView
from django.db.models import Q
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views import generic
from django.views.generic import TemplateView, CreateView
from .forms import *
from .models import *


# Create your views here.

class ProductView(generic.ListView):
    model = Product
    template_name = 'index.html'
    context_object_name = 'product'

    def get_queryset(self):
        return Product.objects.filter()[:5]


class Login(LoginView):
    template_name = 'Login.html'


class Logout(LogoutView):
    success_url = 'index.html'


class CreateProduct(CreateView):
    form_class = ProductCreate
    model = Product
    success_url = '/'


class RegisterUser(CreateView):
    success_url = reverse_lazy('index')
    template_name = 'register.html'
    form_class = RegisterUserForm


class ServiceView(generic.ListView):
    model = Product
    template_name = 'Product.html'
    context_object_name = 'Servise'


class Profile(TemplateView):
    template_name = 'profile.html'


class ProductOneView(generic.ListView):
    model = Product
    template_name = 'ProductOne.html'
    context_object_name = 'productone'

    def get_queryset(self):  # new
        query = self.request.GET.get("q")
        object_list = Product.objects.filter(

        )
        return object_list


class SearchResultsView(generic.ListView):
    model = Product
    template_name = "search_results.html"

    def get_queryset(self):  # new
        query = self.request.GET.get("q")
        object_list = Product.objects.filter(
            Q(name__icontains=query)
        )
        return object_list