# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin
from .models import *

# Register your models here.
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin

# from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import User
from django.utils.translation import ugettext_lazy as _


class UserAdmin(UserAdmin):
    # 重写fieldsets在admin后台加入自己新增的字段
    fieldsets = (
        (None, {'fields': ('company', 'username', 'password')}),
        # (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        # (_('Important dates'), {'fields': ('last_login', 'date_joined')}),

    )
    list_display = ('username', 'company', 'is_staff')
admin.site.register(User, UserAdmin)


class LevelAdmin(admin.ModelAdmin):
    list_display = ('name', 'sn', 'category', 'remark')
    search_fields = ('name',)
admin.site.register(Level, LevelAdmin)


class CompanyAdmin(admin.ModelAdmin):
    list_display = ('name', 'category', 'level', 'parent', 'legacy_id', 'hierarchy', 'external_code', 'external_id', 'tm')
    list_filter = ('level',)
    search_fields = ('name',)
admin.site.register(Company, CompanyAdmin)


class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'remark')
    # list_filter = ('company',)
    search_fields = ('name',)
admin.site.register(Category, CategoryAdmin)


class CategoryPropAdmin(admin.ModelAdmin):
    list_display = ('name', 'desc_cn', 'category', 'unit', 'type', 'remark')
    list_filter = ('category',)
    # search_fields = ('name', 'company')
admin.site.register(CategoryProp, CategoryPropAdmin)


class PrdWorkshopAdmin(admin.ModelAdmin):
    list_display = ('name', 'company', 'legacy_id', 'province', 'city', 'address')
    list_filter = ('company',)

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        if db_field.name == "company" and request:
            kwargs["queryset"] = Company.objects.filter(has_plant=True)
        return super(PrdWorkshopAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

    # search_fields = ('name',)
admin.site.register(PrdWorkshop, PrdWorkshopAdmin)


class PrdLineAdmin(admin.ModelAdmin):
    list_display = ('name', 'company', 'legacy_id', 'prd_plant', 'prd_workshop')
    list_filter = ('prd_plant',)

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        if db_field.name == "company" and request:
            kwargs["queryset"] = Company.objects.filter(has_plant=True)
        return super(PrdLineAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

    # search_fields = ('name',)
admin.site.register(PrdLine, PrdLineAdmin)


class PrdQcAdmin(admin.ModelAdmin):
    list_display = ('name', 'company', 'legacy_id', 'remark')
    list_filter = ('company',)
    # search_fields = ('name',)

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        if db_field.name == "company" and request:
            kwargs["queryset"] = Company.objects.filter(has_plant=True)
        return super(PrdQcAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

admin.site.register(PrdQc, PrdQcAdmin)


class PrdPlantAdmin(admin.ModelAdmin):
    list_display = ('name', 'company', 'legacy_id', 'remark')
    list_filter = ('company',)

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        if db_field.name == "company" and request:
            kwargs["queryset"] = Company.objects.filter(has_plant=True)
        return super(PrdPlantAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

    # search_fields = ('name',)
admin.site.register(PrdPlant, PrdPlantAdmin)


class PrdTeamAdmin(admin.ModelAdmin):
    list_display = ('name', 'company', 'legacy_id', 'remark')
    list_filter = ('company',)

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        if db_field.name == "company" and request:
            kwargs["queryset"] = Company.objects.filter(has_plant=True)
        return super(PrdTeamAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

    # search_fields = ('name',)
admin.site.register(PrdTeam, PrdTeamAdmin)


#2020-04-23
class ErrCategoryAdmin(admin.ModelAdmin):
    list_display = ('name',  'remark')
    search_fields = ('name',)
admin.site.register(ErrCategory, ErrCategoryAdmin)

class ErrCodeAdmin(admin.ModelAdmin):
    list_display = ('name', 'code', 'category','remark')
    search_fields = ('name',)
admin.site.register(ErrCode, ErrCodeAdmin)

