import json

from django.http import HttpResponse
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView

from core.clinic.crm.models import *
from core.reports.forms import ReportForm
from core.security.mixins import AccessModuleMixin


class DateMedicalReportView(AccessModuleMixin, TemplateView):
    template_name = 'datemedical_report/report.html'

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        action = request.POST.get('action', None)
        data = {}
        try:
            if action == 'search_report':
                data = []
                start_date = request.POST['start_date']
                end_date = request.POST['end_date']
                search = DateMedical.objects.filter()
                if len(start_date) and len(end_date):
                    search = search.filter(date_joined__range=[start_date, end_date])
                for i in search:
                    data.append(i.toJSON())
            elif action == 'search_exams':
                data = []
                for i in DateMedicalExam.objects.filter(datemedical_id=request.POST['id']):
                    data.append(i.toJSON())
            elif action == 'search_medicines':
                data = []
                for i in DateMedicalProducts.objects.filter(datemedical_id=request.POST['id']):
                    data.append(i.toJSON())
            elif action == 'search_medicalparameter':
                data = []
                for i in DateMedicalParameters.objects.filter(datemedical_id=request.POST['id']):
                    data.append(i.toJSON())
            else:
                data['error'] = 'No ha ingresado una opción'
        except Exception as e:
            data['error'] = str(e)
        return HttpResponse(json.dumps(data), content_type='application/json')

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['form'] = ReportForm()
        context['title'] = 'Reporte de Citas Médicas'
        return context
