# coding=utf-8

from rest_framework import serializers
from .models import *
from utils.booleancharfield import PriceShowCharField
from apps.base import Formater

class ConfigSerializer(serializers.ModelSerializer):

    class Meta:
        model = Config
        fields = '__all__'

class BalanceSerializer(serializers.ModelSerializer):
    create_user_text = serializers.CharField(source='create_user.username', read_only=True)
    balance = PriceShowCharField(read_only=True)
    date_joined_f = serializers.DateTimeField(source='create_user.date_joined', format=settings.SHORT_DATETIME_FORMAT,
                                              read_only=True)

    class Meta:
        model = Balance
        fields = '__all__'

class BalanceLogSerializer(serializers.ModelSerializer):
    create_time_f = serializers.DateTimeField(source='create_time', format=settings.SHORT_DATETIME_FORMAT,
                                              read_only=True)
    create_user_text = serializers.CharField(source='create_user.username', read_only=True)
    amount = PriceShowCharField(read_only=True)
    balance = serializers.SerializerMethodField()

    def get_balance(self, obj):
        balance = Balance.objects.filter(create_user=obj.create_user).first()
        return Formater.formatPriceShow(balance.balance)

    class Meta:
        model = BalanceLog
        fields = '__all__'

class PosterSerializer(serializers.ModelSerializer):
    create_user_name = serializers.CharField(source='create_user.employee.name', read_only=True)

    class Meta:
        model = Poster
        fields = '__all__'
