<?php 
require_once('includes/check-login.php');
require_once('includes/header.php'); 
require_once('includes/menu.php'); 
require_once('includes/config.php'); 
$tablename="wallet";	
$addurl="addwallet.php";
if($_SESSION['role_id']==1){
$sql="SELECT user_details.user_id,(SUM(debit)*-1) + SUM(credit) AS CustomerBalance,name,email,phone FROM ".$tablename." RIGHT JOIN user_details ON user_details.user_id=wallet.user_id JOIN user_roles ur ON user_details.user_id = ur.user_id JOIN roles r ON r.role_id = ur.role_id WHERE ur.role_id IN(2) GROUP BY wallet.user_id";
}else{
$sql1=$con->query("SELECT user_details.user_id,(SUM(debit)*-1) + SUM(credit) AS CustomerBalance,name,wallet.created,wallet.description FROM ".$tablename." RIGHT JOIN user_details ON user_details.user_id=wallet.user_id JOIN user_roles ur ON user_details.user_id = ur.user_id JOIN roles r ON r.role_id = ur.role_id WHERE ur.role_id IN(2) and user_details.user_id = '".$_SESSION['userid']."' GROUP BY wallet.user_id");  
$row=mysqli_fetch_array($sql1);
$balance=isset($row['CustomerBalance']) ? $row['CustomerBalance'] :0;

$sql="SELECT ((debit)*-1) + (credit) AS CustomerBalance,created,description,user_id FROM ".$tablename." WHERE user_id = '".$_SESSION['userid']."' order by id desc";  
}
$result = $con->query($sql);
?>
<!-- Datatable CSS -->
<link href='data_tables/datatables.min.css' rel='stylesheet' type='text/css' /><!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">                                
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>                    
                        <li class="active"><a href="#">Wallet</a></li>           
                    </ol>                
            </section>

            <!-- Main content -->
            <section class="content">                
                <!-- Default box -->
                <div class="box box-form">
                    <div class="box-header with-border">
                        <h3 class="box-title">Wallet</h3>
						<?php if(isset($_SESSION[$tablename."succ_msg"])) {?>
                 <div class="alert alert-success">
                 <a href="#" class="close" data-dismiss="alert" aria-label="close" title="close">×</a>
                 <?php echo $_SESSION[$tablename."succ_msg"];  unset($_SESSION[$tablename."succ_msg"]);?>
                 </div>
                <?php }?>   
                <?php if(isset($_SESSION[$tablename."err_msg"])) {?>
                <div class="alert alert-success">
                <a href="#" class="close" data-dismiss="alert" aria-label="close" title="close">×</a>
                <?php echo $_SESSION[$tablename."err_msg"]; unset($_SESSION[$tablename."err_msg"]);?>
                </div>
               <?php }?>			  
                        <div class="box-tools pull-right">
                            <button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="Collapse">
                                <i class="fa fa-minus"></i></button>
                            <button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
                                <i class="fa fa-times"></i></button>
                        </div>
                 <?php if($_SESSION['role_id']==2){?>        
				<header class="box-typical-header"><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
			    <div class="tbl-cell btn-group btn-group-sm"><a href="<?php echo $base_url.$addurl;?>" class="tabledit-delete-button btn btn-sm btn-success" title="Add"><span class="glyphicon glyphicon-plus"></span> Add</a></div>
				</header>
				<?php }?>
                    </div>
                    <div class="box-body">    
                        <!-- Table -->
                        <?php if($_SESSION['role_id']==1){?>
                        <table id='workapp' class='display dataTable table table-bordered table-hover'>
                            <thead>
                            <tr>
                                <th>User ID</th>                            
                                <th>Name</th>              
                                <th>Email</th>
                                <th>Phone</th>
                                <th>Wallet</th> 
                            </tr>
                            </thead> 
                              <tbody>
							<?php $i=1;
							if ($result->num_rows > 0) {
                            while($row = $result->fetch_assoc()) {
                            $CustomerBalance=$row['CustomerBalance'];    
                            if($CustomerBalance==""){
                            $CustomerBalance=0;
                            }?>
						   <tr>
						   <td><?php echo $row['user_id'];?></td>
						   <td><?php echo $row['name'];?></td>
						   <td><?php echo $row['email'];?></td>
						   <td><?php echo $row['phone'];?></td>
						   <td><?php echo $CustomerBalance;?></td>
						    </tr>								  
							 <?php $i++;}}?>
                           </tbody>							
                        </table>
                        <?php } else {?>
                        <h2> Balance : <?php echo number_format((float)$balance, 2, '.', '');?></h2>
                        <table id='workapp' class='display dataTable table table-bordered table-hover'>
                            <thead>
                            <tr>
                                <th>User ID</th>    
                                <th>Date</th>
                                <th>Description</th>
                                <th>Wallet</th> 
                            </tr>
                            </thead> 
                              <tbody>
							<?php $i=1;
							if($result->num_rows > 0) {
                            while($row = $result->fetch_assoc()) {
                            $CustomerBalance=$row['CustomerBalance'];    
                            if($CustomerBalance==""){
                            $CustomerBalance=0;
                            }?>
						   <tr>
						   <td><?php echo $row['user_id'];?></td>
						   <td><?php echo date("Y-m-d",strtotime($row['created']));?></td>
						   <td><?php echo $row['description'];?></td>
						   <td><?php echo $CustomerBalance;?></td>
						    </tr>								  
							 <?php $i++;}}?>
                           </tbody>							
                        </table>
                        <?php }?>

                    </div>
                    
                    <!-- /.box-footer-->                    
                </div>
                <!-- /.box -->

            </section>
            <!-- /.content -->                                        
        </div>
        <!-- /.content-wrapper -->        
<script src="js/common.js"></script>  
<script src="js/datatables.js"></script>    
<script>
$(document).ready( function() {
$(".checkAll").change(function () {
    $("input:checkbox").prop('checked', $(this).prop("checked"));
});
});
$(document).ready( function() {		
		$(".deleterow").off('click').on('click',function(e) {
		e.preventDefault();
		var anyBoxesChecked = false;
		var deletearray = new Array();		
		$('input[name="id"]:checked').each(function(){			
			deletearray.push($(this).val());
              anyBoxesChecked = true;	
		});		
		 if(anyBoxesChecked == true) {
                Lobibox.confirm({
                    msg: "Are you sure you want to delete this row?",
                    callback: function ($this, type) {
                        if (type === 'yes') {
                            Lobibox.notify('success', {
								msg: 'You have Deleted Successfully.'			     
                            });           
	       var table="<?php echo $tablename;?>";		  
           $.ajax({
           type: "POST",
           url: "deleterecord.php",
           data:{id:deletearray,table:table},
           cache: false,
           success: function(data){
          setTimeout(function(){location.reload();},1000); 
            }   
           });               } else if (type === 'no') {
                            Lobibox.notify('info', {
                                msg: 'Your row not deleted.'
                            });
                        }
                    }
                });
				}
			else{
				alert("Please select at least one checkbox");
			}
            });
})
</script>
<script src="<?php echo $base_url;?>js/lobibox.js"></script>  
<?php require_once('includes/footer.php'); ?>