/* ------------------------------------------------------------------------------

 *

 *  # Echarts - candlestick and other charts

 *

 *  Candlestick and other chart configurations

 *

 *  Version: 1.0

 *  Latest update: August 1, 2015

 *

 * ---------------------------------------------------------------------------- */



$(function () {



     function padDigits(number, digits) {

     	return Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;

	 }

	 

	 

	 clearInterval(timeTicket);

	 var timeTicket = setInterval(function () {

                

	 	$.ajax({

         	url: "dashboard/datameter",

         	type:"post",

         	success: function(data) {

           			 

					 dataku = JSON.parse(data);		   

		   			 for ( var i = 0; i < dataku.length; i++) {		

					    	   	 

				 	 	 var obj = dataku[i];

					 	 var c  = obj.id_meter;

					 	 var back  = 'back_'+c;
				
					 	 var dt_back  = 'dt_back_'+c;									

					 	 var kwh  = obj.kwh;
					
					 	 var dt  = obj.date_time;		
					
					 	 var id_satuan  = 'satuan_'+c;	
					
					 	 var id_back_satuan  = 'back_satuan_'+c;	
					
					 	 var val_satuan  = '';						

						 if(kwh>0){ 

							if (kwh > 9999999999999) {
								kwh = kwh / 1000;
								val_satuan  = 'mWh';
							} else {
								val_satuan  = 'kWh';
							}
								
					 	 	document.getElementById(c).innerHTML  = padDigits(kwh,13);

							document.getElementById(back).innerHTML  = padDigits(kwh,13);	

							document.getElementById(dt_back).innerHTML  = dt;
							
							document.getElementById(id_satuan).innerHTML  = val_satuan;
							
							document.getElementById(id_back_satuan).innerHTML  = val_satuan;
						 }

						 else if(kwh==0){ 

							val_satuan  = 'kWh';
							
						    document.getElementById(c).innerHTML  = '0000000000000';

							document.getElementById(back).innerHTML  = '0000000000000';
							
							document.getElementById(dt_back).innerHTML  = dt;
							
							document.getElementById(id_satuan).innerHTML  = val_satuan;
							
							document.getElementById(id_back_satuan).innerHTML  = val_satuan;

						 }

						 else{ 

						 	var kwh2  = kwh*(-1);
						
							if (kwh2 < -9999999999999) {
								kwh2 = kwh2 / 1000;
								val_satuan  = 'mWh';
							} else {
								val_satuan  = 'kWh';
							}

						    document.getElementById(c).innerHTML  = '-'+padDigits(kwh2,13);

						    document.getElementById(back).innerHTML  = '-'+padDigits(kwh2,13);
						
							document.getElementById(dt_back).innerHTML  = dt;
							
							document.getElementById(id_satuan).innerHTML  = val_satuan;
							
							document.getElementById(id_back_satuan).innerHTML  = val_satuan;

						 }

						 

						 			

					 }

					             	      

         	}                 

                  

   	 	});

				

	}, 2000);                 



       





});

