# coding=utf-8
import django_filters

from apps.order.models import *


class OrderFilter(django_filters.FilterSet):
    no = django_filters.CharFilter(field_name='no', lookup_expr='icontains')
    customer_tel = django_filters.CharFilter(field_name='customer__tel', lookup_expr='icontains')
    tel = django_filters.CharFilter(field_name='tel', lookup_expr='icontains')
    range_time = django_filters.CharFilter(method='find_base_range_time')

    class Meta:
        model = Order
        fields = '__all__'

    def find_base_range_time(self, queryset, *args):
        if args[1]:
            range_time = args[1].split(' - ')
            end_time = range_time[1] + ' 23:59:59'
            queryset = queryset.filter(create_time__gte=range_time[0], create_time__lte=end_time)
        return queryset


class OrderDetailsFilter(django_filters.FilterSet):
    no = django_filters.CharFilter(field_name='order__no', lookup_expr='icontains')

    class Meta:
        model = OrderDetails
        fields = '__all__'


class ShoppingCartFilter(django_filters.FilterSet):

    class Meta:
        model = ShoppingCart
        fields = '__all__'
