from django.shortcuts import render
from django.contrib import messages
from django.shortcuts import redirect
from articlegroup.views import get_article_group
import requests
from django.http import HttpResponse
import json
from django.http import JsonResponse




# Create your views here.
def get_article_data():  # sourcery skip: inline-immediately-returned-variable
   response = requests.get('https://thingsaccess.com/rfid/index.php/Api/getArticles')
   data_send = response.json()
   data = data_send['response']
   if data == 11:
     data = []  
   return data


def article_page(request):
    data = get_article_data()
    return render(request, 'article/article.html', context={'data': data})

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

def machine_data():
   response = requests.get('https://thingsaccess.com/rfid/index.php/Api/getMachine')
   data_send = response.json()
   return data_send['response']
    
def Get_opeartions(request):
   if request.method == 'POST':
        data = json.loads(request.body)
        data_skill = {
            'skill_id': int(data.get('id')),
        }
        response = requests.post('https://thingsaccess.com/rfid/index.php/Api/getOperationSkills', json=data_skill)
        data_send = response.json()
        data_skill = data_send['response']
        print(data_skill)
        return JsonResponse({'data_skill': data_skill})
   
def Get_bundle_status(request):
   print(request)
   if request.method == 'POST':
        data = json.loads(request.body)
        data_skill = {
            'article_id': str(data.get('id')),
            'state': str(data.get('status')),
        }
        print(data.get('state'))
        response = requests.post('https://thingsaccess.com/rfid/index.php/Api/bundleStatus', json=data_skill)
        data_send = response.json()
        data_skill = data_send['response']
        print(data_skill)
        return JsonResponse({'data_skill': data_skill})
               
   # return render(request, 'article/AddArticle.html')

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


def Add_article_data(request):

   Article_group = get_article_group()
   skills = get_skill()
   
   if request.method == 'POST':
         data_json = json.loads(request.body)
         data_add = {
            'title': data_json.get('title'),
            'pieces': data_json.get('pieces'),
            'orderQuantity': data_json.get('orderQuantity'),
            'bundleSize': data_json.get('bundleSize'),
            'fabricLength': data_json.get('fabricLength'),
            'fabric': data_json.get('fabric'),
            'style_description': data_json.get('style_description'),
            'color': data_json.get('color'),
            'startDate': data_json.get('startDate'),
            'deliveryDate': data_json.get('deliveryDate'),
            'group': data_json.get('groupDataArray')
            
         }

         print(data_add)
        
         response = requests.post('https://thingsaccess.com/rfid/index.php/Api/addArticles', json=data_add)
         data_send = response.json()
         data_article = data_send['response']
         return JsonResponse({'data_article': data_article})
      
   return render(request, 'article/article_add.html',context={'Article_group':Article_group,'skills':skills})


def Edit_Article(request,id):
   response = requests.get('https://thingsaccess.com/rfid/index.php/Api/getArticles')
   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'),
               'pieces': request.POST.get('pieces'),
               'orderQuantity': request.POST.get('orderQuantity'),
               'bundleSize': request.POST.get('bundleSize'),
               'fabricLength': request.POST.get('fabricLength'),
               'startDate': request.POST.get('startDate'),
               'deliveryDate': request.POST.get('deliveryDate'),
         }
         response = requests.post('https://thingsaccess.com/rfid/index.php/Api/editArticles', json=data)
         if response.status_code == 200:
               return redirect('article_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,'article/editArticle.html',context={'item': edit_item})
  
def delete_Article(request,id):
   data_delete = {
        'id': id
   }
   print(data_delete)
   response = requests.post('https://thingsaccess.com/rfid/index.php/Api/deleteArticles', json=data_delete)
   if response.status_code == 200:
        return redirect('article_page')
        # messages.success(request, 'Data added successfully')
   else:
        # Insertion failed, show error message to user
       messages.error(request, 'Failed to delete data')
       return redirect('article_page')

   # return HttpResponse("Article not deleted successfully")


   
