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

import monetary
from django.conf import settings
from django.contrib import admin
from django.contrib.auth.hashers import make_password
from django_admin import AdvancedActionsModelAdmin, AdvancedExportExcelModelAdmin, ReadOnlyModelAdmin
from django_models_ext import ProvinceShortModelMixin
from pysnippets.strsnippets import strip

from logs.models import MchInfoEncryptLogInfo
from mch.models import (ActivityInfo, AdministratorInfo, BrandInfo, ConsumeInfoSubmitLogInfo, ConsumeImpressionInfo, DistributorInfo,
                        LatestAppInfo, LatestAppScreenInfo, ModelInfo, OperatorInfo, SaleclerkInfo, SlideInfo)


class AdministratorInfoAdmin(admin.ModelAdmin):
    list_display = ('admin_id', 'admin_type', 'phone', 'password', 'encryption', 'name', 'brand_id', 'brand_name', 'user_status', 'status', 'created_at', 'updated_at')
    list_filter = ('admin_type', 'user_status', 'status', 'brand_name')
    readonly_fields = ('encryption', 'brand_name')

    def save_model(self, request, obj, form, change):
        obj.phone = strip(obj.phone)
        obj.password = strip(obj.password)
        if obj.password:
            obj.encryption = make_password(obj.password, settings.MAKE_PASSWORD_SALT, settings.MAKE_PASSWORD_HASHER)
        obj.password = ''

        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()


class OperatorInfoAdmin(admin.ModelAdmin):
    list_display = ('operator_id', 'phone', 'name', 'brand_id', 'brand_name', 'brand_domain', 'user_status', 'status', 'created_at', 'updated_at')
    list_filter = ('brand_name', 'user_status', 'status')
    readonly_fields = ('encryption', 'brand_name')

    def save_model(self, request, obj, form, change):
        obj.phone = strip(obj.phone)
        obj.password = strip(obj.password)
        if obj.password:
            obj.encryption = make_password(obj.password, settings.MAKE_PASSWORD_SALT, settings.MAKE_PASSWORD_HASHER)
        obj.password = ''

        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()


class BrandInfoAdmin(admin.ModelAdmin):
    list_display = ('brand_id', 'brand_name', 'tousername', 'brand_descr', 'brand_logo', 'brand_domain', 'clerk_direct_submit', 'position', 'status', 'created_at', 'updated_at')
    list_filter = ('clerk_direct_submit', 'status')

    def save_model(self, request, obj, form, change):
        obj.save()
        OperatorInfo.objects.filter(brand_id=obj.brand_id).update(brand_domain=obj.brand_domain)


class ModelInfoAdmin(admin.ModelAdmin):
    list_display = ('brand_id', 'brand_name', 'jancode', 'model_id', 'model_name', 'model_uni_name', 'model_full_name', 'model_descr', 'category', 'warehouse', 'image', 'url', 'image2', 'factory_yuan', 'integral', 'is_send_redpack', 'redpack_amount', 'redpack_max_amount', 'is_clerk_send_redpack', 'clerk_redpack_amount', 'clerk_redpack_max_amount', 'position', 'display', 'status', 'created_at', 'updated_at')
    list_filter = ('brand_name', 'category', 'warehouse', 'display', 'status')
    readonly_fields = ('brand_name', 'factory_fee')
    search_fields = ('brand_id', 'brand_name', 'jancode', 'model_id', 'model_name', 'model_uni_name', 'model_full_name', 'model_descr', 'category', 'warehouse')

    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.factory_fee = monetary.Yuan2Fen(obj.factory_yuan)
        obj.factory_yuan = monetary.Fen2Yuan(obj.factory_fee)

        obj.save()

        MchInfoEncryptLogInfo.objects.filter(has_send_redpack=False).update(is_send_redpack=obj.is_send_redpack, redpack_amount=obj.redpack_amount, redpack_max_amount=obj.redpack_max_amount)
        MchInfoEncryptLogInfo.objects.filter(has_clerk_send_redpack=False).update(is_clerk_send_redpack=obj.is_clerk_send_redpack, clerk_redpack_amount=obj.clerk_redpack_amount, clerk_redpack_max_amount=obj.clerk_redpack_max_amount)


class ModelImageInfoAdmin(admin.ModelAdmin):
    list_display = ('model_id', 'model_name', 'image', 'url', 'position', 'status', 'created_at', 'updated_at')
    list_filter = ('model_name', 'status')

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

        obj.save()


