# coding=utf-8

from django.db import transaction

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 MaintOrderReserveViewSet(CustomModelViewSet):
    queryset = MaintOrderReserve.objects.filter(delete=False)
    serializer_class = MaintOrderReserveSerializer

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

    @action(methods=['post'], detail=True)
    def change_status(self, request, pk):
        order = MaintOrderReserve.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' % (MaintOrderReserve.STATUS_CHOICES[order.status-1][1], order.id))
        return response_ok()
