# coding=utf-8
import django_filters

from .models import *
from apps.base import clean_datetime_range

class OrderFilter(django_filters.FilterSet):
    no = django_filters.CharFilter(field_name='no', lookup_expr='icontains')
    name = django_filters.CharFilter(field_name='agent__name', lookup_expr='icontains')
    create_time_0 = django_filters.DateTimeFilter(field_name='create_time', lookup_expr='gte')
    create_time_1 = django_filters.DateTimeFilter(field_name='create_time', lookup_expr='lte')
    create_user_name = django_filters.CharFilter(field_name='create_user__name', lookup_expr='icontains')
    pay_no = django_filters.CharFilter(field_name='pay__pay_no', lookup_expr='icontains')
    area_manager = django_filters.CharFilter(field_name='agent__area_manager__name', lookup_expr='icontains')

    class Meta:
        model = Order
        fields = '__all__'

    def __init__(self, data=None, *args, **kwargs):
        data = clean_datetime_range(data, 'create_time')
        super(OrderFilter, self).__init__(data, *args, **kwargs)

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

    class Meta:
        model = Logistics
        fields = '__all__'


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__'

class OrderPaymentVoucherFilter(django_filters.FilterSet):

    class Meta:
        model = OrderPaymentVoucher
        fields = '__all__'


class CustomerOrderFilter(django_filters.FilterSet):
    no = django_filters.CharFilter(field_name='no', lookup_expr='icontains')
    name = django_filters.CharFilter(field_name='agent__name', lookup_expr='icontains')
    create_time_0 = django_filters.DateTimeFilter(field_name='create_time', lookup_expr='gte')
    create_time_1 = django_filters.DateTimeFilter(field_name='create_time', lookup_expr='lte')

    class Meta:
        model = CustomerOrder
        fields = '__all__'

    def __init__(self, data=None, *args, **kwargs):
        data = clean_datetime_range(data, 'create_time')
        super(CustomerOrderFilter, self).__init__(data, *args, **kwargs)


class CustomerShoppingCartFilter(django_filters.FilterSet):

    class Meta:
        model = CustomerShoppingCart
        fields = '__all__'
