<div id="menu1" class="tab-content-active">
	<h3><?php _e('Informations de votre site: ','themoneytizer'); echo $themoney_website->site_url;?></h3>
	<br/>
	<select id="periode" name="periode" style="width:220px;">
		<option value="main_30"><?php _e('Les 30 derniers jours','themoneytizer');?></option>
		<option value="main_month"><?php _e('Le mois dernier','themoneytizer');?></option>
		<option value="main_year"><?php _e('Depuis le début de l\'année','themoneytizer');?></option>
		<option value="custom_date"><?php _e('Choisissez les dates','themoneytizer');?></option>
	</select>
	<div id="custom_date" style="display: none;">
		<?php _e('Du','themoneytizer');?> <input type="text" id="custom_date_from" name="custom_date_from" style="width: 251px;"> <?php _e('au','themoneytizer');?> <input type="text" id="custom_date_to" name="custom_date_to" style="width: 251px;">
	</div>
	<select id="formats" name="formats" style="width:150px;">
		<option value><?php _e('Tous les formats','themoneytizer');?></option>
		<?php
		$themoney_spaces = $themoney_website->site_tags;
		foreach ($themoney_spaces as $space){
			echo "<option value='".$space->tag_name."'>".__($space->tag_name,'themoneytizer')."</option> ";
		}
		?>
	</select>
	<input type="submit"  id="form_stats" name="form_stats" class="bouton_money" value="<?php _e('Voir les stats','themoneytizer');?>" style="width: 204px;">
	<br/><br/><br/><br/>

	<div id="chart_div" style='margin-top:-65px;'></div>

    <!--<div id="stats_revenue" style="width: 100%; height: 100px; margin-bottom: 20px; text-align: center;"></div>
    <div id="format_visibility_chart" style="width: 100%; margin-bottom: 20px; text-align: center;"></div>

    <center>
        <div id="choice_date">
        <?php /*_e('Du','themoneytizer');*/?> <input type="text" id="custom_date_from" name="custom_date_from" style="width: 251px;"> <?php /*_e('au','themoneytizer');*/?> <input type="text" id="custom_date_to" name="custom_date_to" style="width: 251px;">
        <input type="button" id="search_revenue" name="search_revenue" class="bouton_money" value="<?php /*echo _e('Appliquer'); */?>">
        </div>
    </center>

	<br><center><h2><span id="total"></span></h2></center><br>

	<center>
        <div id="dl_stats"><input type="button" class="download_stats bouton_money" onclick="download_stats()" value="<?php /*echo _e('Télécharger vos stats','themoneytizer'); */?>"></div>
        <input type="submit"  id="stats_csv" name="stats_csv" class="bouton_money" value="<?php /*_e('Télécharger vos stats','themoneytizer');*/?>" style="width: 204px;">
    </center>-->
</div>

