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


@admin.register(Students)
class StudentsAdmin(admin.ModelAdmin):
    list_display = (
        'stud_photo',
        'stud_fio',
        # 'group',
        'surname',
        'name',
        'patronymic',
        'gender',
        # 'photo',
    )
    list_display_links = ('stud_fio', )
    search_fields = ('surname',)
    list_editable = ('gender', )
    list_filter = ('gender',)
    autocomplete_fields = ('name_cases', 'patronymic_cases',)
    readonly_fields = ('stud_photo',)
    fields = [
    ('surname', 'surname_cases'),
    ('name', 'name_cases'),
    ('patronymic', 'patronymic_cases'),
    # ('surname', 'name', 'patronymic'),
    # ('surname_cases', 'name_cases', 'patronymic_cases'),
    ('gender', 'birthdate'),
    ('phone', 'e_mail'),
    ('stud_photo', 'photo'),
    ('passport', 'snils', 'diploma')
    ]
    @admin.display(description="фото")
    def stud_photo(self, student: Students):
        if student.photo:
            return mark_safe(f'<img src="{student.photo.url}" width=50')
        return 'Без фото'
    
    @admin.display(description='ФИО', ordering='surname')
    def stud_fio(self, student: Students):
        return f'{student.surname} {student.name} {student.patronymic}'

@admin.register(Emploees)
class EmploeesAdmin(admin.ModelAdmin):
    list_display = (
        # 'group',
        'surname',
        'name',
        'patronymic',
        'gender',
        'photo'
    )
    list_display_links = ('surname',)
    search_fields = ('surname',)

@admin.register(Groups)
class GroupsAdmin(admin.ModelAdmin):
    list_display = (
        'title',
        'graduation_at'
    )

@admin.register(Contracts)
class ContractsAdmin(admin.ModelAdmin):
    list_filter = (
        'group',
    )
    fields = [
        ('number', 'agreement_date'), ('start_date', 'end_date'),
        ('payment_date1', 'payment_date2', 'payment_date3'), ('client', 'customer'),
        'structure', 'scan', 'group', 'degree_work'
        ]
    search_fields = (
        'number',
    )

@admin.register(NameCases)
class NameCasesAdmin(admin.ModelAdmin):
    # list_display = ('')
    search_fields = ('genitive', 'dative',)


@admin.register(Patronymic_cases)
class PatronymicCasesAdmin(admin.ModelAdmin):
    search_fields = ('genitive', 'dative',)




@admin.register(PassportDepartment)
class PassportDepartamentAdmin(admin.ModelAdmin):
    search_fields = ('code',)

@admin.register(Passports)
class PassportAdmin(admin.ModelAdmin):
    fields = [
        ('series', 'number',),
        ('issued_date', 'issued_department',),
        'passport_department', 'address_registration'
    ]
    autocomplete_fields = ('passport_department',)


@admin.register(Orders)
class OrdersAdmin(admin.ModelAdmin):
    filter_horizontal = ['contracts', 'emploees']
    fields = [
        ('number', 'registered', 'event_date', ),
        ('type_order', 'template', ),
        ('structure', ),
        ( 'extract', 'scan', ),
        'contracts', 'emploees',
    ]
    # filter_vertical = ['emploees', ]

@admin.register(Protocols)
class ProtocolsAdmin(admin.ModelAdmin):
    filter_horizontal = ['questions',]

# Register your models here.
admin.site.register(Post)
admin.site.register(Grade)
admin.site.register(Degree)
admin.site.register(Structures)
admin.site.register(Diplomas)
admin.site.register(Order_types)
admin.site.register(Extracts)
admin.site.register(Sertificates)
admin.site.register(SurnameCases)
admin.site.register(Customers)
admin.site.register(Questions)
# admin.site.register(Protocols)
admin.site.register(Degree_works)
admin.site.register(Template_files)
admin.site.register(Access_lists)


 