<?php

 if ((isset($_GET["action"])) && ($_GET["action"] == "delete")) {
    $id = $_GET['id']; 
    $crud = new crud('chat'); // Table
    $crud->excluir("customer = $id"); // ID
    
    header("Location: index.php?mod=Dashboard&y=3");
    }
	
	$cm = mysql_query("SELECT * FROM invoices WHERE status = 'open'");
    $qtd_open_invoices = mysql_num_rows($cm);
	$cmm = mysql_query("SELECT * FROM customers");
    $qtd_customers = mysql_num_rows($cmm);
	$q = mysql_query("SELECT SUM(total) as total FROM invoices WHERE status = 'confirmed'");
 	$r = mysql_fetch_array($q);
	$total_invoice = number_format($r["total"],2,'.','');
	$qs = mysql_query("SELECT SUM(total) as total FROM invoices WHERE status = 'open'");
 	$rs = mysql_fetch_array($qs);
	$total_invoice_open = number_format($rs["total"],2,'.','');
	
	if(isset ($_POST['report'])){
	$dy = $_POST['dys'];
	} else { 
	$dy = date('Y');
	}

	$report1 = mysql_query("SELECT SUM(total) as total FROM invoices WHERE mes = '01' AND status = 'confirmed' AND ano = '$dy'");
	$report1s = mysql_fetch_array($report1);
	$january = number_format($report1s["total"],2,'.','');

	$report2 = mysql_query("SELECT SUM(total) as total FROM invoices WHERE mes = '02' AND status = 'confirmed' AND ano = '$dy'");
	$report2s = mysql_fetch_array($report2);
	$february = number_format($report2s["total"],2,'.','');
		
	$report3 = mysql_query("SELECT SUM(total) as total FROM invoices WHERE mes = '03' AND status = 'confirmed' AND ano = '$dy'");
	$report3s = mysql_fetch_array($report3);
	$march = number_format($report3s["total"],2,'.','');	

	$report4 = mysql_query("SELECT SUM(total) as total FROM invoices WHERE mes = '04' AND status = 'confirmed' AND ano = '$dy'");
	$report4s = mysql_fetch_array($report4);
	$april = number_format($report4s["total"],2,'.','');	

	$report5 = mysql_query("SELECT SUM(total) as total FROM invoices WHERE mes = '05' AND status = 'confirmed' AND ano = '$dy'");
	$report5s = mysql_fetch_array($report5);
	$may = number_format($report5s["total"],2,'.','');	

	$report6 = mysql_query("SELECT SUM(total) as total FROM invoices WHERE mes = '06' AND status = 'confirmed' AND ano = '$dy'");
	$report6s = mysql_fetch_array($report6);
	$june = number_format($report6s["total"],2,'.','');	

	$report7 = mysql_query("SELECT SUM(total) as total FROM invoices WHERE mes = '07' AND status = 'confirmed' AND ano = '$dy'");
	$report7s = mysql_fetch_array($report7);
	$july = number_format($report7s["total"],2,'.','');	

	$report8 = mysql_query("SELECT SUM(total) as total FROM invoices WHERE mes = '08' AND status = 'confirmed' AND ano = '$dy'");
	$report8s = mysql_fetch_array($report8);
	$august = number_format($report8s["total"],2,'.','');	

	$report9 = mysql_query("SELECT SUM(total) as total FROM invoices WHERE mes = '09' AND status = 'confirmed' AND ano = '$dy'");
	$report9s = mysql_fetch_array($report9);
	$september = number_format($report9s["total"],2,'.','');

	$report10 = mysql_query("SELECT SUM(total) as total FROM invoices WHERE mes = '10' AND status = 'confirmed' AND ano = '$dy'");
	$report10s = mysql_fetch_array($report10);
	$october = number_format($report10s["total"],2,'.','');

	$report11 = mysql_query("SELECT SUM(total) as total FROM invoices WHERE mes = '11' AND status = 'confirmed' AND ano = '$dy'");
	$report11s = mysql_fetch_array($report11);
	$november = number_format($report11s["total"],2,'.','');

	$report12 = mysql_query("SELECT SUM(total) as total FROM invoices WHERE mes = '12' AND status = 'confirmed' AND ano = '$dy'");
	$report12s = mysql_fetch_array($report12);
	$december = number_format($report12s["total"],2,'.','');

	$report = mysql_query("SELECT SUM(total) as total FROM invoices WHERE status = 'confirmed' AND ano = '$dy'");
	$reports = mysql_fetch_array($report);
	$totalyear = number_format($reports["total"],2,'.','');
