# coding=utf-8

from django.db import transaction
from utils.exceptions import CustomError
from rest_framework.decorators import action

from utils import response_ok
from utils.custom_modelviewset import CustomModelViewSet
from apps.account import tenant_log
from apps.foundation.models import BizLog

from .filters import *
from .serializers import *
from .models import *


class VehicleDriveReserveViewSet(CustomModelViewSet):
    queryset = VehicleDriveReserve.objects.filter(delete=False)
    serializer_class = VehicleDriveReserveSerializer

    def filter_queryset(self, queryset):
        f = VehicleDriveReserveFilter(self.request.GET, queryset=queryset)
        return f.qs

    @action(methods=['post'], detail=True)
    def change_status(self, request, pk):
        order = VehicleDriveReserve.objects.filter(id=pk).first()
        status = request.GET.get('status')
        with transaction.atomic():
            order.status = int(status)
            order.save()
            tenant_log(self.request.user, BizLog.UPDATE, u'修改试驾单状态[%s]，id=%d' % (VehicleDriveReserve.STATUS_CHOICES[order.status-1][1], order.id))
        return response_ok()


class VehicleInquiryViewSet(CustomModelViewSet):
    queryset = VehicleInquiry.objects.filter(delete=False)
    serializer_class = VehicleInquirySerializer

    def filter_queryset(self, queryset):
        f = VehicleInquiryFilter(self.request.GET, queryset=queryset)
        return f.qs

    @action(methods=['post'], detail=True)
    def visit(self, request, pk):
        order = VehicleInquiry.objects.filter(id=pk).first()
        with transaction.atomic():
            if order.status != VehicleInquiry.WAIT_VISIT:
                raise CustomError(u'当前状态不允许修改！')
            order.status = VehicleInquiry.VISITED
            order.save()
            tenant_log(self.request.user, BizLog.UPDATE, u'新车询价单回访，id=%d' % (order.id))
        return response_ok()



class UsedVehicleInquiryViewSet(CustomModelViewSet):
    queryset = UsedVehicleInquiry.objects.filter(delete=False)
    serializer_class = UsedVehicleInquirySerializer

    def filter_queryset(self, queryset):
        f = UsedVehicleInquiryFilter(self.request.GET, queryset=queryset)
        return f.qs

    @action(methods=['post'], detail=True)
    def visit(self, request, pk):
        order = UsedVehicleInquiry.objects.filter(id=pk).first()
        with transaction.atomic():
            if order.status != UsedVehicleInquiry.WAIT_VISIT:
                raise CustomError(u'当前状态不允许修改！')
            order.status = UsedVehicleInquiry.VISITED
            order.save()
            tenant_log(self.request.user, BizLog.UPDATE, u'二手车询价单回访，id=%d' % (order.id))
        return response_ok()



class UsedVehicleEstimateViewSet(CustomModelViewSet):
    queryset = UsedVehicleEstimate.objects.filter(delete=False)
    serializer_class = UsedVehicleEstimateSerializer

    def filter_queryset(self, queryset):
        f = UsedVehicleEstimateFilter(self.request.GET, queryset=queryset)
        return f.qs

    @action(methods=['post'], detail=True)
    def visit(self, request, pk):
        order = UsedVehicleEstimate.objects.filter(id=pk).first()
        with transaction.atomic():
            if order.status != UsedVehicleEstimate.WAIT_VISIT:
                raise CustomError(u'当前状态不允许修改！')
            order.status = UsedVehicleEstimate.VISITED
            order.save()
            tenant_log(self.request.user, BizLog.UPDATE, u'二手车预估单回访，id=%d' % (order.id))
        return response_ok()


class InsurConsultationViewSet(CustomModelViewSet):
    queryset = InsurConsultation.objects.filter(delete=False)
    serializer_class = InsurConsultationSerializer

    def filter_queryset(self, queryset):
        f = InsurConsultationFilter(self.request.GET, queryset=queryset)
        return f.qs

    @action(methods=['post'], detail=True)
    def visit(self, request, pk):
        order = InsurConsultation.objects.filter(id=pk).first()
        with transaction.atomic():
            if order.status != InsurConsultation.WAIT_VISIT:
                raise CustomError(u'当前状态不允许修改！')
            order.status = InsurConsultation.VISITED
            order.save()
            tenant_log(self.request.user, BizLog.UPDATE, u'续保询价单回访，id=%d' % (order.id))
        return response_ok()