from django.shortcuts import render
from django.views import View
from organizationmanagement.models import Branch
import json
from codesofy.custom_config import set_user_profile,get_privilleges,is_authorized
from django.http import JsonResponse

# Create your views here.


class GetBranchNameByBranchCodeView(View):


    this_feature = "get_branch_name_by_branch_code"

    def get(self, request):

        context = {}
        user_profile = set_user_profile(request,context)

        if user_profile==None:
            return JsonResponse({})


        get_privilleges(user_profile,context)

        if not is_authorized(user_profile,self.this_feature):
            return JsonResponse({})
        
        branch_code = request.GET.get('branch-code')       

        if (branch_code):
            branches = Branch.objects.filter(branch_code=branch_code)
            if (len(branches)>0):

                branch = branches[0]
                branch_name = branch.branch_name
                return JsonResponse({'branch_name':branch_name})
            

class GetBranchCodeByBranchNameView(View):

    this_feature = "get_branch_code_by_branch_name"


    def get(self, request):

        context = {}
        user_profile = set_user_profile(request,context)

        if user_profile==None:
            return JsonResponse({})

        get_privilleges(user_profile,context)

        if not is_authorized(user_profile,self.this_feature):
            return JsonResponse({})
        

        branch_name = request.GET.get('branch-name')    

        if (branch_name):
            branches = Branch.objects.filter(branch_name=branch_name)
            if (len(branches)>0):

                branch = branches[0]
                branch_code = branch.branch_code
                return JsonResponse({'branch_code':branch_code})
            
