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



@staff_login_required
def edit_payment_method(request, method_id):
    method = get_object_or_404(PaymentType, id=method_id)
    if request.method == 'POST':
        method.name = request.POST.get('name')
        method.description = request.POST.get('description')
        if request.FILES.get('image'):
            method.image = request.FILES.get('image')
        method.save()
        return redirect('paymentmethods')
    
    context = {
        'method': method,

    }
    return render(request, 'edit_templates/edit-payment-method.html', context)
    



@staff_login_required
def edit_payment_modal(request):

    context = {

    }

    return render(request, 'edit_templates/edit-payment-modal.html', context) 



@staff_login_required
def update_order_status(request, order_id):
    order = get_object_or_404(Order, id=order_id)
    if request.method == 'POST':
        status = OrderStatus(
            status = request.POST.get('status'),
            order = order,
            date =  request.POST.get('date'),
        )
        status.save()
        return HttpResponse('<script>window.top.location.reload();</script>')
    context = {
        'order': order
    }

    return render(request, 'edit_templates/update-order-status-modal.html', context) 