# coding=utf-8
from rest_framework.views import APIView
from django.db.models import Q

from utils.custom_modelviewset import CustomModelViewSet
from utils import response_ok, response_error
from apps.option.models import Option
from apps.option.serializers import OptionComboboxSerializer
from .models import ReportCustomer
from .serializers import ReportCustomerSerializer
from .filters import ReportCustomerFilter


class ReportCustomerViewSet(CustomModelViewSet):
    permission_classes = []
    queryset = ReportCustomer.objects.filter()
    serializer_class = ReportCustomerSerializer

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

    def perform_create(self, serializer):
        super(ReportCustomerViewSet, self).perform_create(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data

    def perform_update(self, serializer):
        super(ReportCustomerViewSet, self).perform_update(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        # if instance.tenant != request.user.employee.tenant:
        #     raise CustomError(u'禁止跨企业操作！')
        super(ReportCustomerViewSet, self).destroy(self, request, *args, **kwargs)
        return response_ok()

    # @action(methods=['post'], detail=False)
    # def check(self, request, pk):
    #     # 审核
    #     report_status = request.POST.get('report_status')
    #     try:
    #         instance = Customer.objects.filter(id=pk).first()
    #         if not instance:
    #             raise CustomError('当前客户报备信息有误！')
    #
    #         instance.check_user = self.request.user
    #         instance.report_status = report_status
    #         instance.check_time = timezone.now()
    #         instance.save()
    #
    #     except CustomError as e:
    #         return response_error(e.get_error_msg())
    #     except Exception as e:
    #         return response_error(str(e))
    #     return response_ok('审核完成！')

class ReportCustomerDictView(APIView):
    permission_classes = []

    def get(self, request):
        scouce = Option.objects.filter(type=Option.CUSTOMER_SOURCE , enable=True)
        project = Option.objects.filter(type=Option.CATEGORY, enable=True)
        serializer_scouce = OptionComboboxSerializer(scouce, many=True)
        serializer_project = OptionComboboxSerializer(project, many=True)
        return response_ok({
            'source': serializer_scouce.data,
            'project': serializer_project.data,
        })


