<!DOCTYPE html>
<?php
require_once('../Db/dbconfig.php');
@$name=$_GET['username'];
?>
<html>
<head>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.4/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="https://cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
<script src="https://kit.fontawesome.com/c17eae44a3.js" crossorigin="anonymous"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script> 

</head>
<body>
<div class="container-fluid page-body-wrapper">
	<div class="page-header">
      <h3 class="page-title"  >
        <span class="page-title-icon bg-gradient-primary text-white mr-2">
          <i class="fas fa-home fa-xs" style="color: black"></i>
        </span><b>Dashboard</b></h3>
	</div>
  <div class=" card-header d-flex flex-row-reverse">
    <a href="../controller/backup_controller.php"><button type="button" class="btn btn-dark"><i class="ace-icon fa fa-arrow-alt-circle-down bigger-130"></i></button></a>
        <label class="p-2" style="width:170px;" for="dat"><b>Download Backup :</b></label>
  </div>
	<br>
    <div class="main-panel">
        <div class="content-wrapper">
        	<div class="row">
              <div class="col-md-4 stretch-card grid-margin">
              	<div class="card bg-dark text-white" style="height: 210px;background: linear-gradient(to left, #ff6600 0%, #ffcc99 100%);">
				  <img src="../assets/images/dashboard/circle.svg" class="card-img"  alt="circle-image" >
				  <div class="card-img-overlay">
				    <h5 class="card-title">Purchases</h5>
				     <h2 class="mb-5">
                    	<?php 
							$sql = "SELECT count(*) as counter FROM purchase";
							$result = $con->query($sql);

							if ($result->num_rows > 0) 
							{
							    // output data of each row
					    		while($row = $result->fetch_assoc()) 
					    		{
					       			$GLOBALS['purchaseCounter'] = $row['counter'];
									echo $row['counter'];
								}
							}

						?>
					Products
					</h2>
					<?php
                      $sql = "select c.todayc,yt.yestc from (select count(*) as todayc from purchase p where DATE(created_at) = CURDATE() and status=1) as c,(select count(*) as yestc from purchase p where DATE(created_at) = CURDATE()-1 and status=1) as yt";
                      $result = $con->query($sql);
                      $p =0;
                      if ($result->num_rows > 0) 
                      {
                          // output data of each row
                          while($row = $result->fetch_assoc()) 
                          {
                            $currentV= $row['todayc'];
                            $previousV= $row['yestc'];
                            $z =  $previousV - $currentV;
                            if($z >0){
                              $p = ($z / $previousV)*100;
                              $p = ($p * 100)/100;
                            }
                          }
                      }
                    ?>
                    <h6 class="card-text">
                      <?php 
                        if($p < 0){
                          echo "Decreased by ".abs($p)."%";
                        }else if($p > 0){
                          echo "Increased by ".$p."%";
                        }else{
                          echo "No Update";
                        }

                      ?>
                      
                    </h6>
				  </div>
				</div>

              </div>
              <div class="col-md-4 stretch-card grid-margin">
              	<div class="card bg-dark text-white" style="height: 210px; background: linear-gradient(to right, #3366ff 0%, #99ccff 100%);">
				  <img src="../assets/images/dashboard/circle.svg" class="card-img"  alt="circle-image" >
				  <div class="card-img-overlay">
				    <h5 class="card-title">Storage</h5>
				     <h2 class="mb-5"><?php $sql = "SELECT count(*) as counter FROM total_purchase_qty where quantity >0";
						$result = $con->query($sql);

						if ($result->num_rows > 0) 
						{
						    // output data of each row
				    		while($row = $result->fetch_assoc()) 
				    		{
        						$GLOBALS['storageCounter'] = $row['counter'];
					 			echo $row['counter'];
							}
						}

					?>
					Items</h2>
					<?php
                      $sql = "select c.todayc,yt.yestc from (select count(*) as todayc from purchase p,total_purchase_qty tpq where p.item = tpq.item and p.quantity_type = tpq.quantity_type and p.ptype = tpq.ptype and tpq.quantity != 0 and DATE(created_at) = CURDATE()) as c,
						  (select count(*) as yestc from purchase p,total_purchase_qty tpq where p.item = tpq.item and p.quantity_type = tpq.quantity_type and p.ptype = tpq.ptype and tpq.quantity != 0 and DATE(created_at) = CURDATE()-1) as yt";
                      $result = $con->query($sql);
                      $p =0;
                      if ($result->num_rows > 0) 
                      {
                          // output data of each row
                          while($row = $result->fetch_assoc()) 
                          {
                            $currentV= $row['todayc'];
                            $previousV= $row['yestc'];
                            $z =  $previousV - $currentV;
                            if($z >0){
                              $p = ($z / $previousV)*100;
                              $p = ($p * 100)/100;
                            }
                          }
                        }
                    ?>
                    <h6 class="card-text">
                      <?php 
                        if($p < 0){
                          echo "Decreased by ".abs($p)."%";
                        }else if($p > 0){
                          echo "Increased by ".$p."%";
                        }else{
                          echo "No Update";
                        }

                      ?>
                    </h6>
				  </div>
				</div>

              </div>
              
              <div class="col-md-4 stretch-card grid-margin">
              	<div class="card bg-dark text-white" style="height: 210px;background: linear-gradient(to right, #00cc66 0%, #99ffcc 100%);">
				  <img src="../assets/images/dashboard/circle.svg" class="card-img"  alt="circle-image" >
				  <div class="card-img-overlay">
				    <h5 class="card-title">Stock</h5>
				    <h2 class="mb-5"><?php $sql = "SELECT count(*) as counter FROM admin_dpts where send_to =10 and send_to_quantity != 0";
						$result = $con->query($sql);

						if ($result->num_rows > 0) 
						{
						    // output data of each row
				    		while($row = $result->fetch_assoc()) 
				    		{
       						 $GLOBALS['stockCounter'] = $row['counter'];
					 echo $row['counter'];
					
					 
							}
						}

					?>
					Items</h2>
					<?php
                      $sql = "select c.todayc,yt.yestc from (select count(*) as todayc from admin_dpts ad where DATE(created_at) = CURDATE() and send_to =10 and send_to_quantity != 0) as c,(select count(*) as yestc from admin_dpts ad where DATE(created_at) = CURDATE()-1 and send_to =10 and send_to_quantity != 0) as yt";
                      $result = $con->query($sql);
                      $p =0;
                      if ($result->num_rows > 0) 
                      {
                          // output data of each row
                          while($row = $result->fetch_assoc()) 
                          {
                            $currentV= $row['todayc'];
                            $previousV= $row['yestc'];
                            $z =  $previousV - $currentV;
                            if($z >0){
                              $p = ($z / $previousV)*100;
                              $p = ($p * 100)/100;
                            }
                          }
                          
                      }
                    ?>
                    <h6 class="card-text">
                       <?php 
                        if($p < 0){
                          echo "Decreased by ".abs($p)."%";
                        }else if($p > 0){
                          echo "Increased by ".$p."%";
                        }else{
                          echo "No Update";
                        }

                      ?>
                    </h6>
				  </div>
				</div>
                
              </div>
            </div>
            <br>
            <div class="row">
              <div class="col-md-6 grid-margin stretch-card">
                <div class="card">
                  <div class="card-body">
                    <h4 class="card-title">Overall Data Collection</h4>
                    <div id="traffic-chart-legend"></div>
                  </div>
                </div>
              </div>
              <div class="col-md-6 grid-margin stretch-card">
                <div class="card">
                  <div class="card-body">
                    <h4 class="card-title">Recent Users</h4>
                    <div class="table-responsive">
                        <table id="datatableid" class="table table-striped table-bordered table-light table-hover" >
	                        <thead>
	                          <tr>
	                            <th><b> User Name</b> </th>
	                            <th><b> Pass Word</b></th>
	                            <th> <b>Status</b> </th>
	                            <th><b> Last Update</b></th>
								<th> <b>Password Change </b></th>
	                          </tr>
	                        </thead>
	                        <tbody>
	                        	<?php 
									$sql = "SELECT * FROM user where status=1";
									$result = $con->query($sql);

									if ($result->num_rows > 0) 
									{
									    // output data of each row
							    		while($row = $result->fetch_assoc()) 
							    		{
								?>
								 <tr>
		                            <td>
		                              <?php echo $row['user_name']?>
		                            </td>
		                            <td> <?php echo $row['password']?>  </td>

		                            <td>
		                              <a class="nav-link" href="#!"><label class="badge badge-gradient-success" >ACTIVE</label></a>
		                            </td>
		                            <td> <?php echo $row['updated_by']?> </td>
		                            <td> <button style="height:30px" style="width:150px " class="badge badge-gradient-info pass">Password Change</button> </td>
		                            <td style="display:none;"><?php echo $row['user_id']?></td>
		                          </tr>
								  <?php
										}
									}

								  ?>
							</tbody>
                        </table>
                	</div>
                  </div>
                </div>
              </div>
            </div>
        </div>
    </div>
