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

from curtail_uuid import CurtailUUID
from django.contrib import admin
from django.contrib.auth.hashers import make_password

from account.models import (LensmanIncomeExpensesInfo, LensmanInfo, LensmanLoginLogInfo, UserIncomeExpensesInfo,
                            UserInfo, UserLoginLogInfo)


class LensmanInfoAdmin(admin.ModelAdmin):
    readonly_fields = ('lensman_id', 'encryption', )
    list_display = ('lensman_id', 'username', 'name', 'sex', 'phone', 'location', 'proportion', 'status', 'created_at', 'updated_at')
    search_fields = ('name', 'phone', 'location')
    list_filter = ('sex', 'status')

    def save_model(self, request, obj, form, change):
        if not obj.lensman_id:
            obj.lensman_id = CurtailUUID.uuid(LensmanInfo, 'lensman_id')
        if obj.password:
            obj.encryption = make_password(obj.password, None, 'pbkdf2_sha256')
            obj.password = None
        obj.save()


class LensmanLoginLogInfoAdmin(admin.ModelAdmin):
    list_display = ('lensman_id', 'login_ip', 'login_result', 'status', 'created_at', 'updated_at')


class LensmanIncomeExpensesInfoAdmin(admin.ModelAdmin):
    list_display = ('lensman_id', 'photo_id', 'type', 'amount', 'balance', 'remark', 'status', 'created_at', 'updated_at')
    list_filter = ('type', 'status')


class UserInfoAdmin(admin.ModelAdmin):
    readonly_fields = ('user_id', )
    list_display = ('user_id', 'user_from', 'username', 'wx_uid', 'name', 'sex', 'nickname', 'phone', 'location', 'user_status', 'status', 'created_at', 'updated_at')
    search_fields = ('name', 'phone', 'location')
    list_filter = ('user_from', 'sex', 'user_status', 'status')


class UserLoginLogInfoAdmin(admin.ModelAdmin):
    list_display = ('user_id', 'login_ip', 'login_result', 'status', 'created_at', 'updated_at')


class UserIncomeExpensesInfoAdmin(admin.ModelAdmin):
    list_display = ('user_id', 'photo_id', 'type', 'amount', 'balance', 'remark', 'status', 'created_at', 'updated_at')
    list_filter = ('type', 'status')


admin.site.register(LensmanInfo, LensmanInfoAdmin)
admin.site.register(LensmanLoginLogInfo, LensmanLoginLogInfoAdmin)
admin.site.register(LensmanIncomeExpensesInfo, LensmanIncomeExpensesInfoAdmin)
admin.site.register(UserInfo, UserInfoAdmin)
admin.site.register(UserLoginLogInfo, UserLoginLogInfoAdmin)
admin.site.register(UserIncomeExpensesInfo, UserIncomeExpensesInfoAdmin)
