from django.shortcuts import render
from django.contrib import messages
from django.shortcuts import redirect
from articles.views import get_article_data
from machine.views import get_machine_data
import requests
from django.http import HttpResponse
import json
from django.http import JsonResponse


def getAllMachines():
   response = requests.get('https://thingsaccess.com/rfid/index.php/Api/getAllMachines')
   get_machine = response.json()
   return get_machine['response']


def getgroupbundle(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 getStepsbyGroup(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/getGroupSteps', json=data_art)
            data_send = response.json()
            step_data = data_send['response']
            return JsonResponse({'step_data': step_data})
            


def getMachineBundle(request):
    data_artcile = get_article_data()
    all_machine_data = getAllMachines()
    assigned_machine = all_machine_data['assignedMachines']
    unassigned_machine = all_machine_data['unAssignedMachines']
    return render(request, 'MachineBundle/Machinebundle.html',context={'data_artcile': data_artcile,'assigned_machine':assigned_machine,'unassigned_machine':unassigned_machine})


    
def add_Machine_Bundle(request):
    if request.method == 'POST':
        data = json.loads(request.body)
        data_art = {
            'article_id': data.get('articleId'),
            'group_id': data.get('group_id'),
            'selectedMachines': data.get('selectedMachines'),
        }
        
        response = requests.post('https://thingsaccess.com/rfid/index.php/Api/addMachineBundle', json=data_art)
        data_send = response.json()
        machine_bundle = data_send['response']
        # print(machine_bundle)
        return JsonResponse({'machine_bundle': machine_bundle})
        # Process the data as needed
        
        # Return a JSON response if required
       