from django.contrib import admin
from mptt.admin import MPTTModelAdmin
# Register your models here.
from hotel.models import Category
from hotel.models import Product
from hotel.models import Images
from django.utils.html import format_html
from mptt.admin import DraggableMPTTAdmin
from hotel.models import Comments




class ImagesInline(admin.TabularInline):
    model = Images
    extra = 3


class CategoryAdmin(MPTTModelAdmin):
    list_display = ['title' , 'status']
    list_filter = ['status']



class ProductAdmin(admin.ModelAdmin):
    list_display = ['title' , 'status','category', 'price','image_tag','odasayisi']
    list_filter = ['price','star','odasayisi']
    inlines = [ImagesInline]
    prepopulated_fields = {'slug': ('title',)}


class ImagesAdmin(admin.ModelAdmin):
    list_display = ['title','hotel','image_tag']
   


class CategoryAdmin2(DraggableMPTTAdmin):
    mptt_indent_field = "title"
    list_display = ('tree_actions', 'indented_title',
                    'related_products_count', 'related_products_cumulative_count')
    list_display_links = ('indented_title',)
    prepopulated_fields = {'slug': ('title',)}

    def get_queryset(self, request):
        qs = super().get_queryset(request)

        # Add cumulative product count
        qs = Category.objects.add_related_count(
                qs,
                Product,
                'category',
                'products_cumulative_count',
                cumulative=True)

        # Add non cumulative product count
        qs = Category.objects.add_related_count(qs,
                 Product,
                 'category',
                 'products_count',
                 cumulative=False)
        return qs

    def related_products_count(self, instance):
        return instance.products_count
    related_products_count.short_description = 'Related products (for this specific category)'

    def related_products_cumulative_count(self, instance):
        return instance.products_cumulative_count
    related_products_cumulative_count.short_description = 'Related products (in tree)'

class CommentAdmin(admin.ModelAdmin):
    list_display = ['subject','comment','product','user','status']
    list_filter = ['status']


admin.site.register(Comments,CommentAdmin)
admin.site.register(Category,CategoryAdmin2)
admin.site.register(Images,ImagesAdmin)
admin.site.register(Product,ProductAdmin)


