from django.shortcuts import render, redirect, get_object_or_404
from rest_framework import viewsets

from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.status import HTTP_200_OK, HTTP_401_UNAUTHORIZED
from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework.permissions import IsAuthenticated

from rest_framework import permissions
from django.http.response import JsonResponse

from django.views.decorators.csrf import csrf_exempt
from django.db.models import F

import os
import json
import ulid
import base64
import cv2
import numpy as np

from XCMS_PIM.pim_cnsts import *

from app.models.prd_sales_delv_model import PrdSalesDelvModel

class PrdSalesDelvManage(APIView):
    authentication_classes = (JWTAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get(self, request):

        lPrdSalesDelvs = PrdSalesDelvModel.objects.all().order_by('prdsd_name')

        lResult = "success"

        lResponseData = {
            'result': lResult,
            'data':list(lPrdSalesDelvs.values(uid=F('prdsd_uid'),name=F('prdsd_name'),note=F('prdsd_note'))),
        }

        return JsonResponse(lResponseData,safe=False,status=HTTP_200_OK)


class PrdSalesDelvNew(APIView):

    authentication_classes = (JWTAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get(self, request):
        return Response(status=HTTP_500_INTERNAL_SERVER_ERROR)

    def post(self, request):

        lResult = "failure"
        data = json.loads(request.body)

        if data["uid"] == "":
            lPrdSalesDelv = PrdSalesDelvModel.objects.create(prdsd_uid=ulid.new(), sds_uid=data["sds_uid"], prdsd_name=data["name"], prdsd_note=data["note"])
        else:
            lPrdSalesDelv = PrdSalesDelvModel.objects.create(prdsd_uid=data["uid"], sds_uid=data["sds_uid"], prdsd_name=data["name"], prdsd_note=data["note"])

        lResult = "success"

        lResponseData = {
            'result': lResult,
            'uid':str(lPrdSalesDelv.prdsd_uid)
        }

        return JsonResponse(lResponseData,safe=False,status=HTTP_200_OK)

class PrdSalesDelvDel(APIView):

    authentication_classes = (JWTAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get(self, request):
        return Response(status=HTTP_500_INTERNAL_SERVER_ERROR)

    def post(self, request, prdsd_uid):

        lResult = "failure"

        lPrdSalesDelv = get_object_or_404(PrdSalesDelvModel, prdsd_uid=prdsd_uid)
        lPrdSalesDelv.delete()

        lResult = "success"

        lResponseData = {
            'result': lResult,
        }

        return JsonResponse(lResponseData,safe=False,status=HTTP_200_OK)


class PrdSalesDelvUpd(APIView):

    authentication_classes = (JWTAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get(self, request, prdsd_uid):
        lResult = "success"

        lPrdSalesDelv = get_object_or_404(PrdSalesDelvModel, prdsd_uid=prdsd_uid)

        lResponseData = {
            'result': lResult,
            'data':{
            'sds_uid': lPrdSalesDelv.sds_uid,
            'name': lPrdSalesDelv.prdsd_name,
            'note': lPrdSalesDelv.prdsd_note,
            }
        }

        return JsonResponse(lResponseData,safe=False,status=HTTP_200_OK)

    def post(self, request, prdsd_uid):

        lResult = "failure"

        data = json.loads(request.body)

        lPrdSalesDelv = get_object_or_404(PrdSalesDelvModel, prdsd_uid=prdsd_uid)
        lPrdSalesDelv.sds_uid = data["sds_uid"]
        lPrdSalesDelv.prdsd_name = data["name"]
        lPrdSalesDelv.prdsd_note = data["note"]
        lPrdSalesDelv.save()

        lResult = "success"

        lResponseData = {
            'result': lResult,
        }

        return JsonResponse(lResponseData,safe=False,status=HTTP_200_OK)

