from django.shortcuts import render
from django.shortcuts import redirect
import requests
from django.http import HttpResponse
import json
from django.http import JsonResponse
from operators.views import operator_page

# Create your views here.

def index(request):
    return render(request, 'home/index.html')

def bundleState(request):
        if request.method == 'POST':
            data = json.loads(request.body)
            data_state = {
                'bundle_state': data.get('state')
            }
            print(data_state,'data')
            response = requests.post('https://thingsaccess.com/rfid/index.php/Api/getBundleAccStates', json=data_state)
            data_send = response.json()
            dataBundle = data_send['response']
            # print(dataBundle)
            return JsonResponse({'dataBundle': dataBundle})



def getbundlebyGroup(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/getGroupBundles', json=data_art)
            data_send = response.json()
            bundle_data = data_send['response']
            return JsonResponse({'bundle_data': bundle_data})

def getmachineReporting(request):
       if request.method == 'POST':
            data = json.loads(request.body)
            data_art = {
                'date': data.get('date'),
            }
            # print(data_art)
            response = requests.post('https://thingsaccess.com/rfid/index.php/Api/machineReporting', json=data_art)
            data_send = response.json()
            bundle_data = data_send['response']
            # print(bundle_data)
            return JsonResponse({'bundle_data': bundle_data})
def getoperatorReporting(request):
       if request.method == 'POST':
            data = json.loads(request.body)
            data_art = {
                'date': data.get('date'),
            }
            # print(data_art)
            response = requests.post('https://thingsaccess.com/rfid/index.php/Api/operatorReporting', json=data_art)
            data_send = response.json()
            bundle_data = data_send['response']
            # print(bundle_data)
            return JsonResponse({'response': bundle_data})
       
def OperatorState(request):
        if request.method == 'POST':
            data = json.loads(request.body)
            print(data)
            data_state = {
                'operator_state': data.get('operator_state')
            }
            response = requests.post('https://thingsaccess.com/rfid/index.php/Api/getOperatorAccStates', json=data_state)
            data_send = response.json()
            dataOperator = data_send['response']
            return JsonResponse({'dataOperator': dataOperator})


def MachineState(request):
        if request.method == 'POST':
            data = json.loads(request.body)
            data_state = {
                'machine_state': data.get('machine_state')
            }
            response = requests.post('https://thingsaccess.com/rfid/index.php/Api/getMachineAccStates', json=data_state)
            data_send = response.json()
            dataMachine = data_send['response']
            return JsonResponse({'dataMachine': dataMachine})
