from django.contrib import admin
from django.utils.safestring import mark_safe

from .models import *
from admin_ordering.admin import OrderableAdmin

# Register your models here.


@admin.register(Button, UpperButton)
class ButtonModelAdmin(OrderableAdmin, admin.ModelAdmin):
    list_display = ["__str__", "published", "ordering"]
    list_editable = ["ordering"]
    ordering_field_hide_input = True
    exclude = ["ordering"]

@admin.register(AdditionalPagePost)
class AdditionalPagePostModelAdmin(OrderableAdmin, admin.ModelAdmin):
    list_display = ["__str__", "published", "ordering"]
    list_editable = ["ordering"]
    ordering_field_hide_input = True
    exclude = ["ordering"]


# @admin.register(AdditionalPagePost)
class AdditionalPagePostInline(OrderableAdmin, admin.TabularInline):
    model = AdditionalPagePost
    list_editable = ["ordering"]
    ordering_field_hide_input = True
    fields = ["title", "ordering"]
    show_change_link = True
    extra = 0


@admin.register(AdditionalPage)
class AdditionalPageModelAdmin(OrderableAdmin, admin.ModelAdmin):
    list_display = ["__str__", "published"]
    readonly_fields = ["posts"]
    inlines = [AdditionalPagePostInline]

    @admin.display(description="Opublikowane posty")
    def posts(self, obj):
        return mark_safe(''.join([f'<a href="/post/{p.id}" target="_blank">{p.title}</a><br>' for p in obj.posts.filter(published=True)]))


admin.site.site_header = "ŁABS"
admin.site.site_title = "ŁABS"
admin.site.index_title = "Panel administracyjny"
admin.site.site_url = "/"
admin.site.enable_nav_sidebar = True
