from django import forms
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from .models import UserProfile


# admin.py
class UserProfileForm(forms.ModelForm):

    class Meta:
        model = UserProfile
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['d_coin'].disabled = True  # 强制禁用字段编辑


class ProfileInline(admin.StackedInline):
    form = UserProfileForm
    model = UserProfile
    can_delete = False
    readonly_fields = ('d_coin', )
    verbose_name_plural = '扩展资料'
    fieldsets = (
        ('财产', {
            'fields': ('d_coin', ),
        }),
        ('基础信息', {
            'fields': ('gender', 'birth_date', 'avatar'),
            'classes': ('collapse', )
        }),
        ('联系信息', {
            'fields': ('mobile_phone', 'address_street', 'address_city',
                       'address_postcode'),
            'classes': ('collapse', )
        }),
        ('职业信息', {
            'fields': ('department', 'job_title', 'hire_date'),
            'classes': ('collapse', )
        }),
        ('社交信息', {
            'fields': ('wechat_id', 'dingtalk_id'),
            'classes': ('collapse', )
        }),
        ('系统信息', {
            'fields': (
                'timezone',
                'failed_login_attempts',
                'lockout_until',
            ),
            'classes': ('collapse', )
        }),
    )
    readonly_fields = ('created_at', 'updated_at')
    extra = 0
    min_num = 1
    max_num = 1


class CustomUserAdmin(UserAdmin):
    inlines = (ProfileInline, )
    list_display = ('id', 'username', 'email', 'is_staff', 'is_active',
                    'is_superuser')
    list_display_links = ('id', 'username')

    # def get_gender(self, obj):
    #     return obj.profile.gender if hasattr(obj, 'profile') else None

    # get_gender.short_description = '性别'


admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
