# -*- coding: utf-8 -*-

from django.contrib import admin
from pysnippets.strsnippets import strip

from guideline.models import ScreenAdminInfo
from mch.models import BrandInfo


class ScreenAdminInfoAdmin(admin.ModelAdmin):
    list_display = ('admin_id', 'nickname', 'brand_id', 'brand_name', 'user_status', 'status', 'created_at', 'updated_at')
    list_filter = ('brand_name', 'user_status', 'status')
    readonly_fields = ('admin_id', )
    search_fields = ('admin_id', 'unionid', 'openid', 'name', 'phone', 'location', 'brand_id', 'brand_name')

    def save_model(self, request, obj, form, change):
        obj.brand_id = strip(obj.brand_id)
        try:
            obj.brand_name = BrandInfo.objects.get(brand_id=obj.brand_id).brand_name
        except BrandInfo.DoesNotExist:
            obj.brand_name = ''

        obj.save()


admin.site.register(ScreenAdminInfo, ScreenAdminInfoAdmin)