?>

<!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            Dashboard
            <small>WHMX Control Panel</small>
          </h1>
        </section>
		
		 <!-- Main content -->
        <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><?php echo $qtd_open_invoices; ?></h3>
                  <p><?php echo $lang['DASHBOARD_NEWINVOICES']; ?></p>
                </div>
                <div class="icon">
                  <i class="ion ion-bag"></i>
                </div>
                <a href="?mod=Invoices" class="small-box-footer"><?php echo $lang['DASHBOARD_BTNMORE']; ?> <i class="fa fa-arrow-circle-right"></i></a>
              </div>
            </div><!-- ./col -->
            <div class="col-lg-3 col-xs-6">
              <!-- small box -->
              <div class="small-box bg-green">
                <div class="inner">
                  <h3>$ <?php echo $total_invoice; ?><sup style="font-size: 20px"><?php echo $config['moeda']; ?></sup></h3>
                  <p><?php echo $lang['DASHBOARD_INVOICESCONFIRMED']; ?></p>
                </div>
                <div class="icon">
                  <i class="ion ion-pie-graph"></i>
                </div>
                <a href="?mod=InvoicesPaid" class="small-box-footer"><?php echo $lang['DASHBOARD_BTNMORE']; ?> <i class="fa fa-arrow-circle-right"></i></a>
              </div>
            </div><!-- ./col -->
            <div class="col-lg-3 col-xs-6">
              <!-- small box -->
              <div class="small-box bg-yellow">
                <div class="inner">
                  <h3><?php echo $qtd_customers; ?></h3>
                  <p><?php echo $lang['DASHBOARD_CUSTOMERS']; ?></p>
                </div>
                <div class="icon">
                  <i class="ion ion-person-add"></i>
                </div>
                <a href="?mod=Customers" class="small-box-footer"><?php echo $lang['DASHBOARD_BTNMORE']; ?> <i class="fa fa-arrow-circle-right"></i></a>
              </div>
            </div><!-- ./col -->
            <div class="col-lg-3 col-xs-6">
              <!-- small box -->
              <div class="small-box bg-red">
                <div class="inner">
                  <h3>$ <?php echo $total_invoice_open; ?><sup style="font-size: 20px"><?php echo $config['moeda']; ?></sup></h3>
                  <p><?php echo $lang['DASHBOARD_INVOICE_CUSTOMERS']; ?></p>
                </div>
                <div class="icon">
                  <i class="ion ion-stats-bars"></i>
                </div>
                <a href="?mod=Invoices" class="small-box-footer"><?php echo $lang['DASHBOARD_BTNMORE']; ?> <i class="fa fa-arrow-circle-right"></i></a>
              </div>
            </div><!-- ./col -->
          </div><!-- /.row -->
          <!-- Main row -->
          
          
            <div class="row">
           <div class="col-md-6">
                  <!-- USERS LIST -->
                  <div class="box box-danger">
                    <div class="box-header with-border">
                      <h3 class="box-title"><?php echo $lang['DASHBOARD_LATEST_CUSTOMER']; ?></h3>
                      <div class="box-tools pull-right">
                      </div>
                    </div><!-- /.box-header -->
                    
                    <div class="box-body no-padding">
                      <ul class="users-list clearfix">
					<?php
 		  			$query = mysql_query("SELECT * FROM customers order by id DESC LIMIT 12");
 		  			while($result = mysql_fetch_array($query)){ 
					?>
                        <li>
                          <img src="<?php echo gravatar($result['email']); ?>" style="width:80px;height:80px;" alt="<?php echo $result['name']; ?>">
                          <a class="users-list-name" href="?mod=Profile&id=<?php echo base64_encode($result['id']); ?>"><?php echo $result['name']; ?></a>
                          <span class="users-list-date"><?php echo $result['company']; ?></span>
                        </li>
                        <?php } ?>
                      </ul><!-- /.users-list -->
                    </div><!-- /.box-body -->
                    <div class="box-footer text-center">
                      <a href="?mod=Customers" class="uppercase"><?php echo $lang['DASHBOARD_VIEW_ALL']; ?></a>
                    </div><!-- /.box-footer -->
                  </div><!--/.box -->
                </div><!-- /.col -->
                
                
                 <div class="col-md-6">
                 <!-- Chat box -->
              <div class="box box-success">
                <div class="box-header">
                  <i class="fa fa-comments-o"></i>
                  <h3 class="box-title">Chat</h3><?php if($_GET['customer'] <> '') { ?>
                  <div class="box-tools pull-right" data-toggle="tooltip" title="Close Chat Customer">
                    <div class="btn-group" data-toggle="btn-toggle" >
                     <a href="javascript:void(0);" onclick="javascript: if (confirm('<?php echo $lang['ALERT_BTN_DELETE']; ?>')) { window.location.href='?mod=Dashboard&id=<?php echo $_GET['customer']; ?>&action=delete' } else { void('') };" class="btn btn-danger btn-sm">Close</a>
                      <hr>
                    </div>
                  </div><?php } ?>
                </div>
                <?php if($_GET['customer'] <> '') { ?>
                <div class="box-body chat" id="chat-box">
                 
                <div id="chat_msg"></div>                 
                 
                </div><!-- /.chat -->
                
              <form id="ajax_chat" name="ajax_chat" method="POST">
                <div class="box-footer">
				
                  <div class="input-group">
                    <input class="form-control" name="msg" placeholder="Type message... ">
                    <div class="input-group-btn">
                      <input type="submit" value="Send" class="btn btn-warning btn-sm" id="btn-chat">
                    </div>
                  </div>
                </div>
                </form><? } else { ?>
					<div class="box-body chat" id="chat-box">
						<div class="alert alert-danger"><?php echo $lang['DASHBOARD_NO_CHAT']; ?></div>
					</div>
					<?php } ?>
              </div><!-- /.box (chat box) -->
              </div>
              
              
              
              
              <div class="col-md-12">
              <div class="box box-info">
                    <div class="box-header with-border">
                      <h3 class="box-title">Grapy Invoice</h3>
                      <div class="box-tools pull-right">
                      </div>
                    </div><!-- /.box-header -->
                    
                    <div class="box-body">
              
                  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Month', 'Total'],
          ['Jan',  <?php echo $january; ?>],
          ['Feb',  <?php echo $february; ?>],
          ['Mar',  <?php echo $march; ?>],
          ['Apr',  <?php echo $april; ?>],
          ['May',  <?php echo $may; ?>],
          ['Jun',  <?php echo $june; ?>],
          ['Jul',  <?php echo $july; ?>],
          ['Ago',  <?php echo $august; ?>],
          ['Sep',  <?php echo $september; ?>],
          ['Oct',  <?php echo $october; ?>],
          ['Nov',  <?php echo $november; ?>],
          ['Dec',  <?php echo $december; ?>]
        ]);

        var options = {
          title: 'Invoice Performance',
          curveType: 'function',
          legend: { position: 'bottom' }
        };

        var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

        chart.draw(data, options);
      }
    </script>
              <div id="curve_chart" style="width: 100%; height: 500px"></div>
              
              
                </div> </div>
              </div>
                
                
                
          </div>
		
		  </section>

