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

from django.contrib import admin
from django_admin import ChangeOnlyModelAdmin, ReadOnlyModelAdmin

from account.models import FranchiserInfo, SaleclerkInfo, UserInfo
from integral.models import SaleclerkIntegralIncomeExpensesInfo, SaleclerkSubmitLogInfo
from mch.models import ConsumeInfoSubmitLogInfo


class UserInfoAdmin(ChangeOnlyModelAdmin, admin.ModelAdmin):
    list_display = ('user_id', 'username', 'openid', 'unionid', 'name', 'sex', 'nickname', 'phone', 'location', 'balance', 'user_status', 'test_user', 'status', 'created_at', 'updated_at')
    list_filter = ('test_user', 'sex', 'user_status', 'status')
    readonly_fields = ('user_id', )
    search_fields = ('user_id', 'name', 'phone', 'location')

    def save_model(self, request, obj, form, change):
        obj.save()
        if obj.test_user:
            ConsumeInfoSubmitLogInfo.objects.filter(user_id=obj.user_id).update(test_user=True)


class FranchiserInfoAdmin(admin.ModelAdmin):
    list_display = ('franchiser_id', 'franchiser_name', 'franchiser_addr', 'franchiser_phone', 'franchiser_boss_name', 'franchiser_boss_phone', 'status', 'created_at', 'updated_at')
    list_filter = ('status', )
    readonly_fields = ('franchiser_id',)
    search_fields = ('franchiser_id', 'franchiser_name', 'franchiser_addr', 'franchiser_phone', 'franchiser_boss_name', 'franchiser_boss_phone')


class SaleclerkInfoAdmin(admin.ModelAdmin):
    list_display = ('franchiser_id', 'clerk_id', 'clerk_name', 'clerk_sex', 'clerk_phone', 'user_status', 'test_user', 'status', 'created_at', 'updated_at')
    list_filter = ('test_user', 'user_status', 'status')
    readonly_fields = ('franchiser_id', )
    search_fields = ('franchiser_id', 'clerk_id', 'clerk_name', 'clerk_phone')

    def save_model(self, request, obj, form, change):
        obj.save()
        if obj.test_user:
            SaleclerkIntegralIncomeExpensesInfo.objects.filter(franchiser_id=obj.franchiser_id).update(test_user=True)
            SaleclerkSubmitLogInfo.objects.filter(franchiser_id=obj.franchiser_id).update(test_user=True)


admin.site.register(UserInfo, UserInfoAdmin)
admin.site.register(FranchiserInfo, FranchiserInfoAdmin)
admin.site.register(SaleclerkInfo, SaleclerkInfoAdmin)
