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 _

# def sets_for_formfield_for_dbfield(field, db_field):
#     if db_field.name == 'url' or db_field.name == 'name' or db_field.name == 'title' or db_field.name == 'name_plural':
#         field.widget = widgets.TextInput(attrs={'style': 'width: 30%; height: 20px;'})
#     if db_field.name == 'description':
#         field.widget = widgets.Textarea(attrs={'style': 'width: 30%; height: 100px;'})
#     return field



class Admin_StackedInline_BlockPluginPresentation(Admin_Trans_GenericBaseIconStackedInline):
# class Admin_StackedInline_Block(Admin_Trans_GenericBaseIconStackedInline):

    def formfield_for_dbfield(self, db_field, request, **kwargs):
        field = super(Admin_StackedInline_BlockPluginPresentation, self).formfield_for_dbfield(db_field, request, **kwargs)
        return sets_for_formfield_for_dbfield(field, db_field)

    model = BlockPluginPresentation
    extra = 0
    max_num = 1
    fieldsets = [
        (None, {
            'classes': [],
            'fields': [
                'pre_title',
                # 'title',
                'name', 'description', 'picture',
                ('but1_title', 'but1_icon', 'but1_url'),
                ('but2_title', 'but2_icon', 'but2_url'),
                # ('order',),
            ]
        })
    ]



# class Admin_StaticPage(NestedModelAdmin, Admin_Trans_BaseModelViewPage):
class Admin_Section(SuperModelAdmin, Admin_Trans_BaseModelViewPage):


    fieldsets = [
        (None, {
            'classes': ['wide'],
            'fields': ('name', 'page_scheme',
                       'url',
                       'title',
                       'description', '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', 'url',
        # 'title',
        'order'
    ]

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

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

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

    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(Section,Admin_Section)



class Admin_Service(SuperModelAdmin, Admin_Trans_BaseModelViewPage):


    fieldsets = [
        (None, {
            'classes': ['wide'],
            'fields': (
                'section', 'parent_service',
                'name',
                'url',
                # 'title',
                'description', 'text',
                'picture',
                'order',
            )
        }),
        # ('SEO', {
        #     'classes': ['wide', 'collapse'],
        #     'fields': (
        #         'seo_title', 'seo_description', 'seo_keywords', 'seo_text',
        #    )
        # }),
    ]


    list_display = [
        'image_thumb',
        'id',
        'name', 'url', 'section', 'parent_service',
        # 'title',
        'order', 'modifiedDT', 'createDT'
    ]

    ordering = ['section', 'parent_service', 'order']

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

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

    list_filter = ['section', 'parent_service', 'modifiedDT', 'createDT', ]
    search_fields = ['name', 'title']
    # filter_horizontal = ['options']

    inlines = [Admin_StackedInline_Block]

    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(Service, Admin_Service)

