from django.shortcuts import render

# Create your views here.
from django.shortcuts import render
from apps.utils.viewsets import CustomModelViewSet, CustomGenericViewSet
from .models import PatentInfo, Papersecret, PatentRecord, PaperRecord, Platform, Platstanding
from .serializers import PatentInfoSerializer, PaperSeSerializer, PatentRecordSerializer, PaperRecordSerializer, PlatformSerializer, PlatstandingSerializer
   
from rest_framework.exceptions import ParseError
from rest_framework.response import Response
from rest_framework.decorators import action

class PatentInfoViewSet(CustomModelViewSet):
    """list: 专利

    专利
    """
    queryset = PatentInfo.objects.all()
    serializer_class = PatentInfoSerializer
    filterset_fields = ["name", "author", "type"]
    ordering = ["-create_time", "name", "author", "type"]

class PapersecretViewSet(CustomModelViewSet):
    """list: 论文申密审批

    论文申密审批
    """
    queryset = Papersecret.objects.all()
    serializer_class = PaperSeSerializer
    filterset_fields = ["paper_name", "author"]
    ordering = ["-create_time", "paper_name"]

class PatentRecordViewSet(CustomModelViewSet):
    """list: 专利台账登记

    专利台账登记
    """
    queryset = PatentRecord.objects.all()
    serializer_class = PatentRecordSerializer
    select_related_fields = ["patent"]
    filterset_fields = ["volume_number", "patent","inventors"]
    ordering = ["-create_time", "volume_number"]
    search_fields = ["volume_number", "patent", "inventors"]
    
    def get_queryset(self):
        qs = super().get_queryset()
        patent_type = self.request.query_params.get('patent_type', None)
        if patent_type:
            qs = qs.filter(patent__type=patent_type)
        return qs
    

    @action(detail=False, methods=['get'])
    def patent_name(self, request):
        """获取专利列表"""
        search = request.query_params.get('search', '')
        queryset = PatentInfo.objects.all()
        if search:
            queryset = queryset.filter(name__icontains=search)
        patents = [{'id': patent.id, 'name': patent.name} for patent in queryset]
        return Response(patents)

class PaperRecordViewSet(CustomModelViewSet):
    """list: 论文台账登记

    论文台账登记
    """
    queryset = PaperRecord.objects.all()
    serializer_class = PaperRecordSerializer
    select_related_fields = ["paper"]
    filterset_fields = ["paper_number", "paper","author"]
    ordering = ["-create_time", "paper_number"]
    search_fields = ["paper_number", "paper", "author"]
    
    def get_queryset(self):
        qs = super().get_queryset()
        paper_type = self.request.query_params.get('paper_type', None)
        if paper_type:
            qs = qs.filter(paper__paper_type=paper_type)
        return qs
    

    @action(detail=False, methods=['get'])
    def paper_name(self, request):
        """获取论文列表"""
        search = request.query_params.get('search', '')
        queryset = Papersecret.objects.all()
        if search:
            queryset = queryset.filter(paper_name__icontains=search)
        papers = [{'id': paper.id, 'name': paper.paper_name} for paper in queryset]
        return Response(papers)

class PlatformViewSet(CustomModelViewSet):
    """list: 平台审批

    平台审批
    """
    queryset = Platform.objects.all()
    serializer_class = PlatformSerializer
    filterset_fields = ["p_dept"]
    ordering = ["-create_time", "p_dept"]
    search_fields = ["p_dept"]


class PlatstandingViewSet(CustomModelViewSet):
    """list: 平台台账

    平台审批
    """
    queryset = Platstanding.objects.all()
    serializer_class = PlatstandingSerializer
    filterset_fields = ["platinfo", "city_p", "province_p"]
    ordering = ["-create_time"]
    search_fields = ["platinfo", "city_p", "province_p"]

    @action(detail=False, methods=['get'])
    def plat_name(self, request):
        """获取台账列表"""
        search = request.query_params.get('search', '')
        queryset = Platform.objects.all()
        if search:
            queryset = queryset.filter(name__icontains=search)
        papers = [{'id': p.id, 'name': p.name} for p in queryset]
        return Response(papers)