from django.contrib import admin

from . import models


@admin.register(models.PurchaseOrder)
class PurchaseOrderAdmin(admin.ModelAdmin):
    list_display = ('id', 'supplier', 'purchase_date', 'operator', 'status', '_total_amount', '_diff_quantity', '_total_quantity', 'created_at')
    search_fields = ('supplier__name',)
    list_filter = ('operator', 'warehouse', 'status', 'created_at')
    ordering = ('-created_at',)

    @admin.display(description='总价')
    def _total_amount(self, obj: models.PurchaseOrder):
        return obj.get_total_amount()

    @admin.display(description='差异数量')
    def _diff_quantity(self, obj: models.PurchaseOrder):
        return obj.get_total_diff_quantity()

    @admin.display(description='总数量')
    def _total_quantity(self, obj: models.PurchaseOrder):
        return obj.get_total_quantity()


@admin.register(models.PurchaseOrderItem)
class PurchaseOrderItemAdmin(admin.ModelAdmin):
    list_display = (
        'id', 'purchase_order', 'product', 
        'quantity', 'unit', 'price', 'batch_number',
        '_total_amount', 'empty_diff_percent', 
        '_real_quantity', '_diff_quantity',
        'num_of_rolls', 
    )
    search_fields = ('purchase_order__id', 'product__name')
    list_filter = ('purchase_order__operator', 'purchase_order__warehouse', 'created_at')
    ordering = ('-created_at',)

    @admin.display(description='实际数量')
    def _real_quantity(self, obj: models.PurchaseOrderItem):
        return obj.real_quantity()

    @admin.display(description='差异数量')
    def _diff_quantity(self, obj: models.PurchaseOrderItem):
        return obj.diff_quantity()

    @admin.display(description='总金额')
    def _total_amount(self, obj: models.PurchaseOrderItem):
        return obj.total_amount()


@admin.register(models.PurchaseReturnOrder)
class PurchaseReturnOrderAdmin(admin.ModelAdmin):
    list_display = ('id', 'supplier', 'return_date', 'operator', 'status', '_total_amount', 'created_at')
    search_fields = ('supplier__name',)
    list_filter = ('operator', 'warehouse', 'status', 'created_at')
    ordering = ('-created_at',)

    @admin.display(description='总价')
    def _total_amount(self, obj: models.PurchaseReturnOrder):
        return obj.get_total_amount()


@admin.register(models.PurchaseReturnOrderItem)
class PurchaseReturnOrderItemAdmin(admin.ModelAdmin):
    list_display = (
        'id', 'purchase_return_order', 'product',
        'quantity', 'unit', 'price', 'batch_number',
        '_total_amount', 'empty_diff_percent',
        '_real_quantity', '_diff_quantity',
        'num_of_rolls',
    )
    search_fields = ('purchase_return_order__id', 'product__name')
    list_filter = ('purchase_return_order__operator', 'purchase_return_order__warehouse', 'created_at')
    ordering = ('-created_at',)

    @admin.display(description='实际数量')
    def _real_quantity(self, obj: models.PurchaseReturnOrderItem):
        return obj.real_quantity()

    @admin.display(description='差异数量')
    def _diff_quantity(self, obj: models.PurchaseReturnOrderItem):
        return obj.diff_quantity()

    @admin.display(description='总金额')
    def _total_amount(self, obj: models.PurchaseReturnOrderItem):
        return obj.total_amount()


@admin.register(models.SalesReturnOrder)
class SalesReturnOrderAdmin(admin.ModelAdmin):
    list_display = ('id', 'customer', 'return_date', 'operator', 'status', '_total_amount', 'created_at')
    search_fields = ('customer__name',)
    list_filter = ('operator', 'warehouse', 'status', 'created_at')
    ordering = ('-created_at',)

    @admin.display(description='总价')
    def _total_amount(self, obj: models.SalesReturnOrder):
        return obj.get_total_amount()


@admin.register(models.SalesReturnOrderItem)
class SalesReturnOrderItemAdmin(admin.ModelAdmin):
    list_display = (
        'id', 'sales_return_order', 'product',
        'quantity', 'unit', 'price', 'batch_number',
        '_total_amount', 'empty_diff_percent',
        '_real_quantity', '_diff_quantity',
        'num_of_rolls',
    )
    search_fields = ('sales_return_order__id', 'product__name')
    list_filter = ('sales_return_order__operator', 'sales_return_order__warehouse', 'created_at')
    ordering = ('-created_at',)

    @admin.display(description='实际数量')
    def _real_quantity(self, obj: models.SalesReturnOrderItem):
        return obj.real_quantity()

    @admin.display(description='差异数量')
    def _diff_quantity(self, obj: models.SalesReturnOrderItem):
        return obj.diff_quantity()

    @admin.display(description='总金额')
    def _total_amount(self, obj: models.SalesReturnOrderItem):
        return obj.total_amount()


