# coding=utf-8
import datetime
from rest_framework import serializers
from .models import *
from django.conf import settings
from utils.booleancharfield import BooleanCharField

class ProductSerializer(serializers.ModelSerializer):
    enable_text = BooleanCharField(source='enable')
    create_user_text = serializers.CharField(source='create_user.username', read_only=True)
    create_time_f = serializers.DateTimeField(source='create_time', format=settings.SHORT_DATETIME_FORMAT,
                                              read_only=True)
    main_image_url = serializers.CharField(source='main_image.picture', read_only=True)
    detail_image = serializers.SerializerMethodField()

    def get_detail_image(self, obj):
        count = ProductImages.objects.filter(product=obj).count()
        return count

    class Meta:
        model = Product
        fields = '__all__'

    def create(self, validated_data):
        validated_data['create_user'] = self.context['request'].user
        instance = super(ProductSerializer, self).create(validated_data)
        instance.save()
        return instance

    def update(self, instance, validated_data):
        instance = super(ProductSerializer, self).update(instance, validated_data)
        instance.save()
        return instance

class ProductListSerializer(serializers.ModelSerializer):
    mail_image = serializers.SerializerMethodField()
    details_image = serializers.SerializerMethodField()

    def get_mail_image(self, obj):
        return '%s%s' % (settings.SERVER_DOMAIN, obj.main_image.picture)

    def get_details_image(self, obj):
        data = []
        images = ProductImages.objects.filter(product=obj)
        for image in images:
            data.append('%s%s' % (settings.SERVER_DOMAIN, image.image.picture))
        return data

    class Meta:
        model = Product
        fields = ('id','name','month','mail_image','details_image',)

class OrderListSerializer(serializers.ModelSerializer):
    seller_text = serializers.CharField(source='seller.name', read_only=True)
    create_user_text = serializers.CharField(source='create_user.name', read_only=True)
    create_time_f = serializers.DateTimeField(source='create_time', format=settings.SHORT_DATETIME_FORMAT,
                                              read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    sex_text = serializers.CharField(source='get_sex_display', read_only=True)
    laompany_text = serializers.CharField(source='laompany.name', read_only=True)

    class Meta:
        model = Order
        fields = ('id','name','tel','sex_text','inumber','laompany_text','status_text','status','create_time_f',
                  'create_user_text','seller_text','cancel_reason',)

class OrderSerializer(serializers.ModelSerializer):
    create_user_text = serializers.CharField(source='create_user.name', read_only=True)
    laompany_text = serializers.CharField(source='laompany.name', label='产品名称', read_only=True)
    create_time_f = serializers.DateTimeField(source='create_time', format=settings.SHORT_DATETIME_FORMAT,
                                              read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    category_text = serializers.CharField(source='get_category_display', read_only=True)
    sex_text = serializers.CharField(source='get_sex_display', label='性别', read_only=True)
    rpr_text = serializers.CharField(source='get_rpr_display', label='户口', read_only=True)
    nationality_text = serializers.CharField(source='get_nationality_display', label='国籍/地区', read_only=True)
    ntype_text = serializers.CharField(source='get_ntype_display', label='身份证件类型', read_only=True)
    elevel_text = serializers.CharField(source='get_elevel_display', label='教育程度', read_only=True)
    hptype_text = serializers.CharField(source='get_hptype_display', label='现居住房产类型', read_only=True)
    maddress_text = serializers.CharField(source='get_maddress_display', label='邮寄地址', read_only=True)
    itype_text = serializers.CharField(source='get_itype_display', label='行业类型', read_only=True)
    otype_text = serializers.CharField(source='get_otype_display', label='职业类型', read_only=True)
    relation_text = serializers.CharField(source='get_relation_display', label='与借款人关系', read_only=True)
    tsrelation_text = serializers.CharField(source='get_tsrelation_display', label='与借款人关系', read_only=True)
    tsitype_text = serializers.CharField(source='get_tsitype_display', label='身份证件类型', read_only=True)
    tsmaddress_text = serializers.CharField(source='get_tsmaddress_display', label='共借人邮寄地址', read_only=True)
    mstatus_text = serializers.CharField(source='get_mstatus_display', label='婚姻状况', read_only=True)
    wbst_text = BooleanCharField(source='wbst', label='是否挂靠', read_only=True)
    thesame_text = BooleanCharField(source='thesame', label='是否有共同借款人', read_only=True)

    class Meta:
        model = Order
        fields = '__all__'

    def create(self, validated_data):
        validated_data['create_user'] = self.context['request'].user
        instance = super(OrderSerializer, self).create(validated_data)
        instance.save()
        return instance
