<?php

// CRUD 
   @$getId = base64_decode(anti_injection($_GET['id']));  
    if(@$getId){ 
        $query = mysql_query("SELECT * FROM customers WHERE id = + $getId");
        $result = mysql_fetch_array($query);
    }
    if ((isset($_GET["action"])) && ($_GET["action"] == "delete")) {
    $id = $_GET['id']; 
    $customer = $_GET['customer']; 
    $crud = new crud('chat'); // Table
    $crud->excluir("customer = $customer"); // ID
    
    header("Location: index.php?mod=Profile&id=$id");
    }
    
    $innv = mysql_query("SELECT * FROM invoices WHERE customer = '$getId'");
	$qtd_invoices = mysql_num_rows($innv);
    $inns = mysql_query("SELECT * FROM requests_tbl WHERE customer = '$getId'");
	$qtd_services = mysql_num_rows($inns);
	$innt = mysql_query("SELECT * FROM tickets WHERE customer = '$getId'");
	$qtd_tickets = mysql_num_rows($innt);
?>

<!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            <?php echo $lang['PROFILE_CUSTOMER']; ?>
          </h1>
         
        </section>

        <!-- Main content -->
        <section class="content">

          <div class="row">
            <div class="col-md-3">

              <!-- Profile Image -->
              <div class="box box-primary">
                <div class="box-body box-profile">
                  <img class="profile-user-img img-responsive img-circle" src="<?php echo gravatar($result['email']); ?>" alt="<?php echo $result['name']; ?>">
                  <h3 class="profile-username text-center"><?php echo $result['name']; ?></h3>
                  <p class="text-muted text-center">
				  
				  </p>

                  <ul class="list-group list-group-unbordered">
                    <li class="list-group-item">
                      <b><?php echo $lang['COMPANY']; ?></b> <a class="pull-right"><?php echo $result['company']; ?></a>
                    </li>
                    <li class="list-group-item">
                      <b>E-mail</b> <a class="pull-right"><?php echo $result['email']; ?></a>
                    </li>
                    <li class="list-group-item">
                      <b>Tax ID</b> <a class="pull-right"><?php echo $result['cpf_cnpj']; ?></a>
                    </li>
                    <li class="list-group-item">
                      <b><?php echo $lang['ADDRESS']; ?></b> <a class="pull-right"><?php echo $result['address']; ?></a>
                    </li>
                    <li class="list-group-item">
                      <b><?php echo $lang['CITY']; ?></b> <a class="pull-right"><?php echo $result['city']; ?></a>
                    </li>
                    <li class="list-group-item">
                      <b><?php echo $lang['STATE']; ?></b> <a class="pull-right"><?php echo $result['state']; ?></a>
                    </li>
                    <li class="list-group-item">
                      <b><?php echo $lang['ZIPCODE']; ?></b> <a class="pull-right"><?php echo $result['zipcode']; ?></a>
                    </li>
                    <li class="list-group-item">
                      <b><?php echo $lang['SERVICES']; ?></b> <a class="pull-right"><?php echo $qtd_services; ?></a>
                    </li>
                    <li class="list-group-item">
                      <b><?php echo $lang['INVOICES']; ?></b> <a class="pull-right"><?php echo $qtd_invoices; ?></a>
                    </li>
                    <li class="list-group-item">
                      <b><?php echo $lang['TICKETS']; ?></b> <a class="pull-right"><?php echo $qtd_tickets; ?></a>
                    </li>
                  </ul>
                  
                </div><!-- /.box-body -->
              </div><!-- /.box -->

              
            </div><!-- /.col -->
            
            
            <div class="col-md-9">
              <div class="nav-tabs-custom">
                <ul class="nav nav-tabs">
                  <li class="active"><a href="#activity" data-toggle="tab"><?php echo $lang['INVOICES']; ?></a></li>
                  <li><a href="#services" data-toggle="tab"><?php echo $lang['SERVICES']; ?></a></li>
                  <li><a href="#tickets" data-toggle="tab"><?php echo $lang['TICKETS']; ?></a></li>
                  <li><a href="#chat" data-toggle="tab">Chat</a></li>
                  <li><a href="#geo" data-toggle="tab">Geo</a></li>
                </ul>
                <div class="tab-content">
                  <div class="active tab-pane" id="activity">
                   
                 <table class="table table-striped table-hover">
                    <thead>
                      <tr>
                        <th><?php echo $lang['INVOICE']; ?></th>
                        <th><?php echo $lang['CUSTOMER']; ?></th>
                        <th><?php echo $lang['TOTAL']; ?></th>
                        <th><?php echo $lang['PAYMENT']; ?></th>
                        <th><?php echo $lang['DATE']; ?></th>
                        <th><?php echo $lang['PAYMENT_DATE']; ?></th>
                        <th><?php echo $lang['ACTIONS']; ?></th>
                      </tr>
                    </thead>
                    <tbody>
					<?php
 		  			$innv = mysql_query("SELECT * FROM invoices WHERE customer = '$getId'");
 		  			while($invoice = mysql_fetch_array($innv)){ 
						$querycs = mysql_query("SELECT * FROM customers WHERE id = '$getId'");
 		  			    $customer = mysql_fetch_array($querycs);
					?>
		            <tr>
					
		            <td>#<?php echo $invoice['invoice']; ?></td>
		            <td><?php echo $customer['name']; ?></td>
		            
		            <td><?php echo number_format($invoice['total'],2,'.',''); ?></td>
					<td><?php echo $invoice['payment']; ?></td>
					<td><?php echo data_en($invoice['date']); ?></td>
					<td><?php echo data_en($invoice['payment_date']); ?></td>
					
					<td>
						<a href="javascript:void(0);" onclick="javascript: if (confirm('<?php echo $lang['ALERT_BTN_PAID']; ?>')) { window.location.href='?mod=Invoices&id=<?php echo base64_encode($invoice['id']); ?>&request=<?php echo $invoice['request']; ?>&action=paid' } else { void('') };" class="btn btn-success btn-sm"><i class="fa fa-money"></i> Paid</a>
						<a href="javascript:void(0);" onclick="javascript: if (confirm('<?php echo $lang['ALERT_BTN_CANCEL']; ?>')) { window.location.href='?mod=Invoices&id=<?php echo base64_encode($invoice['id']); ?>&request=<?php echo $invoice['request']; ?>&action=cancel' } else { void('') };" class="btn btn-danger btn-sm"><i class="fa fa-remove"></i> Cancel</a>
					
					</td>
					
		            </tr>
					<?php } ?>
					
                    </tbody>
                    <tfoot>
                      <tr>
                        <th><?php echo $lang['INVOICE']; ?></th>
                        <th><?php echo $lang['CUSTOMER']; ?></th>
                        <th><?php echo $lang['TOTAL']; ?></th>
                        <th><?php echo $lang['PAYMENT']; ?></th>
                        <th><?php echo $lang['DATE']; ?></th>
                        <th><?php echo $lang['PAYMENT_DATE']; ?></th>
                        <th><?php echo $lang['ACTIONS']; ?></th>
                      </tr>
                    </tfoot>
                  </table>
                   
                   
                   
                  </div><!-- /.tab-pane -->
                  
                  
                  

                  <div class="tab-pane" id="services">
					  
                  <table class="table table-striped table-hover">
                    <thead>
                      <tr>
                        <th><?php echo $lang['PRODUCT']; ?></th>
                        <th><?php echo $lang['DOMAIN']; ?></th>
                        <th><?php echo $lang['QTD']; ?></th>
                        <th><?php echo $lang['AMOUNT']; ?></th>
                        <th><?php echo $lang['ORDER_TOTAL']; ?></th>
                        <th><?php echo $lang['ACTIONS']; ?></th>
                      </tr>
                    </thead>
                    <tbody>
					<?php
					$idRT = $result['id'];
 		  			$queryRF = mysql_query("SELECT p.*, c.product AS product FROM requests_tbl AS p INNER JOIN products AS c ON p.product = c.id WHERE p.customer = '$getId' ORDER BY p.id DESC");
 		  			while($qres = mysql_fetch_array($queryRF)){
					?>
		            <tr>
		            <td><?php echo $qres['product']; ?></td>
					<td><?php echo $qres['domain']; ?></td>
					<td><?php echo $qres['qtd']; ?></td>
					<td><?php echo number_format($qres['amount'],2,'.',''); ?></td>
					<td><?php echo number_format($qres['amount'] * $qres['qtd'],2,'.',''); ?></td>
				    <td><a class="btn btn-danger" href="javascript:void(0);" onclick="javascript: if (confirm('<?php echo $lang['ALERT_BTN_DELETE']; ?>')) { window.location.href='?mod=OrderEdit&id=<?php echo base64_encode($qres['id']); ?>&idRT=<?php echo $_GET['id']; ?>&action=delete' } else { void('') };"><i class="fa fa-remove"></i></a></td>
		            </tr>
					<?php } ?>
					
                    </tbody>
                    <tfoot>
                      <tr>
                        <th><?php echo $lang['PRODUCT']; ?></th>
                        <th><?php echo $lang['DOMAIN']; ?></th>
                        <th><?php echo $lang['QTD']; ?></th>
                        <th><?php echo $lang['AMOUNT']; ?></th>
                        <th><?php echo $lang['ORDER_TOTAL']; ?></th>
                        <th><?php echo $lang['ACTIONS']; ?></th>
                      </tr>
                    </tfoot>
                  </table>
                    
                  </div><!-- /.tab-pane -->
                  
                   <div class="tab-pane" id="tickets">
					   
					   
					   <table class="table table-hover table-striped">
					<thead>
                      <tr>
                        <th><?php echo $lang['TICKET_CUSTOMER']; ?></th>
                        <th><?php echo $lang['TICKET_SUBJECT']; ?></th>
                        <th><?php echo $lang['TICKET_DEPARTMENT']; ?></th>
                        <th><?php echo $lang['TICKET_DATE']; ?></th>
                        <th><?php echo $lang['TICKET_STATUS']; ?></th>
                        <th><?php echo $lang['ACTIONS']; ?></th>
                      </tr>
                    </thead>	
						
                      <tbody>
						  
					   <?php
				        $pagina = (isset($_GET['pg']))? $_GET['pg'] : 1;
						$cmd = "SELECT * FROM tickets WHERE status = '1' AND customer = '$getId' order by id DESC";
						$tickets = mysql_query($cmd);
						$total = mysql_num_rows($tickets);
						$registros = 20;
						$numPaginas = ceil($total/$registros);
						$inicio = ($registros*$pagina)-$registros;
						$tk = mysql_query("SELECT p.*, c.name AS name, c.email AS email, d.name_department AS name_department FROM tickets AS p INNER JOIN customers AS c ON p.customer = c.id INNER JOIN departments AS d ON p.department = d.id WHERE p.status = '1' AND p.customer = '$getId' order by p.id DESC limit $inicio,$registros");
						$totalregistros = mysql_num_rows($tk);
						while ($ticket = mysql_fetch_array($tk)) {
							
						?>  
                        <tr class="<?php if($ticket['priority'] == 'A') { ?>danger<?php } ?><?php if($ticket['priority'] == 'M') { ?>warning<?php } ?><?php if($ticket['priority'] == 'B') { ?>default<?php } ?>">
						  <td class="mailbox-name"><?php echo $ticket['name']; ?><br><small><?php echo $ticket['email']; ?></small></td>
                          <td class="mailbox-subject"><?php echo limitText($ticket['subject'],40); ?></td>
                          <td class="mailbox-department"><b><?php echo $ticket['name_department']; ?></b></td>
                          <td class="mailbox-date"><?php if($lang['DATE_FORMAT'] == 'PT') { ?><?php echo data_br($ticket['date']); ?><?php } ?><?php if($lang['DATE_FORMAT'] == 'EN') { ?><?php echo data_en($ticket['date']); ?><?php } ?><br><?php echo $ticket['hour']; ?></td>
						  <td class="mailbox-success">
							  <?php if($ticket['status'] == '1') { ?><span class="label label-success"><?php echo $lang['TICKET_OPEN']; ?></span><?php } ?>
							  <?php if($ticket['status'] == '2') { ?><span class="label label-info"><?php echo $lang['TICKET_ANSWERED']; ?></span><?php } ?>
							  <?php if($ticket['status'] == '3') { ?><span class="label label-warning"><?php echo $lang['TICKET_PROCESS']; ?></span><?php } ?>
							  <?php if($ticket['status'] == '4') { ?><span class="label label-danger"><?php echo $lang['TICKET_CLOSED']; ?></span><?php } ?>
							  </td>
						  <td class="mailbox-actions">
							  <a class="btn btn-primary btn-sm" href="?mod=Ticket&id=<?php echo base64_encode($ticket['id']); ?>"><?php echo $lang['TICKET_ANSWER']; ?></a>
							  <a class="btn btn-danger btn-sm" href="javascript:void(0);" onclick="javascript: if (confirm('<?php echo $lang['ALERT_BTN_DELETE']; ?>')) { window.location.href='?mod=Tickets&id=<?php echo base64_encode($ticket['id']); ?>&action=delete' } else { void('') };"><i class="fa fa-remove"></i></a>
							  </td>
                        </tr> 
                        <?php } ?>  
                      </tbody>
                    </table><!-- /.table -->
					   
					   <div class="mailbox-controls">
                    
                    <button class="btn btn-default btn-sm" onClick="window.location.reload()"><i class="fa fa-refresh"></i></button>
                    <div class="pull-right">
                     <?php echo $total; ?> <?php echo $lang['COUNT_REGISTER']; ?> / 
                      <div class="btn-group">
				      <?php
				      for($i = 1; $i < $numPaginas + 1; $i++) {
					  ?>
					  <a class="btn btn-default btn-sm <?php if($_GET['pg'] == $i) { echo 'active'; } ?>" href="?mod=Tickets&pg=<?php echo $i; ?>"><?php echo $i; ?></a>
                      <?php } ?> 
                      
                      </div><!-- /.btn-group -->
                    </div><!-- /.pull-right -->
                  </div>
					   
					   </div>
                  
                  <div class="tab-pane" id="chat">
                  
                  <!-- Chat box -->
              <div class="box box-success">
                <div class="box-header">
                  <i class="fa fa-comments-o"></i>
                  <h3 class="box-title">Chat</h3>
                  <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=Profile&id=<?php echo $_GET['id']; ?>&customer=<?php echo $result['id']; ?>&action=delete' } else { void('') };" class="btn btn-danger btn-sm">Close</a>
                      <hr>
                    </div>
                  </div>
                </div>
             
                <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>
              </div><!-- /.box (chat box) -->
                  
                  </div>
                  
                  
                  <div class="tab-pane" id="geo">
                  
                  <!-- Chat box -->
              <div class="box box-success">
                <div class="box-header">
                  <i class="fa fa-globe"></i>
                  <h3 class="box-title">Geo</h3>
                </div>
        <table width="100%">
       <tr>
       <td>
       <?php
    include("class.ipdetails.php");
    
    function address($lat,$lng) {
    $address = "$lat,$lng";
    $geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false');
    $output= json_decode($geocode);
    $endereco = $output->results[0]->formatted_address;
    return $endereco;
    }
    
    $ip = "177.70.142.40";
    $ipdetails = new ipdetails($ip); 
    $ipdetails->scan();
    echo "<b>IP:</b>        ".$ip                        ."<br />"; 
    echo "<b>Country:</b>      ".$ipdetails->get_country()  ."<br />";
    echo "<b>Address:</b>    ". utf8_decode(address($ipdetails->get_latitude(),$ipdetails->get_longitude()))   ."<br />";
    echo "<b>Latitude:</b>  ".$ipdetails->get_latitude() ."<br />";
    echo "<b>Longitude:</b> ".$ipdetails->get_longitude()."<br />";
    echo "<b>Code Country:</b> ".$ipdetails->get_countrycode()."<br />";
    echo "<b>Code Continent:</b> ".$ipdetails->get_continentcode()."<br />";
    echo "<b>Code Currency:</b> ".$ipdetails->get_currencycode()."<br />";
    echo "<b>Currency:</b> ".htmlspecialchars_decode($ipdetails->get_currencysymbol())."<br />";
    echo "<b>Quote currency (USD):</b> ".$ipdetails->get_currencyconverter()."<br />";    
