import django_filters
from .models import Data, DataMs, TbFgPressinfoLotlist, LotSummary, DataRl, DataWb, \
        LotSummaryRl, LotSummaryWb, RotateData, Manualsize

class DataFilter(django_filters.FilterSet):
    lot_no = django_filters.CharFilter(field_name='lot_no', lookup_expr='icontains')
    code = django_filters.CharFilter(field_name='code', lookup_expr='icontains')
    r_type = django_filters.CharFilter(method='filter_r_type')


    class Meta:
        model = Data
        fields = ['lot_no', 'code', 'r_type']  # Add fields you want to filter
    
    def filter_r_type(self, queryset, name, value):
        # Split the comma-separated values into a list
        r_type_list = [v.strip() for v in value.split(',') if v.strip()]
        # Filter the queryset using the list
        return queryset.filter(**{f"{name}__in": r_type_list})

class DataMsFilter(django_filters.FilterSet):
    lot_no = django_filters.CharFilter(field_name='lot_no', lookup_expr='icontains')
    code = django_filters.CharFilter(field_name='code', lookup_expr='icontains')

    class Meta:
        model = DataMs
        fields = ['lot_no', 'code']  # Add fields you want to filter

class TbFgPressFilter(django_filters.FilterSet):
    productcode = django_filters.CharFilter(field_name='productcode', lookup_expr='icontains')
    product_year = django_filters.CharFilter(field_name='product_year', lookup_expr='icontains')

    class Meta:
        model = TbFgPressinfoLotlist
        fields = ['productcode', 'product_year']  # Add fields you want to filter



class LotSummaryFilter(django_filters.FilterSet):
    lot_no = django_filters.CharFilter(field_name='lot_no', lookup_expr='icontains')
    code = django_filters.CharFilter(field_name='code', lookup_expr='icontains')

    class Meta:
        model = LotSummary
        fields = ['lot_no', 'code']  # Add fields you want to filter


class DataRlFilter(django_filters.FilterSet):
    lot_no = django_filters.CharFilter(field_name='lot_no', lookup_expr='icontains')
    code = django_filters.CharFilter(field_name='code', lookup_expr='icontains')

    class Meta:
        model = DataRl
        fields = ['lot_no', 'code']  # Add fields you want to filter

class DataWbFilter(django_filters.FilterSet):
    lot_no = django_filters.CharFilter(field_name='lot_no', lookup_expr='icontains')
    code = django_filters.CharFilter(field_name='code', lookup_expr='icontains')

    class Meta:
        model = DataWb
        fields = ['lot_no', 'code']  # Add fields you want to filter

class LotSummaryRlFilter(django_filters.FilterSet):
    lot_no = django_filters.CharFilter(field_name='lot_no', lookup_expr='icontains')
    code = django_filters.CharFilter(field_name='code', lookup_expr='icontains')

    class Meta:
        model = LotSummaryRl
        fields = ['lot_no', 'code']  # Add fields you want to filter


class LotSummaryWbFilter(django_filters.FilterSet):
    lot_no = django_filters.CharFilter(field_name='lot_no', lookup_expr='icontains')
    code = django_filters.CharFilter(field_name='code', lookup_expr='icontains')

    class Meta:
        model = LotSummaryWb
        fields = ['lot_no', 'code']  # Add fields you want to filter

class RotateDataFilter(django_filters.FilterSet):
    lot_no = django_filters.CharFilter(field_name='lot_no', lookup_expr='icontains')
    code = django_filters.CharFilter(field_name='code', lookup_expr='icontains')

    class Meta:
        model = RotateData
        fields = ['lot_no', 'code']  # Add fields you want to filter


class ManualsizeFilter(django_filters.FilterSet):
    lotno = django_filters.CharFilter(field_name='lotno', lookup_expr='icontains')
    size_name = django_filters.CharFilter(field_name='size_name', lookup_expr='icontains')
    std = django_filters.NumberFilter(field_name='std')
    tolun = django_filters.NumberFilter(field_name='tolun')
    tolup = django_filters.NumberFilter(field_name='tolup')
    created_at = django_filters.DateTimeFilter(field_name='created_at', lookup_expr='gte')  # Filter for recent entries
    productcode = django_filters.CharFilter(field_name='productcode', lookup_expr='icontains')
    class Meta:
        model = Manualsize
        fields = ['lotno', 'size_name', 'std', 'tolun', 'tolup', 'created_at', 'productcode']
