from django.http import HttpResponse
from django.http import FileResponse
from django.shortcuts import render
from django.views import generic
from django.template import loader
from django.views.decorators.clickjacking import xframe_options_exempt

from vehicles.models import *
from rest_framework import viewsets, filters
from Reskreen.rest_permission import CustomPermission_DjangoModel_based

from .serializers import cl_Vehicles_Serializer, cl_Vehicles_infos_Serializer

class VehicleViewset(viewsets.ModelViewSet):
    queryset = Vehicles.objects.all()
    serializer_class = cl_Vehicles_Serializer
    permission_classes = [CustomPermission_DjangoModel_based]
    filter_backends = [filters.OrderingFilter]
    ordering_fields = ['nStatus', 'nType', 'nOrder']  # Les champs sur lesquels vous voulez permettre l'ordering
    ordering = ['nType', 'nStatus', 'nOrder']  # Ordre par défaut

class VehicleInfosViewset(viewsets.ModelViewSet):
    queryset = Vehicles_infos.objects.all().order_by('-id')
    serializer_class = cl_Vehicles_infos_Serializer
    permission_classes = [CustomPermission_DjangoModel_based]
    ordering_fields = ['dtCreated']
    ordering = ['dtCreated']
    

@xframe_options_exempt
def view_vhc(request):
    if not "target" in request.GET.keys():
        target = "_self"
    else:
        target = "_blank"

    template = loader.get_template("vhc/vhc.html")
    a_vhc = Vehicles.objects.all().order_by("nStatus","nOrder")
    context = {'list_vhc': a_vhc,'target':target}
    print("passs 1 ligne 12")


    return HttpResponse(template.render(context,request))

@xframe_options_exempt
def view_peremptions(request):
    if not "target" in request.GET.keys():
        target = "_self"
    else:
        target = "_blank"
    template = loader.get_template("vhc/peremptions.html")
    a_vhc = Vehicles.objects.all().order_by("nOrder")
    context = {'list_vhc': a_vhc,'target':target}
    print("passs 1 ligne 12")


    return HttpResponse(template.render(context,request))
