# coding=utf-8
from django.conf import settings
from rest_framework import serializers

from apps.account.models import User
from apps.tenant.repair_order.models import RepairOrder, RepairOrderRecord, RepairOrderComment
from apps.upload.serializers import UploadSerializer
from utils.serializersfield import DelayTimeCharField


class RepairOrderDetailSerializer(serializers.ModelSerializer):
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    user_id = serializers.CharField(source='user.id', read_only=True)
    images = serializers.SerializerMethodField()
    repair_type_name = serializers.CharField(source='repair_type.name', read_only=True)
    create_at = DelayTimeCharField(source='create_time', read_only=True)

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

    class Meta:
        model = RepairOrder
        fields = (
            'id', 'status_text', 'status', 'no', 'fault_des', 'tel', 'name', 'create_at', 'device_address',
            'repair_type_name', 'images', 'user_id')


class RepairOrderListSerializer(serializers.ModelSerializer):
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    images = serializers.SerializerMethodField()
    user_id = serializers.CharField(source='user.id', read_only=True)
    repair_type_name = serializers.CharField(source='repair_type.name', read_only=True)
    create_at = DelayTimeCharField(source='create_time', read_only=True)
    start = serializers.SerializerMethodField()

    def get_start(self, obj):
        if obj.status == settings.APPRAISE:
            repair_order_comment = RepairOrderComment.objects.filter(repair_order=obj).first()
            if repair_order_comment:
                return repair_order_comment.start
        return 0

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

    class Meta:
        model = RepairOrder
        fields = (
            'id', 'status_text', 'no', 'fault_des', 'create_at', 'device_address','repair_type_name', 'name',
            'images', 'status', 'start', 'user_id')


class RepairOrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = RepairOrder
        fields = ('id', 'fault_des', 'tel', 'name', 'repair_type','device_name','device_address',)

    def create(self, validated_data):
        if 'images' in self.initial_data:
            validated_data['images'] = self.initial_data['images'].split(',')
        validated_data['user'] = self.context['request'].user
        validated_data['tenant'] = self.context['request'].user.employee.tenant
        instance = super(RepairOrderSerializer, self).create(validated_data)
        instance.no = instance.get_no()
        instance.save()
        RepairOrderRecord.objects.create(repair_order=instance, status=settings.NOT_CHECKED,
                                         user=self.context['request'].user, notes='报修下单')
        return instance


class RepairOrderCommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = RepairOrderComment
        fields = ('content', 'start', 'create_time')
