from django.shortcuts import render, redirect, get_object_or_404
from .models import Igw

def igw_view(request):
    if request.user.is_authenticated:
        records = Igw.objects.filter(author=request.user)
    else:
        records = Igw.objects.all()
    return render(
        request, 
        'nhnc_mgmt/network_igw.html',
        {
            'records' : records
        })
    
def add_igw_record(request):
    if request.method == 'POST':
        igw_name = request.POST.get('igw_name')
        routing_table = request.POST.get('routing_table')
        igw_id = request.POST.get('igw_id')
        author = request.user
        # 데이터 저장
        Igw.objects.create(
            igw_name=igw_name,
            routing_table=routing_table,
            igw_id=igw_id,
            author=author
        )
        return redirect('/nhnc_mgmt/igw')
    
def delete_igw_records(request):
    print(f"삭제동작")
    if request.method == 'POST':
        selected_ids = request.POST.getlist('selected_records')
        if selected_ids:
            Igw.objects.filter(id__in=selected_ids).delete()
    return redirect('/nhnc_mgmt/igw')