@admin.register(models.SalesOrder)
class SalesOrderAdmin(admin.ModelAdmin):
    list_display = ('id', 'human_id', 'customer', 'sales_date', 'operator', 'status', '_total_amount', '_diff_quantity', '_total_quantity', 'created_at')
    search_fields = ('customer__name',)
    list_filter = ('operator', 'warehouse', 'status', 'created_at')
    ordering = ('-created_at',)

    @admin.display(description='总价')
    def _total_amount(self, obj: models.SalesOrder):
        return obj.get_total_amount()

    @admin.display(description='差异数量')
    def _diff_quantity(self, obj: models.SalesOrder):
        return obj.get_total_diff_quantity()

    @admin.display(description='总数量')
    def _total_quantity(self, obj: models.SalesOrder):
        return obj.get_total_quantity()


@admin.register(models.SalesOrderItem)
class SalesOrderItemAdmin(admin.ModelAdmin):
    list_display = (
        'id', 'sales_order', 'product',
        'quantity', 'unit', 'price', 'batch_number',
        'quantity_of_rolls', '_total_amount', 
        'empty_diff_percent', '_real_quantity', 
        '_diff_quantity', 'num_of_rolls',
    )
    search_fields = ('sales_order__id', 'product__name')
    list_filter = ('sales_order__operator', 'sales_order__warehouse', 'created_at')
    ordering = ('-created_at',)

    @admin.display(description='实际数量')
    def _real_quantity(self, obj: models.SalesOrderItem):
        return obj.real_quantity()

    @admin.display(description='差异数量')
    def _diff_quantity(self, obj: models.SalesOrderItem):
        return obj.diff_quantity()

    @admin.display(description='总金额')
    def _total_amount(self, obj: models.SalesOrderItem):
        return obj.total_amount()


@admin.register(models.PaymentOrder)
class PaymentOrderAdmin(admin.ModelAdmin):
    list_display = (
        'id', 'supplier', 'payment_date', 'amount', 'operator', 'status',
        'is_external_source', 'external_source_id', 'created_at',
    )
    search_fields = ('supplier__name', 'external_source_id')
    list_filter = ('operator', 'status', 'is_external_source', 'created_at')
    ordering = ('-created_at',)


@admin.register(models.ReceiptOrder)
class ReceiptOrderAdmin(admin.ModelAdmin):
    list_display = (
        'id', 'customer', 'receipt_date', 'amount', 'operator', 'status',
        'is_external_source', 'external_source_id', 'created_at',
    )
    search_fields = ('customer__name', 'external_source_id')
    list_filter = ('operator', 'status', 'is_external_source', 'created_at')
    ordering = ('-created_at',)


@admin.register(models.CustomerBalance)
class CustomerBalancemAdmin(admin.ModelAdmin):
    list_display = ('id', 'customer', 'balance', 'created_at')
    search_fields = ('customer_balance__id', 'product__name')
    ordering = ('-created_at',)


@admin.register(models.SupplierBalance)
class SupplierBalanceAdmin(admin.ModelAdmin):
    list_display = ('id', 'supplier', 'balance', 'created_at')
    search_fields = ('supplier__name',)
    ordering = ('-created_at',)


@admin.register(models.BalanceChangeRecord)
class BalanceChangeRecordAdmin(admin.ModelAdmin):
    list_display = (
        'id', 'merchant', 'target_type', 'target',
        'source_type', 'source_id', 'delta', 
        'balance_before', 'balance_after', 
        'direction', 'created_at',
        )
    search_fields = ('merchant__name', 'target_type', 'source_type', 'source_id')
    list_filter = ('merchant', 'target_type', 'source_type', 'direction', 'created_at')
    ordering = ('-created_at',)

    @admin.display(description='供应商/客户')
    def target(self, obj: models.BalanceChangeRecord):
        if obj.target_type == models.BalanceChangeTargetEnum.SUPPLIER:
            return obj.supplier.name
        elif obj.target_type == models.BalanceChangeTargetEnum.CUSTOMER:
            return obj.customer.name
        return '-'
    @admin.display(description='商户')
    def merchant(self, obj: models.BalanceChangeRecord):
        return obj.merchant.name

    @admin.display(description='目标类型')
    def target_type(self, obj: models.BalanceChangeRecord):
        return obj.target_type.label

    @admin.display(description='来源类型')
    def source_type(self, obj: models.BalanceChangeRecord):
        return obj.source_type.label