<link rel="stylesheet" href="<?= base_url('bower_components/bootstrap-datepicker/dist/css/bootstrap-datepicker.min.css') ?>">
<link rel="stylesheet" href="<?= base_url('bower_components/bootstrap-daterangepicker/daterangepicker.css') ?>">
<script src="<?= base_url('bower_components/chart.js/Chart.js') ?>"></script>

<?php
//include 'ChromePhp.php';

    $month = time();
    $months[]=convertDate(date("M"));
    $label_months [] = date("M")." ".date("Y");
    $years[]= date("Y");
    for ($i = 1; $i <= 11; $i++) {
        $month = strtotime('last month', $month);
        $months [] = convertDate(date("M", $month));
        $years[] = date("Y", $month);
        $label_months [] = date("M", $month)." ".date("Y", $month);
    }
    /**
    ** Simple User Treatment
    **/

    $date = date("Y");

    $users_simple_data[] = '';
    $users_simple_data =array();
    for ($i = 1; $i <= 12; $i++) {
        $users_simple_query_mounth = $this->db->query("SELECT user_id FROM user_infos
                      WHERE country_id='".$this->session->userdata('current_pays')."' AND MONTH(date_created) = '".$months[$i-1]."' AND YEAR(date_created) = '".$years[$i-1]."' AND network_id IN (SELECT network_id
                                                          FROM hyper_infos
                                                                WHERE country_id ='".$this->session->userdata('current_pays')."'
                                                                AND code_membre ='".$this->session->userdata('current_hyper')."'
                                                          GROUP BY network_id)");
        $users_simple_data[] = $users_simple_query_mounth->num_rows();
    }


    $users_simple_query = $list_simple_users;

    if($users_simple_query!=false){
        $users_simple=$users_simple_query->num_rows();
       // Count networks for simple users
       $array_simple = array();
       $num = 0;
       if ($users_simple > 0) {
           foreach($users_simple_query->result() as $row) {
               $num++;
               $array_simple[] = $row->network;
           }

           $vals_simple = array_count_values($array_simple);
           //echo 'No. of NON Duplicate Items: '.count($vals_simple).'<br><br>';
           //print_r($vals_simple);
           $pieChart2 = array();
           foreach(array_keys($vals_simple) as $paramName2) {
             $color2 = dechex(rand(0x000000, 0xFFFFFF));
             $trash2 = array("value" => $vals_simple[$paramName2],
                 "color"    => "#".$color2,
                 "highlight" => "#".$color2,
                 "label" => $paramName2);

                 $pieChart2[]= $trash2;

           }
       }
    }else{
        $pieChart2 = array();
       }

   /**
   ** Geolocated User Treatment
   **/
    $users_geolocated_query = $list_geolocated_users;

    // Geolocated Users by month replace 2016 by NOW()
    $users_geolocated_data[] = '';
    $users_geolocated_data =array();
    for ($i = 1; $i <= 12; $i++) {
        $users_geolocated_query_january = $this->db->query("SELECT agent_id FROM super_infos
                      WHERE category='geolocated' AND MONTH(date_created) = '".$months[$i-1]."' AND YEAR(date_created) = ".$years[$i-1]." 
                      AND code_parrain IN (SELECT code_membre FROM super_infos WHERE category='super' AND code_parrain='".$this->session->userdata('current_hyper')."')");
        $users_geolocated_data[] = $users_geolocated_query_january->num_rows();
    }

    if($users_geolocated_query!=false){
        $users_geolocated=$users_geolocated_query->num_rows();
        //$users_geolocated_query =  json_encode($users_geolocated_query->result());
        // Counts network for geolocated users
        $array_geolocated = array();
        $num = 0;
        if ($users_geolocated > 0) {
            foreach($users_geolocated_query->result() as $row) {
                $num++;
                $array_geolocated[] = $row->network;
            }
            $vals_geolocated = array_count_values($array_geolocated);
            //echo 'No. of NON Duplicate Items: '.count($vals_geolocated).'<br><br>';
            //print_r($vals_geolocated);
            $pieChart = array();
            foreach(array_keys($vals_geolocated) as $paramName) {
              $color = dechex(rand(0x000000, 0xFFFFFF));
              $trash = array("value" => $vals_geolocated[$paramName],
                  "color"    => "#".$color,
                  "highlight" => "#".$color,
                  "label" => $paramName);

                  $pieChart[]= $trash;
            }
        }
    }else{
        $pieChart = array();
    }
?>

<div class="content-wrapper">

  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
        <?= $this->lang->line('administrateur'); ?>
        <small><?= $this->lang->line('Tableau de bord'); ?></small>
    </h1>
  </section>

  <section class="content">
    <!-- Small boxes (Stat box) -->
    <div class="row">
      <div class="col-lg-3 col-xs-6">
        <!-- small box -->

        <div class="small-box bg-aqua">
          <div class="inner">
            <h3><?= $c_users;?></h3>

            <p><?= $this->lang->line('Utilisateurs simples'); ?></p>
          </div>
          <div class="icon">
            <i class="ion ion-person-add"></i>
          </div>

        </div>
      </div>
      <!-- ./col -->
      <div class="col-lg-3 col-xs-6">
        <!-- small box -->
        <div class="small-box bg-yellow">
          <div class="inner">
            <h3><?= $c_geolocated;?></h3>

            <p><?= $this->lang->line('Utilisateurs géolocalisés'); ?></p>
          </div>
          <div class="icon">
            <i class="ion ion-person-add"></i>
          </div>

        </div>
      </div>
      <!-- ./col -->
      <div class="col-lg-3 col-xs-6">
        <!-- small box -->
        <div class="small-box bg-green">
          <div class="inner">
            <h3><?= $c_code;?></h3>

            <p><?= $this->lang->line('Codes validés'); ?></p>
          </div>
          <div class="icon">
            <i class="ion ion-stats-bars"></i>
          </div>

        </div>
      </div>
      <!-- ./col -->
      <div class="col-lg-3 col-xs-6">
        <!-- small box -->
        <div class="small-box bg-red">
          <div class="inner">
            <h3><?= $c_generated; ?></h3>

            <p><?= $this->lang->line('Codes générés'); ?></p>
          </div>
          <div class="icon">
            <i class="ion ion-pie-graph"></i>
          </div>

        </div>
      </div>
      <!-- ./col -->

      <div class="col-xs-12">
        <!-- BAR CHART -->
        <div class="box box-success">
          <div class="box-header with-border">
            <h3 class="box-title"><?= $this->lang->line('Souscription des 12 derniers mois'); ?></h3>
          </div>
          <div class="box-body">
            <div class="chart" id="chart">
              <canvas id="barChart" style="height:230px"></canvas>
            </div>
          </div>
          <!-- /.box-body -->
        </div>
        <!-- /.box -->
      </div>
      <!--
      <div class="col-xs-6">
        <div class="box box-danger">
          <div class="box-header with-border">
            <h3 class="box-title"><?= $this->lang->line('Utilisateurs simple par réseaux'); ?></h3>
            <div class="box-tools pull-right">
            </div>
          </div>
          <div class="box-body" id="chart2">
            <canvas id="pieChart2" style="height:250px"></canvas>
          </div>
        </div>
      </div>
      <div class="col-xs-6">
        <div class="box box-danger">
          <div class="box-header with-border">
            <h3 class="box-title"><?= $this->lang->line('Utilisateurs géolocalisés par réseaux'); ?></h3>

            <div class="box-tools pull-right">
            </div>
          </div>
          <div class="box-body" id="chart1">
            <canvas id="pieChart" style="height:250px"></canvas>
          </div>
        </div>
      </div>
      -->
    </div>
    <!-- /.row -->
  </section>

</div>


<!-- jQuery 2.2.4 -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

<!-- jQuery UI 1.11.4 -->
<script src="<?= base_url('bower_components/jquery-ui/jquery-ui.min.js') ?>"></script>
<!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->
<script>
  $.widget.bridge('uibutton', $.ui.button);
</script>
<!-- Bootstrap 3.3.7 -->
<script src="<?= base_url('bower_components/bootstrap/dist/js/bootstrap.min.js') ?>"></script>
<!-- Morris.js charts -->
<script src="<?= base_url('bower_components/raphael/raphael.min.js') ?>"></script>
<!-- Sparkline -->
<script src="<?= base_url('bower_components/jquery-sparkline/dist/jquery.sparkline.min.js') ?>"></script>
<!-- jvectormap -->
<script src="<?= base_url('plugins/jvectormap/jquery-jvectormap-1.2.2.min.js') ?>"></script>
<script src="<?= base_url('plugins/jvectormap/jquery-jvectormap-world-mill-en.js') ?>"></script>
<!-- jQuery Knob Chart -->
<script src="<?= base_url('bower_components/jquery-knob/dist/jquery.knob.min.js') ?>"></script>
<!-- daterangepicker -->
<script src="<?= base_url('bower_components/moment/min/moment.min.js') ?>"></script>
<script src="<?= base_url('bower_components/bootstrap-daterangepicker/daterangepicker.js') ?>"></script>
<!-- datepicker -->
<script src="<?= base_url('bower_components/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js') ?>"></script>
<!-- Bootstrap WYSIHTML5 -->
<script src="<?= base_url('plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js') ?>"></script>
<!-- Slimscroll -->
<script src="<?= base_url('bower_components/jquery-slimscroll/jquery.slimscroll.min.js') ?>"></script>
<!-- FastClick -->
<script src="<?= base_url('bower_components/fastclick/lib/fastclick.js') ?>"></script>
<!-- AdminLTE App -->
<script src="<?= base_url('dist/js/adminlte.min.js') ?>"></script>
<!-- AdminLTE for demo purposes -->
<script src="<?= base_url('dist/js/demo.js') ?>"></script>

<!-- ChartJS -->
<script src="<?= base_url('bower_components/chart.js/Chart.js') ?>"></script>



<script type='text/javascript'>
    $(document).ready(function(){
        $('#gestion_btn').click(function(){
            window.location.href = "<?= base_url('Gestion') ?>";
        });

        $('#pays').change(function(){
            var pays = $(this).val();
            $.ajax({
                url:'<?= base_url('Admin/getHypByPays')?>',
                type: 'post',
                data: {pays: pays},
                dataType: 'json',
                success: function(data){
                    var len = data.length;


                    if(len > 0){

                        var select = document.getElementById('hyper');
                        if(select===null){
                            var grpPays = document.getElementById('grp-pays');
                            $(grpPays).empty();
                            $(grpPays).append("<select class='form-control input-lg' name='hyper' required id='hyper'></select>");
                            select = document.getElementById('hyper');
                        }
                        $(select).empty();

                        for (var i in data) {
                            $(select).append('<option value=' + data[i].code_membre + '>' + data[i].lastname +'-' + data[i].network + ' ('+ data[i].code_membre +')</option>');
                        }

                        var code = data[0].code_membre;

                        $.ajax({
                            url:'<?= base_url('Admin/getVilleByHyp')?>',
                            type: 'post',
                            data: {code: code,pays:pays},
                            dataType: 'json',
                            success: function(list){
                                var len = list.length;

                                if(len > 0){

                                    var select = document.getElementById('ville');
                                    if(select===null){
                                        var grpVille = document.getElementById('grp-ville');
                                        $(grpVille).empty();
                                        $(grpVille).append("<select class='form-control input-lg' name='ville' required id='ville'></select>");
                                        select = document.getElementById('ville');
                                    }
                                    $(select).empty();

                                    for (var i in list) {
                                        $(select).append('<option value=' + list[i].town_id + '>' + list[i].town +'</option>');
                                    }


                                }else{

                                    var select = document.getElementById('ville');
                                    $(select).empty();

                                }

                            }
                        });


                    }else{

                        var select = document.getElementById('hyper');
                        $(select).empty();

                        var select2 = document.getElementById('ville');
                        $(select2).empty();
                    }

                }
            });
        });

        $('#hyper').change(function(){
            var code = $(this).val();
            $.ajax({
                url:'<?= base_url('Admin/getVilleByHyp')?>',
                type: 'post',
                data: {code: code},
                dataType: 'json',
                success: function(list){
                    var len = list.length;

                    if(len > 0){

                        var select = document.getElementById('ville');
                        $(select).empty();

                        for (var i in list) {
                            $(select).append('<option value=' + list[i].town_id + '>' + list[i].town +'</option>');
                        }

                    }else{

                        var select = document.getElementById('ville');
                        $(select).empty();

                    }

                }
            });
        });
    });
</script>

<script >
/*

var cities = [];
var cities_simple = [];

//console.log(geocodeposition(52.5487429714954,-1.81602098644987));


for(var i = 0 ; i < 1 ; i++ ) {
  var cit;
  var latit = Number(users[i]['latitude']);
  var longi = Number(users[i]['longitude']);


  //cit = geocodeposition(latit, longi);
  cities[i] = {};
  cities[i] = cit;
}
*/
//console.log(cities);

/*var Pie = '<?= json_encode($pieChart); ?>';

if(Pie==='[]'){
    var select = document.getElementById('chart1');
    $(select.children).hide();
    $(select).append("<p>Aucun utilisateur géolocalisé dans tous les réseaux</p>");
}else{
    Pie = JSON.parse(Pie);
}

var Pie2 = '<?= json_encode($pieChart2); ?>';
if(Pie2==='[]') {
    var select = document.getElementById('chart2');
    $(select.children).hide();
    $(select).append("<p>Aucun utilisateur simple dans tous les réseaux</p>");
}else{
    Pie2 = JSON.parse(Pie2);
}*/


var areaChartData = {
  labels  : <?= json_encode($label_months) ?>,
  datasets: [
    {
      label               : 'Electronics',
      fillColor           : 'rgba(255, 162, 0, 1)',
      strokeColor         : 'rgba(255, 162, 0, 1)',
      pointColor          : 'rgba(255, 162, 0, 1)',
      pointStrokeColor    : '#ffa200',
      pointHighlightFill  : '#fff',
      pointHighlightStroke: 'rgba(220,220,220,1)',
      data: <?= json_encode($users_geolocated_data) ?>
    },
    {
      label               : 'Digital Goods',
      fillColor           : 'rgba(0, 187, 255, 1)',
      strokeColor         : 'rgba(0, 187, 255, 1)',
      pointColor          : '#00bbff',
      pointStrokeColor    : 'rgba(0, 187, 255, 1)',
      pointHighlightFill  : '#fff',
      pointHighlightStroke: 'rgba(0, 187, 255, 1)',
      data                : <?= json_encode($users_simple_data) ?>
    }
  ]
}

</script>
<!-- Page script -->
<script src="<?= base_url('dist/js/custom.js') ?>"></script>
