from django_filters import rest_framework as filters
from .models import TDevice, DoorAuth


class TDeviceFilterSet(filters.FilterSet):
    state = filters.CharFilter(method='filter_state', label="used/unused")

    class Meta:
        model = TDevice
        fields = ['type', 'area', 'employee', 'obj_cate', 'state', 'code']

    def filter_state(self, queryset, name, value):
        if value == 'unused':
            queryset = queryset.filter(employee=None)
        elif value == 'used':
            queryset = queryset.exclude(employee=None)
        return queryset


class DoorAuthFilter(filters.FilterSet):
    class Meta:
        model = DoorAuth
        fields = {
            "employee": ["exact", "in"],
            "dchannel": ["exact", "in"],
            "employee__type": ["exact", "in"],
            "employee__belong_dept": ["exact", "in"],
        }
