from sets.admin import *
from .models import *
from django.contrib import admin
from django.forms import widgets
# from nested_inline.admin import NestedStackedInline, NestedTabularInline, NestedModelAdmin, NestedInline
from super_inlines.admin import SuperInlineModelAdmin, SuperModelAdmin
from GeneralApp.admin import Admin_StackedInline_Block
from SlidesApp.admin import Admin_StackedInline_Slide
from django.utils.translation import gettext as _


class Admin_Project(SuperModelAdmin, Admin_Trans_BaseModelViewPage):
    from ServicesApp.admin import Admin_StackedInline_BlockPluginPresentation

    def services_str(self, obj):
        services = obj.get_services()
        if services:
            return " / ".join(services.values_list("name", flat=True))
        return '-'
    services_str.short_description = _("Услуги")
    services_str.allow_tags = True

    fieldsets = [
        (None, {
            'classes': ['wide'],
            'fields': (
                ('name', 'enable'),
                'url',
                'title',
                'description',
                'services',
                # 'text',
                'picture',
                'order',
                )
        }),
        # (_('Дополнительно'), {
        #     'classes': ['wide', 'collapse'],
        #     'fields': (
        #         'FAQ_title',
        #     )
        # }),
        # ('SEO', {
        #     'classes': ['wide', 'collapse'],
        #     'fields': (
        #         'seo_title', 'seo_description', 'seo_keywords', 'seo_text',
        #    )
        # }),
    ]


    list_display = [
        'image_thumb',
        'id',
        'name', 'enable', 'url',
        # 'title',
        'order',
        'services_str',
        'createDT', 'modifiedDT'
    ]

    prepopulated_fields = {"url": ("name_en",)}

    list_display_links = ['id', 'name', 'image_thumb']
    list_editable = ['order', 'enable']

    list_filter = ['modifiedDT', 'createDT']
    search_fields = ['name', 'title']
    filter_horizontal = ['services']

    inlines = [
        # Admin_StackedInline_Slide,
        Admin_StackedInline_BlockPluginPresentation,
        Admin_StackedInline_Block,
        # AdminStacked_FAQitem,
    ]

    def has_delete_permission(self, request, obj=None):
        if request.user.is_superuser:
           return True

        if obj.url in ('main', 'spec_technics', 'works'):
            return False

admin.site.register(Project, Admin_Project)