#coding=utf-8

import django_filters
from apps.base import clean_datetime_range
from models import Goods, GoodsGodownEntry
from apps.product.models import ProductBase


class GoodsFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(name='product_base__name',lookup_expr='icontains')
    model = django_filters.CharFilter(name='product_base__model', lookup_expr='icontains')
    enabled = django_filters.CharFilter(name='product_base__enabled')

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

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


class GoodsGodownEntryFilter(django_filters.FilterSet):
    no = django_filters.CharFilter(name='no', lookup_expr='icontains')
    create_user_text = django_filters.CharFilter(name='create_user__name', lookup_expr='icontains')
    status = django_filters.CharFilter(name='status')
    create_time = django_filters.DateTimeFromToRangeFilter(field_name='create_time')
    check_time = django_filters.DateTimeFromToRangeFilter(field_name='check_time')
    class Meta:
        model = GoodsGodownEntry
        fields = "__all__"

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


class GoodsProductFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(name='name', lookup_expr='icontains')
    model = django_filters.CharFilter(name='model', lookup_expr='icontains')
    code = django_filters.CharFilter(name='code', lookup_expr='icontains')
    enabled = django_filters.CharFilter(name='product_base__enabled')

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

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