
from django.shortcuts import render
from django.contrib import messages
from django.shortcuts import redirect
import requests
from django.http import HttpResponse
from articles.views import get_article_data
import json
from django.http import JsonResponse
# Create your views here.
def step_page(request):
    if request.method == 'POST':
        data = json.loads(request.body)
        data_art = {
            'article_id': data.get('id'),
        }
        print(data_art)
        response = requests.post('https://thingsaccess.com/rfid/index.php/Api/getStepArticle', json=data_art)
        data_send = response.json()
        data_steps = data_send['response']
        return JsonResponse({'data_steps': data_steps})
        # return render(request, 'steps/steps.html', {'data_steps': data_steps})
    else:
        data_artcile = get_article_data()
        # Initialize an empty list for data_step
        return render(request, 'steps/steps.html', {'data_artcile': data_artcile})
       
def getgroupstep(request):
    if request.method == 'POST':
        data = json.loads(request.body)
        data_art = {
            'article_id': data.get('id'),
        }
        print(data_art)
        response = requests.post('https://thingsaccess.com/rfid/index.php/Api/getGroupArticle', json=data_art)
        data_send = response.json()
        data_group = data_send['response']
        # print(data_group)
        return JsonResponse({'data_group': data_group})
    
def getStepsbyGroups(request):
       if request.method == 'POST':
            data = json.loads(request.body)
            data_art = {
                'group_id': data.get('id'),
            }
            # print(data_art)
            response = requests.post('https://thingsaccess.com/rfid/index.php/Api/getBundleGroup', json=data_art)
            data_send = response.json()
            step_data = data_send['response']
            return JsonResponse({'step_data': step_data})


def getStepBundle(request):
     if request.method == 'POST':
            data = json.loads(request.body)
            data_art = {
                'bundle_id': data.get('id'),
            }
            # print(data_art)
            response = requests.post('https://thingsaccess.com/rfid/index.php/Api/getBundleSteps', json=data_art)
            data_send = response.json()
            step_info = data_send['response']
            return JsonResponse({'step_info': step_info})
       
       

def Add_Step(request):
   if request.method == 'POST':
        data = {
            'article_ID': request.POST.get('article_ID'),
            'title': request.POST.get('title'),
            'last_step': request.POST.get('last_step'),
        }
        response = requests.post('https://thingsaccess.com/rfid/index.php/Api/addSteps', json=data)
        if response.status_code == 200:
            return redirect('step_page')
            # messages.success(request, 'Data added successfully')
        else:
            # Insertion failed, show error message to user
            # messages.error(request, 'Failed to add data')
            return HttpResponse("Steps not added")

   return render(request, 'steps/addSteps.html')

def Edit_Steps(request,id):
   response = requests.get('https://thingsaccess.com/rfid/index.php/Api/getSteps')
   data_send = response.json()
   data = data_send['response']
   edit_item = None
   for item in data:
        if item['ID'] == id:
            edit_item = item
            break
   if edit_item:
      if request.method == 'POST':
         data = {
                'id':id,
                'title': request.POST.get('title'),
                'last_step': request.POST.get('last_step'),
         }
         response = requests.post('https://thingsaccess.com/rfid/index.php/Api/editSteps', json=data)
         if response.status_code == 200:
                return redirect('step_page')
               # messages.success(request, 'Data added successfully')
         else:
               # Insertion failed, show error message to user
               messages.error(request, 'Failed to edit data')
              

   return render(request,'steps/editSteps.html',context={'item': edit_item})


def delete_Steps(request,id):
    data_delete = {
         'id': id
    }
    print(data_delete)
    response = requests.post('https://thingsaccess.com/rfid/index.php/Api/deleteSteps', json=data_delete)
    if response.status_code == 200:
        return redirect('step_page')
    messages.error(request, 'Failed to delete data')
    return HttpResponse("Steps not deleted")
