from pos.models import MenuItem, Store, Setting, Table, BookingTable, Order, OrderItem, UIMenu, Post, PostCategory, Customer, Code, Profile, Message

from django.forms import ModelForm
import django_filters

from django.forms import BaseInlineFormSet, DateField, TimeField, TextInput, ModelMultipleChoiceField, CheckboxSelectMultiple

from mptt.forms import TreeNodeMultipleChoiceField
from django_editorjs_fields import EditorJsWidget
from django.contrib.auth.models import User
from django_google_maps.widgets import GoogleMapsAddressWidget

class TableForm(ModelForm):
    class Meta:
        model = Table
        fields = ('code', 'nSeats', 'occupy',)

class MessageForm(ModelForm):
    class Meta:
        model = Message
        fields = ('subject', 'email', 'tel', 'body', 'messageFile', 'lineId', 'reply', 'parent', )

class UIMenuForm(ModelForm):
    class Meta:
        model = UIMenu
        fields = ('name', 'parent', 'group', 'url', 'icon', 'nOrder', )

class OrderForm(ModelForm):
    class Meta:
        model = Order
        fields = '__all__'

class PostForm(ModelForm):
    #category = TreeNodeMultipleChoiceField(queryset=PostCategory.objects.all())
    '''
    relateMenus = ModelMultipleChoiceField(
        queryset=MenuItem.objects.all(),
        widget=CheckboxSelectMultiple,
        required=False)
    '''

    class Meta:
        model = Post
        fields = ('featureImage', 'title', 'content', 'description', 'category', 'tags', 'relateMenus' )

class CustomerForm(ModelForm):
    #category = TreeNodeMultipleChoiceField(queryset=PostCategory.objects.all())

    class Meta:
        model = Customer
        fields = ('fullName', 'email', 'lineId', 'gender', )

class BookingForm(ModelForm):
    onDate = DateField(
        widget=TextInput(
            attrs={'type': 'date'}
        )
    )
    onTime = TimeField(
        widget=TextInput(
            attrs={'type': 'time'}
        )
    )
    class Meta:
        model = BookingTable
        fields = ('table', 'onDate',  'onTime', 'note',)

class MenuItemForm(ModelForm):
    class Meta:
        model = MenuItem
        fields = ['featureImage', 'name', ]

class StoreForm(ModelForm):
    class Meta:
        model = Store
        fields = ['logo', 'name','description', 'addressText', 'province','address',  'phone_number', 'fax_number', 'lineId',  'email', 'geolocation',  'storeType']

        widgets = {
            "address": GoogleMapsAddressWidget,
        }

class OrderItemFormSet(BaseInlineFormSet):
    def __init__(self, other_model_queryset, *args, **kwargs):
        super(BaseInlineFormSet, self).__init__(*args)

        for form in self.forms:
            form.fields['item'].queryset = other_model_queryset


formMap = {
    'tableForm': TableForm
}

class OrderFilter(django_filters.FilterSet):
    note = django_filters.CharFilter(lookup_expr='iexact')

    class Meta:
        model = Order
        fields = ['table', 'note']

class MenuItemFilter(django_filters.FilterSet):
    class Meta:
        model = MenuItem
        fields = ['name', ]

class StoreFilter(django_filters.FilterSet):
    class Meta:
        model = Store
        fields = ('name', 'address', 'province','storeType')

class BookingFilter(django_filters.FilterSet):
    class Meta:
        model = BookingTable
        fields = ('table', 'onDate', 'note', )

class CustomerFilter(django_filters.FilterSet):
    class Meta:
        model = Customer
        fields = ('fullName', 'email', 'lineId', 'gender', 'created_at', )

class TableFilter(django_filters.FilterSet):
    class Meta:
        model = Table
        fields = ('code', 'nSeats', 'occupy', )

class UIMenuFilter(django_filters.FilterSet):
    class Meta:
        model = UIMenu
        fields = ('name', 'parent', 'group', 'url', 'icon','nOrder')

class MessageFilter(django_filters.FilterSet):
    class Meta:
        model = Message
        fields = ('subject', 'email', 'tel', 'body', 'reply', 'parent', 'created_at',  )

class PostFilter(django_filters.FilterSet):
    class Meta:
        model = Post
        fields = ('title', 'content', )

class UserFilter(django_filters.FilterSet):
    profile__role = django_filters.ChoiceFilter(label="Role", choices=Profile.ROLE_CHOICES)

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', )




class CodeForm(ModelForm):
    class Meta:
        model = Code
        exclude = ['store', 'owner',]
        widgets = {
            'body_editorjs': EditorJsWidget(config={'minHeight': 100}),
            'body_editorjs_text': EditorJsWidget(plugins=["@editorjs/image", "@editorjs/header"]),
        }

