from django.contrib import admin
from django.urls import path
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt

from accounts.models import Customers, CompanyUser, Company, CustomerDocument, Transactions
from accounts.views.logout import custom_logout_view
from qr_manager.models import Category, Batch


# Register your models here.
@admin.register(Customers)
class CustomersAdmin(admin.ModelAdmin):
    list_display = ('qr_code', "email", "first_name","last_name", "phone")
    # actions = [download_pdf]


class CustomAdminSite(admin.AdminSite):
    @method_decorator(csrf_exempt)
    def get_urls(self):
        urls = super().get_urls()
        custom_urls = [
            path('logout/', custom_logout_view, name='custom_logout'),
        ]
        return custom_urls + urls


custom_admin_site = CustomAdminSite(name='custom_admin')


class TransactionsAdmin(admin.ModelAdmin):
    exclude = ('transaction_updated_by',)
    list_display = ('company', 'amount', 'transaction_updated_by', 'created', 'updated')

    def save_model(self, request, obj, form, change):
        if not obj.pk:
            # Only set the user on creation
            obj.transaction_updated_by = request.user
        super().save_model(request, obj, form, change)
        

@admin.register(Batch)
class BatchAdmin(admin.ModelAdmin):
    list_display = ('batch_number','batch_name','category','count', 'color', 'bgcolor', 'created')
    

admin.site.register(Transactions, TransactionsAdmin)

admin.site.register(Category)
admin.site.register(CompanyUser)
admin.site.register(Company)
#admin.site.register(Batch)
admin.site.register(CustomerDocument)


# custom_admin_site.register(Category)
# custom_admin_site.register(CompanyUser)
# custom_admin_site.register(Company)
# custom_admin_site.register(Batch)