from apps.pm.models import ProductionPlan, SubProductionPlan, SubProductionProgress
from rest_framework import serializers
from apps.sam.serializers import OrderSerializer, OrderSimpleSerializer
from apps.mtm.serializers import MaterialSimpleSerializer, ProcessSimpleSerializer, SubProductionSimpleSerializer
from apps.system.serializers import OrganizationSimpleSerializer


class ProductionPlanCreateFromOrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProductionPlan
        fields = ['order', 'count', 'start_date', 'end_date']

class ProductionPlanSerializer(serializers.ModelSerializer):
    order_ = OrderSimpleSerializer(source='order', read_only=True)
    product_ = MaterialSimpleSerializer(source='product', read_only=True)
    class Meta:
        model = ProductionPlan
        fields ='__all__'

class ResourceCalSerializer(serializers.Serializer):
    id = serializers.IntegerField(label='产品ID')
    count = serializers.IntegerField(label='生产数量')

class ResourceCalListSerializer(serializers.ListSerializer):
    child = ResourceCalSerializer()

class SubProductionPlanListSerializer(serializers.ModelSerializer):
    workshop_ = OrganizationSimpleSerializer(source='workshop', read_only=True)
    process_ = ProcessSimpleSerializer(source='process', read_only=True)
    subproduction_ = SubProductionSimpleSerializer(source='subproduction', read_only=True)
    main_product_ = MaterialSimpleSerializer(source='main_product', read_only=True)
    product_ = serializers.SerializerMethodField()
    class Meta:
        model=SubProductionPlan
        fields = '__all__'
    
    def get_product_(self, obj):
        return MaterialSimpleSerializer(instance=obj.production_plan.product).data




class SubProductionPlanUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = SubProductionPlan
        fields = ['start_date', 'end_date']

class GenSubPlanSerializer(serializers.Serializer):
    pass

class SubProductionProgressSerializer(serializers.ModelSerializer):
    material_ = MaterialSimpleSerializer(source='material', read_only=True)
    class Meta:
        model = SubProductionProgress
        fields = '__all__'

class PickNeedSerializer(serializers.Serializer):
    warehouse = serializers.IntegerField(label="仓库ID")

class PlanDestorySerializer(serializers.Serializer):
    ids = serializers.ListField(child=serializers.IntegerField(), label='主计划ID列表')

class SubproductionPlanSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SubProductionPlan
        fields = ['id', 'number']