</div>

<!-- Update Customer Modal -->
<div id="password" class="modal fade" role="dialog" tabindex="-1" aria-hidden="true"> 
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
	  <h4 class="modal-title">Change Password</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        
      </div>
      <div class="modal-body">
       
		<form>
		
					  
		 <div class="form-group">
		    <label style="width:150px;" for="user">User Name : </label>
		  <input style="width:150px;" type="text"  id="user" name="user" readonly>
		    
		  </div>	  
				 <div class="form-group">
		    <label style="width:150px;" for="p_qantity">Old Password : </label>
			  <input style="width:150px;" type="text" id="opass" name="opass" readonly>
		  
		  </div>	
		  <div class="form-group">
		    <label style="width:150px;" for="p_qantity">New Password: </label>
		    <input style="width:150px;" type="text"  id="npass" name="npass" required>
			  <input type="hidden" id="userid" name="userid">
		  
		  </div>
		  <div class="dropdown-divider"></div>
		  
		  <input type="hidden" name="id" id="id">
		  <input type="hidden" name="send_from" id="send_from" value="Other">
		  <button type="submit" id="updatP" class="btn btn-gradient-primary mr-2">Submit</button>
		  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
		</form> 

      </div>
     
    </div>

  </div>
</div>

</body>
<script type="text/javascript">
$(document).ready(function(){

	$('#datatableid').on('click','.pass',function() {
			
		$('#password').modal('show');

		$tr = $(this).closest('tr');

		var data = $tr.children('td').map(function(){
			return $(this).text();
		}).get();

		//console.log('Updated By',htmlString);
		
		$('#user').val(data[0].trim());
		$('#opass').val(data[1].trim());
		$('#userid').val(data[5].trim());
  
	});

	$('#updatP').on('click',function() {
     var  userId = $('#userid').val();
     var oPass = $('#opass').val();
     var nPass = $('#npass').val();
     
     $.ajax({
        url: '../controller/dashboard_controller.php',
        method: 'POST',
        data:'type=updatePassword&userid='+userId+'&opass='+oPass+'&npass='+nPass,
        
        success: function(msg) {
            console.log("typeof msg : ", typeof msg);
            console.log('Message',msg.status);
            if(msg.status == true){
              alert('Password Updated');
            }else{
              alert("Failed to update password.");
            }
        },

        error: function (request, status, error) {
          console.log("request : ", request);
          console.log("status  : ", status);
          console.log("error   : ", error);
          alert("Failed to update password.");
        }             
    });
   });

	Highcharts.chart('traffic-chart-legend', {
    chart: {
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false,
        type: 'pie'
    },
    title: {
        text: ''
    },
    tooltip: {
        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
    },
    accessibility: {
        point: {
            valueSuffix: '%'
        }
    },
    plotOptions: {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                format: '<b>{point.name}</b>: {point.percentage:.1f} %'
            }
        }
    },
    series: [{
        name: 'Brands',
        colorByPoint: true,
        data: [{
            name: 'Purchase',
            y: <?php echo $purchaseCounter; ?>,
            sliced: true,
            selected: true
        }, {
            name: 'Storage',
            y: <?php echo $storageCounter; ?>
        }, {
            name: 'Stock',
            y: <?php echo $stockCounter; ?>
        }]
    }]
});

});

</script>

</html>