
<?php \Laravel\Section::start('content'); ?>

<script type="text/javascript">
function date_heure(id)
{
  date = new Date;
  annee = date.getFullYear();
  moi = date.getMonth();
  mois = new Array("<?php echo __('admin.janvier'); ?>", "<?php echo __('admin.fevrier'); ?>", "<?php echo __('admin.mars'); ?>", "<?php echo __('admin.avril'); ?>", "<?php echo __('admin.mai'); ?>", "<?php echo __('admin.juin'); ?>", "<?php echo __('admin.juillet'); ?>", "<?php echo __('admin.aout'); ?>", "<?php echo __('admin.septembre'); ?>", "<?php echo __('admin.octobre'); ?>", "<?php echo __('admin.novembre'); ?>", "<?php echo __('admin.decembre'); ?>");
  j = date.getDate();
  jour = date.getDay();
  jours = new Array("<?php echo __('admin.dimanche'); ?>", "<?php echo __('admin.lundi'); ?>", "<?php echo __('admin.mardi'); ?>", "<?php echo __('admin.mercredi'); ?>", "<?php echo __('admin.jeudi'); ?>", "<?php echo __('admin.vendredi'); ?>", "<?php echo __('admin.samedi'); ?>");
  h = date.getHours();
  if(h<10)
  {
          h = "0"+h;
  }
  m = date.getMinutes();
  if(m<10)
  {
          m = "0"+m;
  }
  s = date.getSeconds();
  if(s<10)
  {
          s = "0"+s;
  }
  resultat = jours[jour]+' '+j+' '+mois[moi]+' '+annee+', '+h+':'+m+':'+s;
  document.getElementById(id).innerHTML = resultat;
  setTimeout('date_heure("'+id+'");','1000');
  return true;
}
</script>

