from django.contrib import messages
from medic.models import medications,medic_inhouse,inventory_entry
from  medic.forms import  form_medications,form_medications_search,form_medic_inhouse,form_inventory_entry_search,form_medic_inhouse_search,form_inventory_entry
from baseapp.views import formview_base,view_base,formview_base_patient,view_base_patient
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ValidationError
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
# Create your views here.


@login_required
def formview_medications(request,pid=1, id = 0):    return formview_base_patient(request,form_medications,medications, 'Diagnosis',pid,id,'add_medications')

def view_medications(request,pid,viewtype ='tbl',*args, **kwargs):
    return view_base_patient(request,medications,form_medications_search,pid,form_medications.Meta.fields ,form_medications.Meta.fields, 'Diagnosis',viewtype,'view_meications','drug_name')


# def formview_med_unit(request,id = 0):  
#     return formview_base(request,form_med_unit,med_unit,'Medical Unit',id,'add_med_unit')

# def view_med_unit(request, viewtype ='tbl',*args, **kwargs):
#     return view_base(request,med_unit,form_med_unit_search, form_med_unit.Meta.fields ,form_med_unit.Meta.fields, 'Medical Unit',viewtype,'unit_type','view_med_unit' )


def formview_medic_inhouse(request,id = 0):  
    return formview_base(request,form_medic_inhouse,medic_inhouse,'Medicine Inhouse',id,'add_med_inhouse')

def view_medic_inhouse(request, viewtype ='tbl',*args, **kwargs):
    return view_base(request,medic_inhouse,form_medic_inhouse_search, form_medic_inhouse.Meta.fields ,form_medic_inhouse.Meta.fields, 'Medicine Inhouse',viewtype,'drug_name','view_med_inhouse')



def formview_inventory_entry(request,pid=1,id = 0):  
    return formview_base_patient(request,form_inventory_entry,inventory_entry, 'Inventory Entry',pid,id,'add_ins')

def view_inventory_entry(request,pid,viewtype ='tbl',*args, **kwargs):
    return view_base_patient(request,inventory_entry,form_inventory_entry_search,pid,form_inventory_entry.Meta.fields ,form_inventory_entry.Meta.fields, 'Inventory Entry',viewtype,'view_inventory')


def formview_inventory_entry_manage(request,id = 0):  
    if request.method == "POST":
        if request.POST.get('entry_type') != 'Goods Recieved' and  float(request.POST.get('dosage_unit'))>0 : 
            messages.error(request, 'Any deduction should have -ve number')
            return HttpResponseRedirect( request.build_absolute_uri())
    return formview_base(request,form_inventory_entry,inventory_entry, 'Inventory Entry',id,'add_ins')    



def view_inventory_entry_manage(request,viewtype ='tbl',*args, **kwargs):
    return view_base(request,inventory_entry,form_inventory_entry_search,form_inventory_entry.Meta.fields ,form_inventory_entry.Meta.fields, 'Inventory Entry',viewtype,'entry_details','view_inventory')


# 



