angular.module("knimbusLib").controller("premiumContentCtrl",function($scope,PromiseAjax,$rootScope,$location,$uibModal,$timeout,$filter){$(document).ready(function(){$(document).scrollTop(185)});$scope.init=function(){$scope.getCurrentUserDetail();$scope.getAllPaidUserGroups();$scope.fetchPaidSections();$scope.getUserGroups()};$scope.paidUserGroup={};$scope.noValidResult=false;$scope.orgValidPaidSectionIds=[];$scope.paidUserGroupIds=[];$scope.purchasedUserGroupIds=[];$scope.purchasedSectionIds=[];
$scope.userGroupSectionsData={};$scope.getUserGroups=function(){$scope.userPaidUserGroups=[];$scope.purchasedUserGroupIds=[];$scope.purchasedSectionIds=[];$scope.userGroupSectionsData={};PromiseAjax.getData("/ws/getUserGroups","GET").then(function(response){if(response.response.responseCode==100){$scope.paidUserGroupIds=response.paidUserGroupIds;$scope.purchasedUserGroupIds=response.purchasedUserGroupIds;$scope.purchasedSectionIds=response.purchasedSectionIds;$scope.userGroupSectionsData=response.userGroupSectionsData}$timeout(function(){$scope.bindUserGroupData()},
500)}).catch(function(error){console.error("error: "+error)})};$scope.bindUserGroupData=function(){$scope.noValidResult=false;$scope.validPaidUserGroupsByOrgId=[];angular.forEach($scope.allPaidUserGroupsByOrgId,function(paidUserGroup){paidUserGroup.hasUserGroupAssociation=$scope.paidUserGroupIds.length>0&&$scope.paidUserGroupIds.indexOf(paidUserGroup.groupId)!==-1;paidUserGroup.hasPurchasedUserGroup=$scope.purchasedUserGroupIds.length>0&&$scope.purchasedUserGroupIds.indexOf(paidUserGroup.groupId)!==
-1;paidUserGroup.sectionId=$scope.userGroupSectionsData[paidUserGroup.groupId]==null?null:$scope.userGroupSectionsData[paidUserGroup.groupId][0];paidUserGroup.isValidGroup=false;if($scope.orgValidPaidSectionIds.length>0&&$scope.orgValidPaidSectionIds.indexOf(paidUserGroup.sectionId)>-1){paidUserGroup.isValidGroup=true;$scope.validPaidUserGroupsByOrgId.push(paidUserGroup)}});$scope.noValidResult=$scope.allPaidUserGroupsByOrgId.length==0||$scope.orgValidPaidSectionIds.length==0};$scope.showNoAccess=
function(){$scope.strMessage="Currently, you do not have access to this premium content. Kindly, contact your librarian for further assistance.";$("#alertBoxModal").modal("show")};$scope.getSection=function(userGroup){$scope.sectionDataArr=$filter("filter")($scope.paidSectionData,{sectionId:userGroup.sectionId});$scope.sectionObj=$scope.sectionDataArr.length>0?$scope.sectionDataArr[0]:null;if($scope.sectionObj!=null)$rootScope.redirectOrOpenSignInForm($scope.sectionObj,"","sectionMenuItem")};$scope.fetchPaidSections=
function(){$rootScope.showloader();$scope.paidSectionData=[];$scope.orgPaidSectionIds=[];PromiseAjax.getData("/ws/getPaidSections?orgId\x3d"+$rootScope.orgId,"GET").then(function(response){if(response.response.responseCode==100)$scope.orgPaidSectionIds=response.paidSectionIds}).catch(function(error){console.error("error: "+error)});PromiseAjax.getData("/ws/getAllSection?orgId\x3d"+$rootScope.orgId,"GET").then(function(data){$scope.allSectionData=data;var param={"orgId":$rootScope.orgId};PromiseAjax.getData("/ws/getSectionWithContent",
"POST",param).then(function(response){$scope.sectionIdsWithContent=response;$scope.orgValidPaidSectionIds=[];if($scope.orgPaidSectionIds.length>0)angular.forEach($scope.orgPaidSectionIds,function(id){if($scope.sectionIdsWithContent.sectionIds.indexOf(id)!==-1||$scope.sectionIdsWithContent.sectionIdWithDocCount[id]!==-1)$scope.orgValidPaidSectionIds.push(id)});angular.forEach($scope.allSectionData,function(data){if($scope.orgValidPaidSectionIds.indexOf(data.sectionId)!==-1){data.paid=true;$scope.paidSectionData.push(data)}});
$rootScope.hideloader()}).catch(function(error){console.error("error: "+error);$rootScope.hideloader()})}).catch(function(error){console.error("error: "+error);$rootScope.hideloader()})};$scope.placeOrder=function(groupId){$rootScope.showloader();PromiseAjax.getData("/ws/placeOrder?groupId\x3d"+groupId,"GET").then(function(response){if(response.response.responseCode==100)$scope.initiateRazorpay(response.orderAmount,response.razorPayKey,response.razorPayOrderId);else{$scope.strMessage="some error";
$("#alertBoxModal").modal("show")}$rootScope.hideloader()}).catch(function(error){console.error("error: "+error)})};$scope.verifyPayment=function(razorPayPaymentId,razorPayOrderId,razorPaySignature){$rootScope.showloader();var param={"razorpay_order_id":razorPayOrderId,"razorpay_payment_id":razorPayPaymentId,"razorpay_signature":razorPaySignature};PromiseAjax.getData("/ws/verifyPayment","POST",param).then(function(response){if(response.response.responseCode==100){$scope.strMessage="Payment done successfully!";
$("#alertBoxModal").modal("show");$scope.getUserGroups()}else{$scope.strMessage="some error";$("#alertBoxModal").modal("show")}$rootScope.hideloader()}).catch(function(error){console.error("error: "+error)})};$scope.initiateRazorpay=function(amount,razorPayKey,razorPayOrderId){var options={"key":razorPayKey,"amount":amount,"currency":"INR","order_id":razorPayOrderId,"handler":function(response){$scope.verifyPayment(response.razorpay_payment_id,response.razorpay_order_id,response.razorpay_signature)},
"prefill":{"name":$rootScope.userName,"email":$rootScope.loginId.replace(/_/g,"@"),"contact":""}};var rzp1=new Razorpay(options);rzp1.on("payment.failed",function(response){console.error("errorResponse",response)});rzp1.open()};$scope.getAllPaidUserGroups=function(){$rootScope.showloader();$scope.allPaidUserGroupsByOrgId=[];$scope.totalUserGroupCount=0;PromiseAjax.getData("/ws/getPaidUserGroupsByOrgId?orgId\x3d"+$rootScope.orgId,"GET").then(function(response){if(response.response.responseCode==100){if(response.paidUserGroupsByOrgId!=
undefined&&response.paidUserGroupsByOrgId!=null){angular.forEach(response.paidUserGroupsByOrgId,function(data){$scope.allPaidUserGroupsByOrgId.push(data)});$scope.totalUserGroupCount=$scope.allPaidUserGroupsByOrgId.length}}else{if(response.response.responseCode==500)$rootScope.strMessage="server error";$("#alertBoxModal").modal("show")}$rootScope.hideloader()}).catch(function(error){console.error("error: "+error);$rootScope.hideloader()})};$scope.getCurrentUserDetail=function(){PromiseAjax.getData("/ws/getCurrentUser",
"GET").then(function(result){if(result=="")$rootScope.userSignInFlag=false;else{$rootScope.userSignInFlag=true;$rootScope.pwdDateCheckForProfileModal=result.pwdResetDate;$rootScope.userName=result.userName;$rootScope.loginId=result.loginId;$rootScope.role=result.roleAuthoritySet[0];$rootScope.isLibrarian=result.roleAuthoritySet[0]=="LIBRARIAN"?true:false}})}});function resizeIframe(obj){obj.style.height=parseInt(obj.contentWindow.document.body.scrollHeight-125)+"px"};