'use strict';angular.module('twoBookApp.chart-performance',['ui.router']).component('chartPerformance',{templateUrl:'chart-performance/chart-performance.template.html',bindings:{tboHeight:'=',tboDashboard:'=',},controller:['$http','$timeout','RequestsErrorHandler','SessionService','$filter','$scope','$rootScope','$injector','ErrorDialogEmbedded',function($http,$timeout,RequestsErrorHandler,SessionService,$filter,$scope,$rootScope,$injector,ErrorDialogEmbedded){var self=this;var _chartOtions;var _performance;var _chartHeight;var _level;var _updateChart;var _period;var _lea;this._chartHeight=this.tboHeight;this._updateChart=false;this._level=1;this._lea=0;this._noData=false;this.setHeight=function(height){$timeout(function(){self.tboHeight=height;});}
this._chartOptions={culture:"de-DE",transitions:true,seriesColors:["darkgreen","darkred","darkblue"],series:[{field:'Income',name:'Einnahmen',type:"column",tooltip:{visible:true,template:"
#= series.name #
"+"#= kendo.format('{0:C0}',value) #
"+"#if (dataItem.IncomeDiff != null) {#"+"VJ: #= tboNumberRelation(dataItem.IncomeDiff) #
"+"#}#"},},{field:'Expense',name:'Ausgaben',type:"column",tooltip:{visible:true,template:"#= series.name #
"+"#= kendo.format('{0:C0}',value) #
"+"#if (dataItem.ExpenseDiff != null) {#"+"VJ: #= tboNumberRelation(dataItem.ExpenseDiff)#
"+"#}#"},},{field:'Win',name:'Gewinn',type:"line",axis:"Win",tooltip:{visible:true,template:"#= series.name #
"+"#= kendo.format('{0:C0}',value) #
"+"#if (dataItem.WinDiff != null) {#"+"VJ: #= tboNumberRelation(dataItem.WinDiff)#
"+"#}#"},color:function(data){if(data.value<0){return"red";}else{return"black";}},}],axisDefaults:{majorGridLines:{visible:false,}},valueAxes:[{title:{text:"Einnahmen/Ausgaben [Eur]"},labels:{format:"{0:n0}"},line:{visible:true},axisCrossingValue:0,},{name:"Win",title:{text:"Gewinn [Eur]"},labels:{format:"{0:n0}"},line:{visible:true},color:"darkblue",axisCrossingValue:0,}],categoryAxis:{field:'Year',line:{visible:true},labels:{template:"",},axisCrossingValues:[0,200]},}
this._performance=new kendo.data.DataSource({transport:{read:function(e){RequestsErrorHandler.specificallyHandled(function(){SessionService.getCurrentBookKeeping().then(function(response){self._lea=response.Lea;self._clientName=response.ClientName;$http({method:'GET',url:SessionService.getApiUrl()+'/api/charts',headers:{'Content-Type':'text/xml','lea':response.Lea,'year':response.Year,'type':'performance'}}).then(function(response){if(response.data.length==0){self._noData=true;}else{self._noData=false;var opt=$('#tboChartPerformance').data("kendoChart").options;if(!self.tboDashboard){opt.title={text:"Jänner bis "+decodeHtml(monthNamesLong[response.data[0].Ytm-1]),font:"24px Arial,Helvetica,sans-serif"};}else{opt.title={text:"Jänner bis "+decodeHtml(monthNamesLong[response.data[0].Ytm-1]),font:"12px Arial,Helvetica,sans-serif"};}}
e.success(response.data)},function(response){self._errorMessage=response.statusText;e.error("XHR response",response.error,response.statusText);});})})}},sort:[{field:"Year",dir:"asc"},{field:"Month",dir:"asc"}],filter:[{field:"Month",operator:"eq",value:98},]});this.seriesClick=function(e){if(self.tboDashboard){return;}
if(e.dataItem.Month==99||e.dataItem.Month==98){self._performance.filter([{field:"Year",operator:"eq",value:e.dataItem.Year},{field:"Month",operator:"lte",value:12}]);self._chartOptions.title={text:""+e.dataItem.Year,font:"24px Arial,Helvetica,sans-serif"};self._chartOptions.categoryAxis.field="Month";self._chartOptions.categoryAxis.labels.template="#= decodeHtml(monthNamesShort[value-1]) #";self._level=2;self._period={Year:e.dataItem.Year,Month:0};self._updateChart=!self._updateChart;$scope.$apply();$('#tboChartPerformance').data("kendoChart").redraw();}else{self._level=3;self._updateChart=!self._updateChart;self._period={Year:e.dataItem.Year,Month:e.dataItem.Month};$scope.$apply();}}
this.up=function(){if(this._level==2){self._performance.filter([{field:"Month",operator:"eq",value:98}]);self._chartOptions.title={text:"Jänner bis "+decodeHtml(monthNamesLong[self._performance._data[0].Ytm-1]),font:"24px Arial,Helvetica,sans-serif"};self._chartOptions.categoryAxis.field="Year";self._chartOptions.categoryAxis.labels.template="";self._level=1;self._updateChart=!self._updateChart;}else if(this._level==3){self._level=2;$timeout(function(){self._updateChart=!self._updateChart});}
$('#tboFocus').focus();}
this.pdf=function(){kendo.drawing.drawDOM($(".k-content"),{template:''+''+''+'
',}).then(function(group){var PAGE_RECT=new kendo.geometry.Rect([0,0],[mm(297-20),mm(210-20)]);var content=new kendo.drawing.Group();content.append(group);kendo.drawing.fit(content,PAGE_RECT)
return kendo.drawing.exportPDF(content,{paperSize:"A4",margin:"1cm",landscape:true,});}).done(function(data){kendo.saveAs({dataURI:data,fileName:"2book_Einnahmen_Ausgaben"+(self._level==3?'_'+self._period.Month:'')+(self._level>1?'_'+self._period.Year:'')+".pdf",});});$('#tboFocus').focus();}
this._bkListener=$rootScope.$on('tboCurrentBookKeeping',function(event,data){DEBUG&&console.log("chart performance received book keeping change");var chart=$("#tboChartPerformance").getKendoChart();if(self._lea!=data.Lea){chart.dataSource.read();}});$scope.$on('$destroy',self._bkListener);}]});