from django.shortcuts import render
from .models import LabTest
from patient.models import *

# Create your views here.
def index(request):
    data = request.POST.get('data')
    print(request.POST.get('data'))
    if data is not None :
        tests = ['Common Blood Count','MRI','X-Ray','Metabolism Panel']
        test_name = data
        patient_id = request.POST.get('patient_id')
        _date = request.POST.get('date')
        if test_name == tests[0] :
            print(patient_id)
            tup = Commonbloodcount(pid=Patient.objects.get(pid=patient_id),date=_date)
            tup.rbc = request.POST.get('rbc')
            tup.wbc = request.POST.get('wbc')
            tup.hemoglobin = request.POST.get('hemoglobin')
            tup.hematocrit = request.POST.get('hematocrit')
            tup.mcv = request.POST.get('mcv')
            tup.mch = request.POST.get('mch')
            tup.mchc = request.POST.get('mchc')
            tup.rdw = request.POST.get('rdw')
            tup.platelets = request.POST.get('platelets')
            tup.neurophils = request.POST.get('neurophils')
            tup.lymphs = request.POST.get('lymphs')
            tup.monocytes = request.POST.get('monocytes')
            tup.save()
        elif test_name == tests[1] :
            tup = Mri(pid=Patient.objects.get(pid=patient_id),date=_date)
            tup.mri = request.FILES.get('file')
            tup.save()
        elif test_name == tests[2] :
            print(patient_id)
            tup = Xray(pid=Patient.objects.get(pid=patient_id),date=_date)
            tup.xray = request.FILES.get('file')
            tup.save()
        else :
            tup = Basicmetabolismpanel(pid=Patient.objects.get(pid=patient_id),date=_date)
            tup.glucose = request.POST.get('glucose')
            tup.creatinine = request.POST.get('creatinine')
            tup.sodium = request.POST.get('sodium')
            tup.potassium = request.POST.get('potassium')
            tup.chloride = request.POST.get('chloride')
            tup.carbon_dioxide = request.POST.get('carbon_dioxide')
            tup.save()
        labtest = LabTest.objects.get(pid=patient_id,date=_date,test=test_name)
        labtest.delete()
    test_list = { 'test_list' : LabTest.objects.all() }
    return render(request, "button_lab.html", test_list)

def test(request) :
    test_id = request.POST['fill']
    test_tup = LabTest.objects.get(id=test_id)
    tests = ['Common Blood Count','MRI','X-Ray','Metabolism Panel']
    details = { 'patient_id' : test_tup.pid.pid , 'date' : test_tup.date }
    if test_tup.test == tests[0] :
        return render(request, "form_cbc.html", details)
    elif test_tup.test == tests[1] :
        return render(request, "form_mri.html", details)
    elif test_tup.test == tests[2] :
        return render(request, "form_xray.html", details)
    else:
        return render(request, "form_metabolism.html", details)

def log(request):
    return render(request, "login.html")

