from django.contrib import admin


"""
设置后台管理的全局参数
"""

admin.site.site_header = '诊断应用后台'
admin.site.site_title = '诊断服务端'
admin.site.index_title = '诊断服务端'

"""
设置后台管理的全局参数
结束
"""
from django.urls import reverse
from django.utils.html import format_html

# Register your models here.
from . models import EcUser, Subject, SchoolInfo
from django import forms

class UcUserInfoForm(forms.ModelForm):
    class Meta:
        model = EcUser
        fields = '__all__'
        widgets = {
            'subjects': forms.CheckboxSelectMultiple,  # 使用复选框小部件
        }

@admin.register(EcUser)
class UcUserInfo(admin.ModelAdmin):
    # 在列表视图中显示哪些字段
    list_display = ('username','id', 'gender', 'dob', 'political_status', 'custom_link')

    # 通过 fieldsets 分组字段并定义显示顺序
    fieldsets = (
        (None, {
            'fields': ('username', 'password')  # 先显示用户名和密码
        }),
        ('Personal Information', {
            'fields': ('name', 'gender', 'age', 'ethnicity', 'dob', 'id_card_number', 'political_status', 'subjects', 'subject_usage_count')
        }),
    )

    # 还可以通过 'search_fields' 添加搜索功能
    search_fields = ('name', 'id_card_number')

    # 可以设置过滤选项，方便后台管理
    list_filter = ('gender', 'political_status')

    # 可选：设置只读字段
    # readonly_fields = ('id_card_number',)  # 设置身份证号为只读字段
    form = UcUserInfoForm  # 设置使用自定义表单

    def custom_link(self, obj):
        # 为每个用户添加自定义链接
        url = reverse('all_info_form', args=[obj.pk])  # 生成链接
        return format_html('<a href="{}">查看</a>', url)  # 创建 HTML 链接

    custom_link.short_description = '详细信息'  # 设置列标题



admin.site.register(Subject)
admin.site.register(SchoolInfo)