class DistributorInfoAdmin(admin.ModelAdmin):
    list_display = ('brand_id', 'brand_name', 'distributor_id', 'distributor_name', 'distributor_short_name', 'distributor_province_name', 'position', 'status', 'created_at', 'updated_at')
    list_filter = ('brand_name', 'sr_id', 'distributor_province_name', 'status')
    readonly_fields = ('brand_name', 'distributor_province_code')
    search_fields = ('brand_id', 'brand_name', 'distributor_id', 'distributor_name', 'distributor_short_name', 'distributor_descr', 'distributor_province_code', 'distributor_province_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.distributor_province_code = ProvinceShortModelMixin.PROVINCE_NAME_CODE_DICT.get(obj.distributor_province_name, '')

        obj.save()


class SaleclerkInfoAdmin(AdvancedExportExcelModelAdmin, AdvancedActionsModelAdmin, admin.ModelAdmin):
    list_display = ('brand_id', 'brand_name', 'distributor_id', 'distributor_name', 'clerk_id', 'clerk_name', 'clerk_sex', 'clerk_phone', 'unionid', 'openid', 'num', 'integral', 'total_integral', 'user_status', 'test_user', 'status', 'created_at', 'updated_at')
    list_filter = ('test_user', 'user_status', 'status')
    search_fields = ('brand_id', 'brand_name', 'distributor_id', 'distributor_name', 'clerk_id', 'clerk_name', 'clerk_phone', 'unionid', 'openid')
    actions_exclude = ('delete_selected', )


class BrandModelDistributorPriceInfoAdmin(admin.ModelAdmin):
    list_display = ('brand_id', 'brand_name', 'model_id', 'model_name', 'distributor_id', 'distributor_name', 'factory_yuan', 'integral', 'status', 'created_at', 'updated_at')
    list_filter = ('brand_name', 'model_name', 'distributor_name', 'status')
    readonly_fields = ('brand_name', 'model_name', 'distributor_name', 'factory_fee', )
    search_fields = ('brand_id', 'brand_name', 'model_id', 'model_name', 'distributor_id', 'distributor_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.model_id = strip(obj.model_id)
        try:
            obj.model_name = ModelInfo.objects.get(model_id=obj.model_id).model_name
        except BrandInfo.DoesNotExist:
            obj.model_name = ''

        obj.distributor_id = strip(obj.distributor_id)
        try:
            obj.distributor_name = DistributorInfo.objects.get(distributor_id=obj.distributor_id).distributor_name
        except BrandInfo.DoesNotExist:
            obj.distributor_name = ''

        obj.factory_fee = monetary.Yuan2Fen(obj.factory_yuan)
        obj.factory_yuan = monetary.Fen2Yuan(obj.factory_fee)

        obj.save()


class LatestAppInfoAdmin(admin.ModelAdmin):
    list_display = ('latest_adr_version_code', 'latest_adr_version_name', 'latest_adr_app', 'latest_adr_url', 'status', 'created_at', 'updated_at')
    list_filter = ('status', )
    readonly_fields = ('status', )

    def save_model(self, request, obj, form, change):
        if not obj.pk and LatestAppInfo.objects.filter(status=True).exists():
            return
        obj.latest_adr_version_name = strip(obj.latest_adr_version_name)
        obj.latest_adr_app = strip(obj.latest_adr_app)
        obj.latest_adr_url = strip(obj.latest_adr_url)
        obj.save()


class LatestAppScreenInfoAdmin(admin.ModelAdmin):
    list_display = ('latest_adr_version_code', 'latest_adr_version_name', 'latest_adr_app', 'latest_adr_url', 'status', 'created_at', 'updated_at')
    list_filter = ('status', )
    readonly_fields = ('status', )

    def save_model(self, request, obj, form, change):
        if not obj.pk and LatestAppInfo.objects.filter(status=True).exists():
            return
        obj.latest_adr_version_name = strip(obj.latest_adr_version_name)
        obj.latest_adr_app = strip(obj.latest_adr_app)
        obj.latest_adr_url = strip(obj.latest_adr_url)
        obj.save()


class ConsumeImpressionInfoAdmin(ReadOnlyModelAdmin, admin.ModelAdmin):
    list_display = ('clerk_id', 'brand_id', 'model_id', 'serialNo', 'sex', 'rage', 'identity', 'purpose', 'status', 'created_at', 'updated_at')
    list_filter = ('sex', 'rage', 'status')


class ConsumeInfoSubmitLogInfoAdmin(ReadOnlyModelAdmin, admin.ModelAdmin):
    list_display = ('user_id', 'phone', 'lat', 'lon', 'serialNo', 'verifyResult', 'dupload', 'test_user', 'submit_during_activity', 'activity_id', 'coupon_expire_at', 'coupon_value', 'has_used', 'status', 'created_at', 'updated_at')
    list_filter = ('brand_id', 'submit_during_activity', 'activity_id', 'dupload', 'test_user', 'verifyResult', 'has_used', 'status')
    search_fields = ('user_id', 'phone', 'serialNo')


class ActivityInfoAdmin(admin.ModelAdmin):
    list_display = ('activity_name', 'model_uni_names', 'start_at', 'end_at', 'coupon_expire_type', 'coupon_valid_period', 'coupon_expire_at', 'coupon_value', 'status', 'created_at', 'updated_at')
    list_filter = ('coupon_expire_type', 'status')


class SlideInfoAdmin(admin.ModelAdmin):
    list_display = ('brand_id', 'image', 'url', 'position', 'status', 'created_at', 'updated_at')
    list_filter = ('brand_id', 'status')


admin.site.register(AdministratorInfo, AdministratorInfoAdmin)
admin.site.register(OperatorInfo, OperatorInfoAdmin)
admin.site.register(BrandInfo, BrandInfoAdmin)
admin.site.register(ModelInfo, ModelInfoAdmin)
# admin.site.register(ModelImageInfo, ModelImageInfoAdmin)
admin.site.register(DistributorInfo, DistributorInfoAdmin)
admin.site.register(SaleclerkInfo, SaleclerkInfoAdmin)
# admin.site.register(BrandModelDistributorPriceInfo, BrandModelDistributorPriceInfoAdmin)
admin.site.register(LatestAppInfo, LatestAppInfoAdmin)
admin.site.register(LatestAppScreenInfo, LatestAppScreenInfoAdmin)
admin.site.register(ConsumeImpressionInfo, ConsumeImpressionInfoAdmin)
admin.site.register(ConsumeInfoSubmitLogInfo, ConsumeInfoSubmitLogInfoAdmin)
admin.site.register(ActivityInfo, ActivityInfoAdmin)
admin.site.register(SlideInfo, SlideInfoAdmin)
