from django.contrib import admin
from .models import Store, MenuItem, Setting, StoreFile, Table, BookingTable, Order,  MenuFile, OrderItem, UIMenu, PostCategory, Post
from django_better_admin_arrayfield.admin.mixins import DynamicArrayMixin
from mptt.admin import MPTTModelAdmin
from django_admin_hstore_widget.forms import HStoreFormField

from django.contrib.postgres.fields import HStoreField, ArrayField

from django import forms




# Register your models here.

class StoreFileAdmin(admin.TabularInline):
    model = StoreFile

class MenuItemAdmin(admin.StackedInline):
    model = MenuItem

class OrderItemAdmin(admin.TabularInline):
    model = OrderItem

class StoreAdmin(admin.ModelAdmin):
   inlines = [StoreFileAdmin, MenuItemAdmin]
   formfield_overrides = {
        HStoreField: {'widget': HStoreFormField},
    }

class SettingAdmin(admin.ModelAdmin):
    pass

class OrderAdmin(admin.ModelAdmin):
   inlines = [OrderItemAdmin,]


class SettingAdminForm(forms.ModelForm):
    data = HStoreFormField()

    class Meta:
       model = Setting
       exclude = ()

@admin.register(Setting)
class SettingAdmin(admin.ModelAdmin):
    form = SettingAdminForm

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'tag_list', 'cats', 'created_at', 'store', 'featureImage')
    save_as = True

    def tag_list(self, obj):
        return u", ".join(o.name for o in obj.tags.all())


admin.site.register(Store, StoreAdmin)
admin.site.register(MenuItem)
#admin.site.register(Setting, SettingAdmin)
admin.site.register(StoreFile)
admin.site.register(Table)
admin.site.register(BookingTable)
admin.site.register(Order, OrderAdmin)
admin.site.register(MenuFile)
admin.site.register(UIMenu, MPTTModelAdmin)
#admin.site.register(Post)
admin.site.register(PostCategory, MPTTModelAdmin)


