from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet
from .models import *
from .serializers import *
from apps.system.permission_data import RbacFilterSet
from apps.system.mixins import CreateUpdateCustomMixin, OptimizationMixin
# Create your views here.
class EmployeeViewSet(CreateUpdateCustomMixin, OptimizationMixin, ModelViewSet):
    """
    详细信息-增删改查
    """
    perms_map = {'get': '*', 
                 'put': 'employee_update', 'delete': 'employee_delete'}
    queryset = Employee.objects.all()
    serializer_class = EmployeeSerializer
    search_fields = ['code', 'remember_code', 'user__name']
    filterset_fields = ['is_onjob', 'is_fulltime']
    ordering = ['-create_time']

    def get_serializer_class(self):
        if self.action == 'update':
            return EmployeeUpdateSerializer
        else:
            return self.serializer_class

class QualificationViewSet(CreateUpdateCustomMixin, OptimizationMixin, ModelViewSet):
    """
    注册资格-增删改查
    """
    perms_map = {'get': '*', 'post': 'qucalification_create',
                 'put': 'qucalification_update', 'delete': 'qucalification_delete'}
    queryset = Qualification.objects.all()
    serializer_class = QualificationSerializer
    search_fields = ['cert_number', 'auditor_type__name']
    filterset_fields = ['auditor_type', 'cert_field', 'is_enabled']
    ordering = ['-create_time']

class AbilityViewSet(CreateUpdateCustomMixin, OptimizationMixin, ModelViewSet):
    """
    专业能力-增删改查
    """
    perms_map = {'get': '*', 'post': 'ability_create',
                 'put': 'ability_update', 'delete': 'ability_delete'}
    queryset = Ability.objects.all()
    serializer_class = AbilitySerializer
    search_fields = ['major__code', 'major_rule__code']
    filterset_fields = ['major', 'major_rule', 'auditor_abilitys']
    ordering = ['-create_time']

class EducationViewSet(CreateUpdateCustomMixin, OptimizationMixin, ModelViewSet):
    """
    教育经历-增删改查
    """
    perms_map = {'get': '*', 'post': 'education_create',
                 'put': 'education_update', 'delete': 'education_delete'}
    queryset = Education.objects.all()
    serializer_class = EducationSerializer
    search_fields = ['school', 'major', 'employee__name']
    filterset_fields = ['level']
    ordering = ['-create_time']

class TrainViewSet(CreateUpdateCustomMixin, OptimizationMixin, ModelViewSet):
    """
    培训记录-增删改查
    """
    perms_map = {'get': '*', 'post': 'train_create',
                 'put': 'train_update', 'delete': 'train_delete'}
    queryset = Train.objects.all()
    serializer_class = TrainSerializer
    search_fields = ['name', 'content']
    # filterset_fields = ['status']
    ordering = ['-create_time']