<!--<input type="hidden" name="update_graph" value="0" id="update_graph" />
<script>
  jQuery_money(document).ready(function() {
    var colorsLine = [
      {'color':'#3366CB','disabledColor':'#d5e0f5'},
      {'color':'#DC3912','disabledColor':'#facbc0'},
      {'color':'#FF9900','disabledColor':'#ffebcc'},
      {'color':'#109618','disabledColor':'#c6f9c9'},
      {'color':'#990099','disabledColor':'#ffe5ff'},
      {'color':'#0099c6','disabledColor':'#dff8ff'},
      {'color':'#dd4477','disabledColor':'#fcf1f5'},
      {'color':'#66aa00','disabledColor':'#e7ffc3'},
      {'color':'#b82e2e','disabledColor':'#faebeb'},
      {'color':'#316395','disabledColor':'#d4e2f1'},
      {'color':'#994499','disabledColor':'#edd6ed'},
      {'color':'#22aa99','disabledColor':'#d5f6f2'},
      {'color':'#aaaa11','disabledColor':'#f9f9c1'},
      {'color':'#6633cc','disabledColor':'#f0ebfa'},
      {'color':'#e67300','disabledColor':'#fff2e6'},
      {'color':'#8b0707','disabledColor':'#fcc8c8'},
      {'color':'#651067','disabledColor':'#f2b5f4'},
      {'color':'#329262','disabledColor':'#d3f0e1'},
      {'color':'#5574a6','disabledColor':'#ebeff5'},
      {'color':'#3b3eac','disabledColor':'#ededf9'},
      {'color':'#b77322','disabledColor':'#f9eddf'},
      {'color':'#16d620','disabledColor':'#eefdef'},
      {'color':'#b91383','disabledColor':'#fde8f6'},
      {'color':'#f4359e','disabledColor':'#fff6fb'},
      {'color':'#9c5935','disabledColor':'#f3e5dd'},
      {'color':'#a9c413','disabledColor':'#f6fbdb'},
      {'color':'#2a778d','disabledColor':'#eff8fa'},
      {'color':'#668d1c','disabledColor':'#f1f9e2'},
      {'color':'#bea413','disabledColor':'#fbf5d5'},
      {'color':'#0c5922','disabledColor':'#e8fced'},
      {'color':'#743411','disabledColor':'#fcf3ee'},
      {'color':'#3366cc','disabledColor':'#ebf0fa'},
    ];
    var chart, dataView, columns;
    var chartVisibility, dataViewVisibility, columnsVisibility;
    var nullFunc = function() {return null;};

    graph();
    jQuery_money('#search_revenue').click(function () {
      jQuery_money('#update_graph').val(0);

     /* graph();*/
      drawChart();
    });

    function graph(){
      jQuery_money('#stats_revenue').html('<img src="<?/*= $themoney_website->path_format_img . 'loading.gif'; */?>" width="50" height="50" style="margin-top: 20px;" />');

      /*google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);*/
      google.load("visualization", "1", {'packages':['corechart']});
      google.setOnLoadCallback(drawChart);
    }

    function drawChart() {
      if (jQuery_money('#update_graph').val() == 0) {
        columns = [];

        var ajaxData = {
          action: 'get_stats_visibility',
          site_id: '<?php /*//echo $themoney_website->site_id; */?>',
          date_from: jQuery_money('#custom_date_from').val(),
          date_to: jQuery_money('#custom_date_to').val(),
        };

        /*var jsonData = jQuery_money.ajax({
          url: the_ajax_script.ajaxurl,
          async: false,
          type: 'POST',
          data: {
            action: 'get_stats_vanilla',
            site_id: '<?php /*echo $themoney_website->site_id; */?>',
            date_from: jQuery_money('#custom_date_from').val(),
            date_to: jQuery_money('#custom_date_to').val(),
          },
        }).responseText;*/

        jQuery_money.post(the_ajax_script.ajaxurl, ajaxData, function(response) {
          var jsonData = response;
          jQuery_money("#chart_div").empty();
          var data = new google.visualization.DataTable();
          console.log(jsonData);
          if (jsonData === '') {
            jQuery_money('#category_show_revenue #site_active span').html(affichage_perf_total);
            jQuery_money('#stats_revenue').html('<?php /*echo __('Aucune statistique trouvée sur la période demandée'); */?>').css({'margin':'30px 0', 'height':'auto'});
            return;
          }

          jsonData = jQuery_money.parseJSON(response);

          var formats = jsonData.formats;
          var formats_perf_date = jsonData.formats_perf_date;
          var perf_total = jsonData.perf_total;
          var affichage_perf_total = jsonData.affichage_perf_total;
          var perf_formats = jsonData.perf_formats;
          var ht = jsonData.ht;
          var currency = jsonData.currency;

          if(perf_total == 0){
            jQuery_money('#category_show_revenue #site_active span').html(affichage_perf_total);
            jQuery_money('#stats_revenue').html('<?php /*echo __('Aucune statistique trouvée sur la période demandée'); */?>').css({'margin':'30px 0', 'height':'auto'});

            for (var i = 0; i < perf_formats.length; i++){
              jQuery_money('#bloc_format_' + perf_formats[i].id + ' > div:nth-child(3) > div:nth-child(1) > span').html(perf_formats[i].affichage_cpm);
              jQuery_money('#bloc_format_' + perf_formats[i].id + ' > div:nth-child(3) > div:nth-child(2) > span').html(perf_formats[i].affichage_revenues);
            }

            return;
          }

          data.addColumn('string', 'Date');
          columns.push({'type':'string', 'label':'Date'});

          for (var i = 0; i < formats.length; i++){
            data.addColumn('number', formats[i].name);

            var visible = false;
            if(formats[i].name == "TOTAL" || formats[i].name == "CPM TOTAL"){
              visible = true;
            }

            columns.push({'type':'number', 'label':formats[i].name, 'color': colorsLine[i].color, 'visible': visible, 'disabledColor':colorsLine[i].disabledColor});
          }

          for (var i = 0; i < formats_perf_date.length; i++){
            data.addRow(formats_perf_date[i].date);
          }

          jQuery_money('#category_show_revenue #site_active span').html(affichage_perf_total);

          for (var i = 0; i < perf_formats.length; i++){
            jQuery_money('#bloc_format_' + perf_formats[i].id + ' > div:nth-child(3) > div:nth-child(1) > span').html(perf_formats[i].affichage_cpm);
            jQuery_money('#bloc_format_' + perf_formats[i].id + ' > div:nth-child(3) > div:nth-child(2) > span').html(perf_formats[i].affichage_revenues);
          }

          dataView = new google.visualization.DataView(data);

          chart = new google.visualization.ComboChart(document.getElementById('stats_revenue'));

          jQuery_money('#update_graph').val(1);

          localStorage['columns'] = JSON.stringify(columns);
          localStorage['currency'] = JSON.stringify(currency);
          localStorage['ht'] = JSON.stringify(ht);

          // Toggle visibility of data series on click of legend.
          google.visualization.events.addListener(chart, 'click', function (target) {
            if (target.targetID.match(/^legendentry#\d+$/)) {
              var index = parseInt(target.targetID.slice(12)) + 1;

              if(columns[index].label == "TOTAL" || columns[index].label == "CPM TOTAL"){
                if(columns[index].visible == false){
                  columns[1].visible = true;
                  columns[2].visible = true;
                }else{
                  columns[1].visible = false;
                  columns[2].visible = false;
                }

                for (var i = 0; i < columns.length; i++){
                  if(columns[i].label != "TOTAL" && columns[i].label != "CPM TOTAL"){
                    columns[i].visible = false;
                  }
                }
              } else{
                if(columns[index].label.match(/^CPM /)){
                  if(columns[index].visible == false){
                    columns[index].visible = true;
                    columns[index + 1].visible = true;
                  }else{
                    columns[index].visible = false;
                    columns[index + 1].visible = false;
                  }
                } else{
                  if(columns[index].visible == false){
                    columns[index].visible = true;
                    columns[index - 1].visible = true;
                  }else{
                    columns[index].visible = false;
                    columns[index - 1].visible = false;
                  }
                }

                if(columns[index].label != "CPM TOTAL" && columns[index].label != "TOTAL"){
                  for (var i = 0; i < columns.length; i++){
                    if(columns[i].label == "TOTAL" || columns[i].label == "CPM TOTAL"){
                      columns[i].visible = false;
                    }
                  }
                }
              }

              //console.log(columns);

              localStorage['columns'] = JSON.stringify(columns);

              drawChart();
            }
          });

          google.visualization.events.addListener(chart, 'onmouseover', function (target) {
            jQuery_money('#stats_revenue').css('cursor','pointer');
          });
          google.visualization.events.addListener(chart, 'onmouseout', function (target) {
            jQuery_money('#stats_revenue').css('cursor','default');
          });

          // VISIBILITY START 1

          columnsVisibility = [];

          var ajaxDataVisibility = {
            action: 'get_stats_visibility',
            site_id: '<?php /*echo $themoney_website->site_id; */?>',
            date_from: jQuery_money('#custom_date_from').val(),
            date_to: jQuery_money('#custom_date_to').val(),
          };


          /*var jsonDataVisibility = jQuery_money.ajax({
            url: the_ajax_script.ajaxurl,
            async: false,
            type: 'POST',
            data: {
              action: 'get_stats_visibility',
              site_id: '<?php /*echo $themoney_website->site_id; */?>',
              date_from: jQuery_money('#custom_date_from').val(),
              date_to: jQuery_money('#custom_date_to').val(),
            },
          }).responseText;*/

          jQuery_money.post(the_ajax_script.ajaxurl, ajaxDataVisibility, function(response) {
            jQuery_money("#chart_div").empty();
            var jsonDataVisibility = response;

            var dataVisibility = new google.visualization.DataTable();

            console.log(jsonDataVisibility);
            if (jsonDataVisibility === '') {
              jQuery_money('#category_show_revenue #site_active span').html(affichage_perf_total);
              jQuery_money('#stats_revenue').html('<?php /*echo __('Aucune statistique trouvée sur la période demandée'); */?>').css({'margin':'30px 0', 'height':'auto'});
              return;
            }

            jsonDataVisibility = jQuery_money.parseJSON(jsonDataVisibility);
            for (var i = 1; i<jsonDataVisibility.visibility.length; i++) {
              jsonDataVisibility.visibility[i] = Object.values(jsonDataVisibility.visibility[i])
            }

            var perfFormatsVisibility = jsonDataVisibility.visibility;
            console.log(perfFormatsVisibility);

            if(perfFormatsVisibility[0].length === 1){
              jQuery_money('#format_visibility_chart').html('<?php /*echo _('Aucune statistique trouvée sur la période demandée'); */?>').css({'margin':'30px 0', 'height':'auto'});

              return;
            }

            dataVisibility.addColumn('string', 'Date');
            columnsVisibility.push({'type':'string', 'label':'Date'});
            var formatForVisibility = perfFormatsVisibility[0].slice(1);

            for (var i = 0; i < formatForVisibility.length; i++){
              dataVisibility.addColumn('number', formatForVisibility[i]);

              var visible = false;
              if(i === 0){
                visible = true;
              }

              columnsVisibility.push({'type':'number', 'label':formatForVisibility[i], 'visible': visible, 'color': colorsLine[i].color, 'disabledColor':colorsLine[i].disabledColor});
            }

            for (var i = 1; i < perfFormatsVisibility.length; i++){
              dataVisibility.addRow(perfFormatsVisibility[i]);
            }

            dataViewVisibility = new google.visualization.DataView(dataVisibility);
            chartVisibility = new google.visualization.LineChart(document.getElementById('format_visibility_chart'));

            localStorage['columnsVisibility'] = JSON.stringify(columnsVisibility);

            // Toggle visibility of data series on click of legend.
            google.visualization.events.addListener(chartVisibility, 'click', function (target) {
              if (target.targetID.match(/^legendentry#\d+$/)) {
                var index = parseInt(target.targetID.slice(12)) + 1;

                if(columnsVisibility[index].visible === false){
                  columnsVisibility[index].visible = true;
                }
                else{
                  columnsVisibility[index].visible = false;
                }

                localStorage['columnsVisibility'] = JSON.stringify(columnsVisibility);

                drawChart();
              }
            });

            google.visualization.events.addListener(chartVisibility, 'onmouseover', function (target) {
              jQuery_money('#format_visibility_chart').css('cursor','pointer');
            });
            google.visualization.events.addListener(chartVisibility, 'onmouseout', function (target) {
              jQuery_money('#format_visibility_chart').css('cursor','default');
            });

            // VISIBILITY END 1
          });
        });
      }

      var visibleColumnIndexes = [0];
      var colors = [];

      columns = jQuery_money.parseJSON(localStorage['columns']);
      currency = jQuery_money.parseJSON(localStorage['currency']);
      ht = jQuery_money.parseJSON(localStorage['ht']);

      for (var i = 1; i < columns.length; i++) {
        if (columns[i].visible) {
          colors.push(columns[i].color);

          visibleColumnIndexes.push(i);
        } else {
          colors.push(columns[i].disabledColor);

          visibleColumnIndexes.push({
            calc: nullFunc,
            type: columns[i].type,
            label: columns[i].label,
          });
        }
      }

      var options = {
        title: '<?php /*echo __('Cliquez sur le format de votre choix pour voir les stats apparaître'); */?>',
        titleTextStyle: {
          italic: true,
          color: 'gray',
          fontName: 'Oswald',
          fontSize: 18
        },
        focusTarget: 'category',
        tooltip: {isHtml: true},
        //hAxis: {title: 'Date'},
        vAxis: {title: currency + ' ' + ht,  minValue: 0},
        legend: { position: 'top', maxLines: 7, alignment: 'center'},
        colors: colors,
        isStacked: true,
        seriesType: 'bars',
      };

      var series = {};
      for (var i = 0; i < columns.length; i++){
        if(i%2 == 0){
          series[i] = {type: 'line', targetAxisIndex:1};
        }
      }

      options.series = series;

      dataView.setColumns(visibleColumnIndexes);

      jQuery_money('#stats_revenue').height(400);
      chart.draw(dataView,options);

      // VISIBILITY START 2

      var visibilityVisibleColumnIndexes = [0];
      var colorsVisibility = [];

      columnsVisibility = jQuery_money.parseJSON(localStorage['columnsVisibility']);

      for (var i = 1; i < columnsVisibility.length; i++) {
        if (columnsVisibility[i].visible) {
          colorsVisibility.push(columnsVisibility[i].color);

          visibilityVisibleColumnIndexes.push(i);
        } else {
          colorsVisibility.push(columnsVisibility[i].disabledColor);

          visibilityVisibleColumnIndexes.push({
            calc: nullFunc,
            type: columnsVisibility[i].type,
            label: columnsVisibility[i].label,
          });
        }
      }

      var optionsVisibility = {
        title: '<?php /*echo __('Cliquez sur le format de votre choix pour voir les stats apparaître'); */?>',
        focusTarget: 'category',
        tooltip: {isHtml: true},
        //hAxis: {title: 'Date'},
        vAxis: {title: '% <?php /*echo __('Visibilité'); */?>'},
        legend: { position: 'top', maxLines: 7, alignment: 'center'},
        colors: colorsVisibility,
        curveType: 'function'
      };

      dataViewVisibility.setColumns(visibilityVisibleColumnIndexes);
      jQuery_money('#format_visibility_chart').height(400);
      chartVisibility.draw(dataViewVisibility, optionsVisibility);

      // VISIBILITY END 2
    }
  });
</script>
-->
