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

from vehicles.models import *

class _vhc_admin(admin.ModelAdmin):

    list_display = ('sName', 'nBases', 'nStatus', "bLucas","bDrap", "nOrder")
    ordering = ["nOrder"]


    search_fields = ['sName']
    list_filter = [
        "sName",
        "nBases",
        "nStatus"
    ]


    #fields = ["sBases", "dtDate",'type', "user","nHour", "nMinutes", "sRemarques"]
    fieldsets = (
        ('Informations liées au véhicule', {
            'fields': ('sName', "nBases")
        }),
        ('Statut et équipement', {
            'fields': ('nStatus', 'bLucas',"bDrap")
        }),
        ('Informations diverses', {
            'fields': ("nOrder", "nType")
        }),
    )

class _vhc_infos_admin_form(forms.ModelForm):
    class Meta:
        model = Vehicles_infos
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super(_vhc_infos_admin_form, self).__init__(*args, **kwargs)
        if 'initial' in kwargs:
            initial = kwargs.get('initial', {})
            if 'Vehicle' in kwargs['initial']:
                vehicle_id = initial.get('Vehicle')
                if vehicle_id:
                   self.fields['Vehicle'].initial  = vehicle_id
            if 'nType' in kwargs['initial']:
                nType_value = initial.get('nType')
                if nType_value:
                   self.fields['nType'].initial  = nType_value
class _vhc_infos_admin(admin.ModelAdmin):
    form = _vhc_infos_admin_form
    always_show_username = True
    save_as = True
    list_display = ('Vehicle', 'nType', 'sDesc')

    list_filter = [
        "Vehicle",
        "nType",
        "bEnabled"
    ]



    search_fields = ['Vehicle']


    #fields = ["Vehicle", "nType",'sTitle', "sDesc","dtStart", "dtEnd", "Author"]
    fieldsets = (
        ('Informations liées au véhicule', {
            'fields': ('Vehicle',)
        }),
        ('Information sur le message', {
            'fields': ('nType','sDesc')
        }),
        ('Information supplémentaires et validité', {
            'fields': ("dtStart","dtEnd", 'Author', "bEnabled")
        }),
    )

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        field = super(_vhc_infos_admin, self).formfield_for_foreignkey(db_field, request, **kwargs)
        if db_field.remote_field.model == User:
            field.label_from_instance = self.get_user_label
        return field

    def formfield_for_manytomany(self, db_field, request=None, **kwargs):
        field = super(_vhc_infos_admin, self).formfield_for_manytomany(db_field, request, **kwargs)
        if db_field.remote_field.model == User:
            field.label_from_instance = self.get_user_label
        return field

    def get_user_label(self, user):
        name = user.get_full_name()
        username = user.username
        if not self.always_show_username:
            return name or username
        return (name and name != username and '%s (%s)' % (name, username) or username)
    def get_changeform_initial_data(self, request):
        """
        Return initial data for the admin form.
        """
        initial = super(_vhc_infos_admin, self).get_changeform_initial_data(request)
        get_data = request.GET.copy()
        
        # Pré-remplir le champ 'Vehicle'
        if 'Vehicle' in get_data:
            initial['Vehicle'] = get_data.get('Vehicle')
        
        # Pré-remplir le champ 'nType'
        if 'nType' in get_data:
            initial['nType'] = get_data.get('nType')
        
        return initial



admin.site.register(Vehicles_infos, _vhc_infos_admin)
admin.site.register(Vehicles, _vhc_admin)