?>
       
       </td>
       <td>
       <img src="https://maps.googleapis.com/maps/api/staticmap?center=<?php echo $ipdetails->get_latitude(); ?>,<?php echo $ipdetails->get_longitude(); ?>&zoom=12&size=400x300">

       </td>
       
       </tr> 
        </table>     
    <br><br>
              </div><!-- /.box (chat box) -->
                  
                  </div>
                  
                  
                  
                </div><!-- /.tab-content -->
              </div><!-- /.nav-tabs-custom -->
            </div><!-- /.col -->
          </div><!-- /.row -->

        </section><!-- /.content -->
      </div><!-- /.content-wrapper -->
      
      <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 $result['id']; ?>",
				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 $result['id']; ?>");
   		      var refreshId = setInterval(function() {
      		      $("#chat_msg").load("ajax_chat.php?customer=<?php echo $result['id']; ?>");
   		      }, 1000);
   		      $.ajaxSetup({ cache: false });
   		      
   		      $("#chat_nm").load("ajax_chat.php?cc=y&customer=<?php echo $result['id']; ?>");
   		      var refreshId = setInterval(function() {
      		      $("#chat_nm").load("ajax_chat.php?cc=y&customer=<?php echo $result['id']; ?>");
   		      }, 1000);
   		      $.ajaxSetup({ cache: false });
   		      
   		      });
   		  
   		  
   		  
   		  
</script>
<script type="text/javascript"  src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
	var IPLatlng = new google.maps.LatLng(-21.6969, -45.2533);
	var myOptions = {
  	  zoom: 10,
			maxZoom: 15,
			streetViewControl: false,			
    	center: IPLatlng,
	    mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map"), myOptions);
	var marker = new google.maps.Marker({
  	  position: IPLatlng,
    	map: map,
	    title:"Tres Coracoes"
	});
</script>
