#coding=utf-8
from django.utils import timezone
import datetime
import django_filters
from apps.base import clean_datetime_range
from .models import *
from apps.customer.models import Customer
from apps.account.models import Branch


class BranchFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(field_name='name',lookup_expr='icontains')
    tel = django_filters.CharFilter(field_name='tel', lookup_expr='icontains')
    enabled = django_filters.CharFilter(field_name='enabled')

    class Meta:
        model = Branch
        fields = (
            'name', 'tel', 'enabled'
        )

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


class CustomerFilter(django_filters.FilterSet):
    nickname = django_filters.CharFilter(field_name='nickname', lookup_expr='icontains')
    tel = django_filters.CharFilter(field_name='tel', lookup_expr='icontains')

    class Meta:
        model = Customer
        fields = '__all__'


class MemberCouponFilter(django_filters.FilterSet):
    write_off = django_filters.CharFilter(field_name='write_off')
    coupon_name = django_filters.CharFilter(field_name='coupon__name', lookup_expr='icontains')
    activity_name = django_filters.CharFilter(field_name='activity__title', lookup_expr='icontains')

    status = django_filters.CharFilter(method='find_coupon_by_status')

    class Meta:
        model = MemberCoupon
        fields = '__all__'

    def find_coupon_by_status(self, queryset, *args):
        if args[1]:
            if args[1] == '1':#待使用
                now = datetime.datetime.date(timezone.now())
                queryset = queryset.filter(write_off=False, end_date__gt=now)
            elif args[1] == '2':#已使用
                queryset = queryset.filter(write_off=True)
            elif args[1] == '3':#已过期
                now = datetime.datetime.date(timezone.now())
                queryset = queryset.filter(write_off=False, end_date__lt=now)
        return queryset


class OrderFilter(django_filters.FilterSet):
    activity_name = django_filters.CharFilter(field_name='activity__title', lookup_expr='icontains')
    branch_name = django_filters.CharFilter(field_name='branch__name', lookup_expr='icontains')
    activity = django_filters.CharFilter(field_name='activity')
    delete = django_filters.CharFilter(field_name='delete')

    order_status = django_filters.CharFilter(method='find_by_order_status')

    class Meta:
        model = Order
        fields = '__all__'

    def find_by_order_status(self, queryset, *args):
        if args[1]:
            if args[1] == '1': # 进行中
                queryset = queryset.filter(status=Order.DEFAULT)
            elif args[1] == '2': # 已完成
                queryset = queryset.filter(status=Order.FINISH)
            elif args[1] == '3': # 已失败
                queryset = queryset.filter(status=Order.FAIL)
        return queryset



class CouponFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(field_name='name',lookup_expr='icontains')
    activity = django_filters.CharFilter(field_name='activity')
    enabled = django_filters.CharFilter(field_name='enabled')

    class Meta:
        model = Coupon
        fields = (
            'name', 'enabled'
        )

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


class ActivityFilter(django_filters.FilterSet):
    title = django_filters.CharFilter(field_name='title',lookup_expr='icontains')
    branch_name = django_filters.CharFilter(field_name='branch_name', lookup_expr='icontains')
    enabled = django_filters.CharFilter(field_name='enabled')
    check_status = django_filters.CharFilter(field_name='check_status')

    class Meta:
        model = Activity
        fields = (
            'title', 'branch_name', 'check_status', 'enabled'
        )

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