# coding=utf-8

from rest_framework import serializers
from apps.base import Formater
from .models import ProductOrder

class ProductOrderSerializer(serializers.ModelSerializer):
    product_name = serializers.CharField(source='product.name', read_only=True)
    name = serializers.CharField(source='customer.name', read_only=True)
    customer_name = serializers.CharField(source='customer_address.name', read_only=True)
    customer_tel = serializers.CharField(source='customer_address.tel', read_only=True)
    area = serializers.CharField(source='customer_address.area', read_only=True)
    addr = serializers.CharField(source='customer_address.addr', read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    price = serializers.SerializerMethodField()
    amount = serializers.SerializerMethodField()

    def get_price(self, obj):
        return Formater.formatPriceShow(obj.price)

    def get_amount(self, obj):
        return Formater.formatAmountShow(obj.amount)

    class Meta:
        model = ProductOrder
        fields = '__all__'

    def validate(self, attrs):
        if 'price' in attrs:
            attrs['price'] = Formater.formatPrice(attrs['price'])
        return attrs

