# coding=utf-8
from django.db import transaction
from rest_framework.views import APIView
from utils import response_ok
from utils.custom_modelviewset import CustomModelViewSet
from utils.exceptions import CustomError
from utils.permission import IsTenantUser, permission_required
from apps.tenant import tenant_log
from apps.log.models import BizLog
from .serializers import AreaSerializer, AreaDepartmentSerializer
from .models import Area, Department
from .filters import AreaFilter, AreaDepartmentFilter

class DictView(APIView):
    permission_classes = [IsTenantUser, ]

    def get(self, request):
        tenant = request.user.employee.tenant
        ret = {
            'area': AreaSerializer(Area.objects.filter(delete=False, enable=True, tenant=tenant), many=True).data,
            'department': AreaDepartmentSerializer(Department.objects.filter(delete=False, enable=True, area__tenant=tenant), many=True).data
        }
        return response_ok(ret)


class AreaViewSet(CustomModelViewSet):
    permission_classes = [IsTenantUser, ]
    queryset = Area.objects.filter(delete=False)
    serializer_class = AreaSerializer

    @permission_required('area.browse_area')
    def filter_queryset(self, queryset):
        queryset = queryset.filter(tenant=self.request.user.employee.tenant)
        f = AreaFilter(self.request.GET, queryset=queryset)
        return f.qs

    @permission_required('area.add_area')
    def perform_create(self, serializer):
        super(AreaViewSet, self).perform_create(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data
        tenant_log(self.request.user.employee, BizLog.INSERT, u'添加区域[%s]，id=%d' % (instance.name, instance.id),
                   validated_data)

    @permission_required('area.add_area')
    def perform_update(self, serializer):
        super(AreaViewSet, self).perform_update(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data
        tenant_log(self.request.user.employee, BizLog.UPDATE, u'修改区域[%s]，id=%d' % (instance.name, instance.id),
                   validated_data)

    @permission_required('area.delete_area')
    def destroy(self, request, *args, **kwargs):
        with transaction.atomic():
            instance = self.get_object()
            instance.delete = True
            instance.save()
            tenant_log(self.request.user.employee, BizLog.DELETE, u'删除区域[%s]，id=%d' % (instance.name, instance.id))
        return response_ok()


class AreaDepartmentViewSet(CustomModelViewSet):
    permission_classes = [IsTenantUser, ]
    queryset = Department.objects.filter(delete=False,area__delete=False)
    serializer_class = AreaDepartmentSerializer

    @permission_required('area.browse_area')
    def filter_queryset(self, queryset):
        queryset = queryset.filter(area__tenant=self.request.user.employee.tenant)
        f = AreaDepartmentFilter(self.request.GET, queryset=queryset)
        return f.qs

    @permission_required('area.add_area')
    def perform_create(self, serializer):
        super(AreaDepartmentViewSet, self).perform_create(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data
        tenant_log(self.request.user.employee, BizLog.INSERT, u'添加区域部门[%s]，id=%d' % (instance.name, instance.id),
                   validated_data)

    @permission_required('area.add_area')
    def perform_update(self, serializer):
        super(AreaDepartmentViewSet, self).perform_update(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data
        tenant_log(self.request.user.employee, BizLog.UPDATE, u'修改区域部门[%s]，id=%d' % (instance.name, instance.id),
                   validated_data)

    @permission_required('area.delete_area')
    def destroy(self, request, *args, **kwargs):
        with transaction.atomic():
            instance = self.get_object()
            instance.delete = True
            instance.save()
            tenant_log(self.request.user.employee, BizLog.DELETE, u'删除区域[%s]部门[%s]，id=%d' % (instance.area.name,instance.name, instance.id))
        return response_ok()
