# coding=utf-8

from rest_framework import serializers

from .models import RepairOrder,RepairOrderFinish
from apps.upload.serializers import UploadSerializer

class RepairOrderSerializer(serializers.ModelSerializer):
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    user_username = serializers.CharField(source='user.username', read_only=True)
    repair_type = serializers.CharField(source='repair_type.name', read_only=True)
    images = serializers.SerializerMethodField()

    def get_images(self, obj):
        return UploadSerializer(obj.images, many=True).data

    class Meta:
        model = RepairOrder
        fields = (
            'id', 'status','status_text', 'no', 'fault_des', 'name', 'tel', 'create_time', 'device_name', 'device_address',
            'repair_type', 'user', 'user_username', 'images')

class RepairOrderFinishSerializer(serializers.ModelSerializer):
    fault_cause_text = serializers.CharField(source='fault_cause.name', read_only=True)
    images = serializers.SerializerMethodField()

    def get_images(self, obj):
        return UploadSerializer(obj.images, many=True).data

    class Meta:
        model = RepairOrderFinish
        fields = (
            'id', 'fault_cause_text','content', 'images')
