from django.contrib import admin
from .models import ExternalFinishedProduct, Shipment, SalesItem


class SalesItemInline(admin.TabularInline):
    model = SalesItem
    extra = 1
    fields = ['name', 'quantity', 'unit', 'position', 'remark', 'printing_job_id', 'customer_id', 'created_by']
    readonly_fields = ['created_by']


class ExternalFinishedProductInline(admin.TabularInline):
    model = ExternalFinishedProduct
    extra = 1
    fields = ['style_name', 'num_of_rolls', 'remark', 'created_by', 'created_at']
    readonly_fields = ['created_by', 'created_at']


@admin.register(Shipment)
class ShipmentAdmin(admin.ModelAdmin):
    list_display = ['id', 'merchant', 'customer', 'shipment_date', 'status', 'external_id', 'items_count', 'created_by', 'created_at']
    list_filter = ['merchant', 'status', 'shipment_date', 'created_at']
    search_fields = ['customer__name', 'remark', 'external_id']
    readonly_fields = ['created_at', 'updated_at', 'created_by']
    date_hierarchy = 'shipment_date'
    inlines = [SalesItemInline, ExternalFinishedProductInline]
    
    def items_count(self, obj):
        return obj.items.count()
    items_count.short_description = '销售品数量'
    
    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user
        super().save_model(request, obj, form, change)
    
    def save_formset(self, request, form, formset, change):
        instances = formset.save(commit=False)
        for instance in instances:
            if isinstance(instance, (SalesItem, ExternalFinishedProduct)) and not instance.pk:
                instance.created_by = request.user
            instance.save()
        formset.save_m2m()


@admin.register(SalesItem)
class SalesItemAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'quantity', 'unit', 'position', 'shipment_status', 'printing_job_id', 'customer_id', 'created_by', 'created_at']
    list_filter = ['unit', 'created_at', ('shipment', admin.EmptyFieldListFilter)]
    search_fields = ['name', 'shipment__customer__name', 'position', 'remark']
    readonly_fields = ['created_at', 'updated_at', 'created_by']
    raw_id_fields = ['shipment']
    
    def shipment_status(self, obj):
        if obj.shipment:
            return f'出货单 #{obj.shipment.id}'
        return '待分配'
    shipment_status.short_description = '出货单'
    
    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user
        super().save_model(request, obj, form, change)


@admin.register(ExternalFinishedProduct)
class ExternalFinishedProductAdmin(admin.ModelAdmin):
    list_display = ['id', 'shipment', 'style_name', 'num_of_rolls', 'remark', 'created_by', 'created_at']
    list_filter = ['created_at', ('shipment', admin.EmptyFieldListFilter)]
    search_fields = ['style_name', 'remark', 'shipment__external_id', 'shipment__customer__name']
    readonly_fields = ['created_at', 'updated_at', 'created_by']
    raw_id_fields = ['shipment']

    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user
        super().save_model(request, obj, form, change)
