quoteCalculator.service("envelopeTableService",function(ajaxService,finishingsParseService,restructureService){var $scope;this.instantiate=function(scope){($scope=scope).envelope=!1,$scope.envelopesOn=function(){if(!$scope.envelope){$scope.envelope=new QuoteDataEnvelopes(quoteId,null);let firstEnvelopeType=$scope.IndexedChoiceData["77-Envelope Type"][0];for(let i=0;i<$scope.IndexedChoiceData["77-Envelope Type"].length;i++)"default"==$scope.IndexedChoiceData["77-Envelope Type"][i].OptionName&&(firstEnvelopeType=$scope.IndexedChoiceData["77-Envelope Type"][i]);$scope.envelope.materialTypeId=firstEnvelopeType.ChoiceValueId;var envelopeOptions=$scope.getEnvelopes();envelopeOptions.length?$scope.envelope.materialId=envelopeOptions[0].ChoiceValueId:$scope.envelope.materialId=0,$scope.envelope.colourTypeId=0,$scope.setEnvelopeQuantityToQuoteQuantity(),$scope.IndexedChoiceData["77-Envelope Type"].selected=$scope.IndexedChoiceData["77-Envelope Type"][0],$scope.updateEnvelope($scope.ApiQuote.id)}},$scope.envelopesOff=function(){let requestId=$scope.getUpdateId();$scope.priceUpdateSent(),ajaxService.getRequest("quote/data/envelopes/remove/"+$scope.ApiQuote.id,{},function(response,httpCode,optionalData){$scope.priceUpdateReceived(requestId)&&(response=$scope.cleanQuoteDataUpdate(response),restructureService.parseQuoteDataArray(response,$scope))}),$scope.envelope=null},$scope.envelopesOnQuery=function(){return null!==$scope.envelope},$scope.getEnvelope=function(quoteId){ajaxService.getRequest("quote/data/envelopes/"+quoteId,{},function(response,httpCode,optionalData){0<response.length?$scope.envelope=response[0]:$scope.envelope=!1,$scope.$digest()})},$scope.setEnvelopes=function(envelopes){envelopes&&(0<envelopes.length?$scope.envelope=envelopes[0]:$scope.envelope=!1)},$scope.removeEnvelope=function(){$scope.envelope=!1,$scope.IndexedChoiceData["77-Envelope Type"].selected.ChoiceDisplayName=null},$scope.updateEnvelope=function(quoteId){if(quoteId=quoteId||$scope.ApiQuote.id,$scope.envelope)for(var i=0;i<$scope.envelope.length;i++)delete $scope.envelope[i].id,delete $scope.envelope[i].datetimeAdded;if("null"!==$scope.envelope.materialId&&null!==$scope.envelope.materialId){$scope.envelope.quoteId=quoteId;let requestId=$scope.getUpdateId();$scope.priceUpdateSent(),ajaxService.postRequest([$scope.envelope],"quote/data/envelopes",{},function(response){$scope.priceUpdateReceived(requestId)&&$scope.IntegrateNewApiQuoteDataObjectFromChildService(response),$scope.getEnvelope($scope.ApiQuote.id)})}},$scope.envelopeTimeoutHandle=!1,$scope.queueEnvelopeUpdate=function(quoteId){clearTimeout($scope.envelopeTimeoutHandle),$scope.envelopeTimeoutHandle=setTimeout($scope.updateEnvelope,500,quoteId)},$scope.returnSelectedEnvelopeTypeName=function(envelopeObject,optionArray,quoteId){if(envelopeObject){if(optionArray&&0!=optionArray.length){for(var i=0;i<optionArray.length;i++)if(optionArray[i].ChoiceValueId==envelopeObject.materialId)return optionArray[i].ChoiceDisplayName;return"Please select an option"}return"No envelope data available"}return"Please select an option"},$scope.selectEnvelopeType=function(options,selectedOption,quoteId){var originalValueId=$scope.envelope.materialTypeId,selectedOption=($scope.IndexedChoiceData["77-Envelope Type"].selected=selectedOption,$scope.envelope.materialTypeId=selectedOption.ChoiceValueId,$scope.refreshEnvelopeOptions(),$scope.getEnvelopes());selectedOption&&(selectedOption.length?($scope.envelope.materialId=selectedOption[0].ChoiceValueId,$scope.IndexedChoiceData["77-Envelopes"].selected=selectedOption[0]):($scope.envelope.materialTypeId=originalValueId,$scope.showMiniPopup("An envelope of this type for your size can't be found")),$scope.$$phase||$scope.$digest()),$scope.updateEnvelope($scope.ApiQuote.id),$scope.envelope||($scope.envelope=new QuoteDataEnvelopes($scope.ApiQuote.id,"null"),$scope.envelope.colourTypeId=$scope.IndexedChoiceData["77-Print Colour"][0].ChoiceValueId,$scope.envelope.insertPrinted=0,$scope.envelope.materialOptionId=0,$scope.envelope.envelopeQuantity=parseInt($scope.IndexedChoiceData["3-Quantity"].valueDisplay))},$scope.envelopeOptionSelected=function(option){$scope.IndexedChoiceData["77-envelope-options"].selected=option,$scope.envelope.materialOptionId=option.ChoiceValueId,$scope.updateEnvelope(quoteId)},$scope.envelopeOptionSelected=function(option){$scope.IndexedChoiceData["77-envelope-material-option"].selected=option,$scope.IndexedChoiceData["77-envelope-material-option"].displayed=option,$scope.envelope.materialOptionId=option.ChoiceValueId,121==option.ChoiceValueId&&($scope.envelope.colourTypeId=0),$scope.updateEnvelope($scope.ApiQuote.id)},$scope.getEnvelopeMaterialOptionsChildData=function(materialId){materialId="choice/"+$scope.ApiQuote.id+"/"+materialId+"/envelopematerialoptionid";ajaxService.getRequest(materialId,{},function(response,httpCode,optionalData){if(0<response.length){response.forEach(function(row){row.ChoiceValueId=row.choiceValueId,row.ChoiceDisplayName=row.choiceDisplayName}),$scope.IndexedChoiceData["77-envelope-material-option"]||($scope.IndexedChoiceData["77-envelope-material-option"]=[],$scope.IndexedChoiceData["77-envelope-material-option"].selected=new ChoiceObject($scope.ApiQuote.id,-1,"Please select an option"),$scope.IndexedChoiceData["77-envelope-material-option"].displayed=new ChoiceObject($scope.ApiQuote.id,-1,"Please select an option"),$scope.IndexedChoiceData["77-envelope-material-option"].original={valueId:"null"}),$scope.IndexedChoiceData["77-envelope-material-option"].splice(0,$scope.IndexedChoiceData["77-envelope-material-option"].length),response.forEach(function(row){$scope.IndexedChoiceData["77-envelope-material-option"].push(row)});let selectedFound=!1;$scope.IndexedChoiceData["77-envelope-material-option"].forEach(function(row){row.ChoiceValueId==$scope.IndexedChoiceData["77-envelope-material-option"].original.valueId&&(selectedFound=row)}),selectedFound?($scope.IndexedChoiceData["77-envelope-material-option"].selected=selectedFound,$scope.IndexedChoiceData["77-envelope-material-option"].displayed=selectedFound):($scope.IndexedChoiceData["77-envelope-material-option"].selected=$scope.IndexedChoiceData["77-envelope-material-option"][0],$scope.IndexedChoiceData["77-envelope-material-option"].displayed=$scope.IndexedChoiceData["77-envelope-material-option"][0]),$scope.envelope.materialOptionId=$scope.IndexedChoiceData["77-envelope-material-option"].selected.ChoiceValueId,$scope.updateEnvelope($scope.ApiQuote.id)}else $scope.IndexedChoiceData["77-envelope-material-option"]=!1,$scope.envelope.materialOptionId=null;$scope.$digest()})},$scope.envelopeQuantityManuallyChanged=function(envelope,quoteId){envelope.envelopeQuantity<0&&(envelope.envelopeQuantity=0),$scope.queueEnvelopeUpdate(quoteId)},$scope.incrementEnvelopeQuantity=function(envelope,incrementQuantity,quoteId){envelope.matchQuantity||(envelope.envelopeQuantity+=incrementQuantity,envelope.envelopeQuantity<0&&(envelope.envelopeQuantity=0),$scope.queueEnvelopeUpdate(quoteId))},$scope.returnSelectedEnvelopeColour=function(){if($scope.envelope){var choiceArray=$scope.IndexedChoiceData["77-Print Colour"];if(choiceArray&&0<choiceArray.length){for(var i=0;i<choiceArray.length;i++)if(choiceArray[i].ChoiceValueId==$scope.envelope.colourTypeId)return choiceArray[i].ChoiceDisplayName;return"Please select a colour"}}return"No colour data available"},$scope.selectPrintColour=function(option,quoteId){$scope.envelope.colourTypeId=option.ChoiceValueId,$scope.updateEnvelope(quoteId)},$scope.changeInsertedMaterial=function(newBoolValue){$scope.envelope.insertPrinted=newBoolValue,$scope.envelope.insertItemQuantity=1,$scope.queueEnvelopeUpdate($scope.ApiQuote.id)},$scope.incrementEnvInsertQuantity=function(amount){$scope.envelope.insertItemQuantity+=amount,$scope.envelope.insertItemQuantity=$scope.validateEnvInsQuant($scope.envelope.insertItemQuantity),$scope.queueEnvelopeUpdate(scope.ApiQuote.id)},$scope.envInsertQuantityChanged=function(){$scope.envelope.insertItemQuantity=$scope.validateEnvInsQuant($scope.envelope.insertItemQuantity),$scope.queueEnvelopeUpdate(scope.ApiQuote.id)},$scope.validateEnvInsQuant=function(quantity){return quantity=quantity<0?0:quantity},$scope.checkIfEnvelopesNeedRequest=function(){if(!$scope.IndexedChoiceData["77-Envelope Type"])return!1;if(!$scope.IndexedChoiceData["77-Envelope Type"].original.valueId)return!1;if(!$scope.getEnvelopes())return!1;if($scope.getSelectedEnvelope().ChoiceValueId){let found=!1;for(let i=0;i<$scope.getEnvelopes().length;i++)$scope.getEnvelopes()[i].ChoiceValueId===$scope.getSelectedEnvelope().ChoiceValueId&&(found=!0);found||$scope.getEnvelopeMaterialChildData($scope.IndexedChoiceData["77-Envelope Type"].original.valueId)}},$scope.getSelectedEnvelopeMaterialTypeDisplayName=function(){if(!$scope.IndexedChoiceData)return"Please select an option";let options=$scope.IndexedChoiceData["77-Envelope Type"];if(!options)return"Please select an option";$scope.envelope&&$scope.envelope.materialTypeId;var selected=options.find(function(f){return f.ChoiceValueId==$scope.envelope.materialTypeId});return selected?selected.ChoiceDisplayName:"Please select an option"},$scope.validateEnvelopes=function(){if($scope.getEnvelopes()&&$scope.envelope){var material=$scope.getSelectedEnvelope();if($scope.checkEnvelopeDisabled(material)){var options=$scope.getEnvelopes(!0);let found=!1;for(let i=0;i<options.length;i++){var option=options[i];$scope.checkEnvelopeDisabled(option)||($scope.envelopeSelected(option,!0),i=options.length,found=!0)}!found&&$scope.envelope&&($scope.showMiniPopup("Envelope for this size could not be found, envelope removed"),$scope.envelopesOff(),$scope.envelope=!1)}}},$scope.getSelectedEnvelope=function(){return $scope.envelope&&$scope.envelope.materialId?$scope.IndexedChoiceData["77-Envelopes"].find(function(f){return f.ChoiceValueId==$scope.envelope.materialId}):null},$scope.envelopeSelected=function(option,doubleGetPrice){$scope.IndexedChoiceData["77-Envelopes"].selected=option,"Printable"==($scope.IndexedChoiceData["77-Envelopes"].displayed=option).choiceLabel&&($scope.envelope.colourTypeId=0),$scope.getEnvelopeMaterialOptionsChildData(option.ChoiceValueId),$scope.envelope.materialId=option.ChoiceValueId,$scope.updateEnvelope($scope.ApiQuote.id,doubleGetPrice)},$scope.getEnvelopeSizeMaps=function(){ajaxService.getRequest("admin/envelope-size-maps",null,function(response){$scope.envelopeSizeMaps=response,$scope.$$phase||$scope.$digest()})},$scope.checkEnvelopeDisabled=function(material){if(!$scope.IndexedChoiceData["5-Print Size"].selected)return!0;let printSizeId=$scope.IndexedChoiceData["5-Print Size"].selected.ChoiceValueId;return!printSizeId||(!material||!$scope.envelopeSizeMaps.filter(function(f){return f.envelopeMaterialId==material.ChoiceValueId&&f.printSizeId==printSizeId}).length)},$scope.envelopeSizeMaps=[],$scope.envelopeOptions=null,$scope.getEnvelopes=function(refreshEnvelopes){return $scope.envelopeOptions&&!refreshEnvelopes||$scope.refreshEnvelopeOptions(),$scope.envelopeOptions},$scope.refreshEnvelopeOptions=function(){if($scope.envelope){let parentId=$scope.envelope.materialTypeId;$scope.envelopeOptions=$scope.IndexedChoiceData["77-Envelopes"].filter(function(f){return f.ChoiceParentId==parentId&&!$scope.checkEnvelopeDisabled(f)})}},$scope.setEnvelopeQuantityToQuoteQuantity=function(){for(var totalQuantity=0,i=0;i<$scope.Quantity.length;i++)totalQuantity+=$scope.Quantity[i].versionQuantity*$scope.Quantity[i].versionTypeCount;$scope.envelope.envelopeQuantity=totalQuantity},$scope.toggleMatchQuoteQuantity=function(){$scope.envelope.matchQuantity=!$scope.envelope.matchQuantity,$scope.envelope.matchQuantity&&$scope.envelope&&$scope.envelope.matchQuantity&&$scope.setEnvelopeQuantityToQuoteQuantity(),$scope.updateEnvelope($scope.ApiQuote.id)},restructureService.onUpdateAddEvent(function($scope){$scope.validateEnvelopes()},[$scope])}});