</div>
<script>
jQuery(document).ready(function(){
		jQuery('#ajax_chat').submit(function(){
			var dados = jQuery( this ).serialize();
			var elem = document.getElementById('chat-box'); 
			jQuery.ajax({
				type: "POST",
				url: "ajax_chat_post.php?customer=<?php echo $_GET['customer']; ?>",
				data: dados,
				success: function( data )
				{
	
					 document.ajax_chat.reset();
					 elem.scrollTop = elem.scrollHeight;
				}
			});
			
			return false;
		});
	});
	$(document).ready(function() {
 	 	      $("#chat_msg").load("ajax_chat.php?customer=<?php echo $_GET['customer']; ?>");
   		      var refreshId = setInterval(function() {
      		      $("#chat_msg").load("ajax_chat.php?customer=<?php echo $_GET['customer']; ?>");
   		      }, 1000);
   		      $.ajaxSetup({ cache: false });
   		      
   		      $("#chat_nm").load("ajax_chat.php?cc=y&customer=<?php echo $_GET['customer']; ?>");
   		      var refreshId = setInterval(function() {
      		      $("#chat_nm").load("ajax_chat.php?cc=y&customer=<?php echo $_GET['customer']; ?>");
   		      }, 1000);
   		      $.ajaxSetup({ cache: false });
   		      
   		      });
</script>
