from rest_framework import serializers
from .models import LableMat
from apps.qm.models import NotOkOption
from apps.wpm.models import WmStateOption


class TidSerializer(serializers.Serializer):
    tid = serializers.CharField(label='表ID')


class LabelMatSerializer(serializers.ModelSerializer):
    material_name = serializers.StringRelatedField(source='material', read_only=True)
    material_origin_name = serializers.StringRelatedField(source='material_origin', read_only=True)
    supplier_name = serializers.CharField(source='supplier.name', read_only=True)
    defect_name = serializers.CharField(source="defect.name", read_only=True)
    notok_sign_name = serializers.SerializerMethodField()
    state_name = serializers.SerializerMethodField()
    class Meta:
        model = LableMat
        fields = '__all__'
    
    def get_notok_sign_name(self, obj):
        return getattr(NotOkOption, obj.notok_sign, NotOkOption.qt).label if obj.notok_sign else None
    
    def get_state_name(self, obj):
        return getattr(WmStateOption, str(obj.state), WmStateOption.OK).label if obj.state else None