

from django.shortcuts import render, get_object_or_404
from support.models import *
from osinacore.decorators import *
from django.http import HttpResponse


def add_ticket_department_modal(request, ticket_number):
    ticket = get_object_or_404(Ticket, ticket_number=ticket_number)
    departments = Department.objects.all().order_by('-id')
    if request.method == 'POST':
        department = get_object_or_404(Department, id=request.POST.get('department'))
        ticketdepartment = TicketDepartment(
            department=department,
            ticket=ticket,
            date_added = request.POST.get('date')
        )
        ticketdepartment.save()
        return HttpResponse('<script>window.top.location.reload();</script>')

    context = {  
        'ticket': ticket,
        'departments': departments,
    }

    return render(request, 'add_templates/add-ticket-department-modal.html', context)




def add_ticket_member_modal(request, ticket_number):
    ticket = get_object_or_404(Ticket, ticket_number=ticket_number)
    staffprofiles = StaffProfile.objects.filter(active=True).all()
    if request.method == 'POST':
        staff = get_object_or_404(StaffProfile, id=request.POST.get('staff'))
        ticketstaff = TicketStaff(
            staff=staff,
            ticket=ticket,
            date_added = request.POST.get('date')
        )
        ticketstaff.save()
        return HttpResponse('<script>window.top.location.reload();</script>')

    context = {  
        'ticket': ticket,
        'staffprofiles': staffprofiles,
    }

    return render(request, 'add_templates/add-ticket-member-modal.html', context)