<div class="container">
  
  <div class="alert alert-success">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <?php echo __('admin.stats_a_ce_jour'); ?> : <span id="date_heure"></span></a><script type="text/javascript">window.onload = date_heure('date_heure');</script>.
  </div>
  
  <div class="hero-unit heroPerso">
    <h2><?php echo __('admin.panneau_controle'); ?> </h2>
    <p>Bienvenue dans le back-office.</p>
  </div>
  
  <div style="width:300px;margin-left:auto;margin-right:auto">
  	<div id="date-range" style="z-index:20000;"><div id="date-range-field"><span></span><a href="#">&#9660;</a></div><div id="datepicker-calendar"></div></div>
  </div>
  
  <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  <div id="chart_div" style="width: 100%; height: 500px;"></div>
  <div style="text-align:center;margin-bottom:20px;width:400px;margin-left:auto;margin-right:auto;">
	  <div class="well">
		  <h4>CA Total période : <span id="total_periode"></span></h4>
		  <div>
			<strong>CA mois en cours : <?php echo number_format($mois_en_cours['ca'], 2, ',', ' '); ?> € HT - <?php echo $mois_en_cours['nb_factures']; ?> facture(s)</strong><br />
			<strong>CA mois précédent : <?php echo number_format($mois_precedent['ca'], 2, ',', ' '); ?> € HT - <?php echo $mois_precedent['nb_factures']; ?> facture(s)</strong><br />
			<strong>CA moyen par mois en <?php echo date("Y"); ?> : <?php echo number_format($moyenne_du_mois_annee_en_cours, 2, ',', ' '); ?> € HT </strong><br />
			<strong>CA <?php echo date("Y"); ?> : <?php echo number_format($annee_en_cours, 2, ',', ' '); ?> € HT - <?php echo $annee_en_cours_nb_fact; ?> facture(s)</strong><br />
		  </div>
	  </div>
  </div>

	<script type="text/javascript">
	function add_zero(input){
		if(input<10){
			return '0'+input;
		}
		else{
			return input;
		}
	}
	
	var to = new Date();
	var from = new Date(to.getTime() - 1000 * 60 * 60 * 24 * 90);
	
	var date_from = from.getFullYear()+'-'+('0'+(from.getMonth()+1)).slice(-2)+'-'+from.getDate();
	var date_to = to.getFullYear()+'-'+('0'+(to.getMonth()+1)).slice(-2)+'-'+to.getDate();
	
	google.load("visualization", "1", {packages:["corechart"]});
	google.setOnLoadCallback(drawChart);
	function drawChart() {
		var json = $.ajax({
			url: '<?php echo URL::to_route('ajaxgraph'); ?>?from='+date_from+'&to='+date_to,
			dataType: 'json',
			async: false
		}).responseText;
		var data = new google.visualization.DataTable(json);
		
		// Met à jour le CA total de la période 
		var total_periode = 0;
		var parsedJson = $.parseJSON(json);
		$.each(parsedJson.rows, function(key, value) {
			// Mois en cours
	        //alert(key + " " + value.c[0].v);
	        
	        // Total
	        //alert(key + " " + value.c[1].v);
	        total_periode += parseFloat(value.c[1].v);
	    });
	    $("#total_periode").html(total_periode.toFixed(2)+" € HT");
		
		var options = {
		  title: 'CA par période',
		  width: $("#chart_div").width(),
		  seriesType: "bars",
		};
		
		var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
		chart.draw(data, options);
	}

  /* Special date widget */
  $(document).ready(function() {
	$('#datepicker-calendar').DatePicker({
	  inline: true,
	  date: [from, to],
	  calendars: 3,
	  mode: 'range',
	  current: new Date(to.getFullYear(), to.getMonth() - 1, 1),
	  onChange: function(dates,el) {
	    // Quand on séléctionne une nouvelle date, alors on met à jour le input
	    $('#date-range-field span').text(dates[0].getDate()+' '+dates[0].getMonthName(true)+' '+dates[0].getFullYear()+' - '+dates[1].getDate()+' '+dates[1].getMonthName(true)+' '+dates[1].getFullYear());
		
		date_from = dates[0].getFullYear()+'-'+('0'+(dates[0].getMonth()+1)).slice(-2)+'-'+add_zero(dates[0].getDate());
	  	date_to = dates[1].getFullYear()+'-'+('0'+(dates[1].getMonth()+1)).slice(-2)+'-'+add_zero(dates[1].getDate());

	  	drawChart();
	  }
	});
	
	// Place la bonne date lors du chargement de page
	$('#date-range-field span').text(from.getDate()+' '+from.getMonthName(true)+' '+from.getFullYear()+' - '+to.getDate()+' '+to.getMonthName(true)+' '+to.getFullYear());
	
	// bind a click handler to the date display field, which when clicked
	// toggles the date picker calendar, flips the up/down indicator arrow,
	// and keeps the borders looking pretty
	$('#date-range-field').bind('click', function(){
	  $('#datepicker-calendar').toggle();
	  if($('#date-range-field a').text().charCodeAt(0) == 9660) {
	    // switch to up-arrow
	    $('#date-range-field a').html('&#9650;');
	    $('#date-range-field').css({borderBottomLeftRadius:0, borderBottomRightRadius:0});
	    $('#date-range-field a').css({borderBottomRightRadius:0});
	  } else {
	    // switch to down-arrow
	    $('#date-range-field a').html('&#9660;');
	    $('#date-range-field').css({borderBottomLeftRadius:5, borderBottomRightRadius:5});
	    $('#date-range-field a').css({borderBottomRightRadius:5});
	  }
	  return false;
	});
	
	// global click handler to hide the widget calendar when it's open, and
	// some other part of the document is clicked.  Note that this works best
	// defined out here rather than built in to the datepicker core because this
	// particular example is actually an 'inline' datepicker which is displayed
	// by an external event, unlike a non-inline datepicker which is automatically
	// displayed/hidden by clicks within/without the datepicker element and datepicker respectively
	$('html').click(function() {
	  if($('#datepicker-calendar').is(":visible")) {
	    $('#datepicker-calendar').hide();
	    $('#date-range-field a').html('&#9660;');
	    $('#date-range-field').css({borderBottomLeftRadius:5, borderBottomRightRadius:5});
	    $('#date-range-field a').css({borderBottomRightRadius:5});
	  }
	});
	
	// stop the click propagation when clicking on the calendar element
	// so that we don't close it
	$('#datepicker-calendar').click(function(event){
	  event.stopPropagation();
	});
  });
  /* End special page widget */
  </script>
  
  <div class="sortable row-fluid">
      <?php if(Auth::user()->has_permission(5)): ?>
      <a class="well span3 apercu1" href="<?php echo  URL::to_route('customers') ; ?>">
        <i class="icon-briefcase"></i>
        <div><?php echo __('admin.clients'); ?></div>
        <span class="badge badge-success"><?=$customers;?></span>
      </a>
      <?php endif; ?>
      <?php if(Auth::user()->has_permission(28)): ?>
      <a class="well span3 apercu1" href="<?php echo  URL::to_route('references') ; ?>">
    		<i class="icon-list-alt"></i>
    		<div>Références</div>
    		<span class="badge badge-info"><?=$references;?></span>
      </a>
      <?php endif; ?>
      <?php if(Auth::user()->has_permission(22)): ?>
      <a class="well span3 apercu1" href="<?php echo  URL::to_route('factures') ; ?>">
        <i class="icon-shopping-cart"></i>
        <div>Factures</div>
        <span class="badge badge-warning"><?=$factures;?></span>
      </a>
      <?php endif; ?>
      <?php if(Auth::user()->has_permission(21)): ?>
      <a class="well span3 apercu1" href="<?php echo  URL::to_route('devis') ; ?>">
    		<i class="icon-eye-open"></i>
    		<div>Devis</div>
    		<span class="badge badge-inverse"><?=$devis;?></span>
      </a>
      <?php endif; ?>
  </div>
  
  <div class="row-fluid">
    <ul class="thumbnails">
      <li class="span4">
        <div class="thumbnail">
          <div class="caption">
            <h4><i class="icon-search"></i> ckc-net.com</h4>
            <p><?php echo __('admin.ouvrir_site'); ?></p>
            <p><a href="<?php echo  URL::base() ; ?>" target="_blank" class="btn btn-success"><i class="icon-wrench icon-white"></i> <?php echo __('admin.gerer'); ?></a></p>
          </div>
        </div>
      </li>
      <?php if(Auth::user()->has_permission(22)): ?>
      <li class="span4">
        <div class="thumbnail">
          <div class="caption">
            <h4><i class="icon-list"></i> Blog</h4>
            <p>Afficher les articles du blog et en rédiger de nouveaux</p>
            <p><a href="<?php echo  URL::to_route('pagesblog') ; ?>" class="btn btn-success"><i class="icon-wrench icon-white"></i> <?php echo __('admin.gerer'); ?></a></p>
          </div>
        </div>
      </li>
      <?php endif; ?>
      <li class="span4">
        <div class="thumbnail">
          <div class="caption">
            <h4><i class="icon-align-left"></i> <?php echo __('admin.google_analytics'); ?></h4>
            <p><?php echo __('admin.google_analytics_administration'); ?></p>
            <p><a href="http://www.google.com/analytics/" target="_blank" class="btn btn-success"><i class="icon-wrench icon-white"></i> <?php echo __('admin.gerer'); ?></a></p>
          </div>
        </div>
      </li>
    </ul>
  </div>

</div>

<?php \Laravel\Section::stop(); ?>
<?php echo view('backend::template_default')->with(get_defined_vars())->render(); ?>