<?php
session_start();
include "connect.php";
$currentpage=8;
$activepage="Credit";

if(!isset($_SESSION['admin_login'])){ header("location:index.php");}
$user = "dochcare";
$q_sql="SELECT balance_request_tbl.*,user_tbl.name,user_tbl.user_unique_id,bank_details.bank_name,bank_details.acc_no FROM `balance_request_tbl` JOIN user_tbl ON user_tbl.id = balance_request_tbl.user_id JOIN bank_details ON bank_details.id = balance_request_tbl.bank_id";
$q_query=mysqli_query($conn,$q_sql." WHERE balance_request_tbl.status = 0 ORDER BY balance_request_tbl.id DESC");

	if (isset($_REQUEST['idd']) && $_REQUEST['active'] == 'true') {
    $query_service = "UPDATE `balance_request_tbl` SET `status`= 1 WHERE id ='".$_REQUEST['idd']."'";
     
        $result_service = mysqli_query($conn,$query_service);
		
		if($result_service){
			$query_sel = "SELECT * FROM `balance_request_tbl` WHERE id ='".$_REQUEST['idd']."'";
			$result_sel = mysqli_query($conn,$query_sel);
			$row_sel = mysqli_fetch_array($result_sel);
			$user_id = $row_sel['user_id'];
			$amt = $row_sel['amt'];
			
			$query_sel1 = "SELECT * FROM `user_wallet` WHERE user_id = '$user_id'";
			$result_sel1 = mysqli_query($conn,$query_sel1);
			$num = mysqli_num_rows($result_sel1);
			if($num == 0){
			$query_sel2 = "INSERT INTO `user_wallet`(`id`, `user_id`, `amt`) VALUES (NULL,'$user_id','$amt')";
			$result_sel2 = mysqli_query($conn,$query_sel2);
			}else{
			$query_sel2 = "UPDATE `user_wallet` SET `amt` = amt  + $amt WHERE user_id = '$user_id'";
			$result_sel2 = mysqli_query($conn,$query_sel2);
			}
			echo "<script>alert('Credit request approved successfully. ');   window.location = 'credit_request.php'; </script>";
			// header("location:view_branch.php");
		}
}
if (isset($_POST['edit_reason']) && $_POST['edit_reason'] == 'Reason') {
	$reason = $_REQUEST['reason'];
    $query_service = "UPDATE `balance_request_tbl` SET `reason`='$reason',`status`= -1 WHERE id ='".$_POST['user_id']."'";
     
        $result_service = mysqli_query($conn,$query_service);
		
		if($result_service){
			
			echo "<script>alert('Credit request rejected successfully. ');   window.location = 'credit_request.php'; </script>";
			// header("location:view_branch.php");
		}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>:: SCSCROWD | View Member ::</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<?php include "common/CSS_link.php";?>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="js/jquery-ui.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
    $('#MyTable').DataTable( {
        initComplete: function () {
            this.api().columns().every( function () {
                var column = this;
                var select = $('<select><option value=""></option></select>')
                    .appendTo( $(column.footer()).empty() )
                    .on( 'change', function () {
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val()
                        );
                //to select and search from grid
                        column
                            .search( val ? '^'+val+'$' : '', true, false )
                            .draw();
                    } );
 
                column.data().unique().sort().each( function ( d, j ) {
                    select.append( '<option value="'+d+'">'+d+'</option>' )
                } );
            } );
        }
    } );
} );
</script>
<script>
  var dateToday = new Date(); 
	$( function() {	
    $( ".datepicker" ).datepicker({
    dateFormat: 'yy-mm-dd'   
	});
  } ); 
 
    
function isNumber(evt) {
	var iKeyCode = (evt.which) ? evt.which : evt.keyCode
	if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57)){
		return false;
	}
	return true;
} 

$(function() {

	$(".name").focus();
	var clients = [
	<?php						
		$q_pps=mysqli_query($conn,"SELECT * FROM user_tbl");
		while($r_pps=mysqli_fetch_array($q_pps)){
	?>
	  {        
        label: "<?php echo $r_pps['user_name'];?>",        
        icon: "<?php echo $r_pps['user_id'];?>"
      },
	<?php	}	?>
	 ];
    $( ".name" ).autocomplete({
      minLength: 0,
      source: clients,
      focus: function( event, ui ) {
        $( ".name" ).val( ui.item.label );
		return false;
      },
      select: function( event, ui ) {			
        $( ".name" ).val( ui.item.label );        
        return false;
      }
    })
    .autocomplete( "instance" )._renderItem = function( ul, item ) {		
	 return $( "<li>" )
       .append( "<a>" + item.label + "<br><span style='color:#808080; font-size:10px;'>" + item.icon + "</span></a>" )
	    //.append( "<a>" + item.label + "</a>" )
        .appendTo( ul );
		};
  });

$(function() {

	$("#name123").focus();
	var clients = [
	<?php						
		$q_pps123=mysqli_query($conn,"SELECT * FROM company_tbl GROUP BY company_id,company_name ASC LIMIT 10");
		while($r_pps123=mysqli_fetch_array($q_pps123)){
	?>
	  {        
        label: "<?php echo $r_pps123['company_id'];?>",
		icon: "<?php echo $r_pps123['company_name'];?>",
      },
	<?php	}	?>
	<?php						
		$q_pps=mysqli_query($conn,"SELECT * FROM user_tbl GROUP BY user_id,user_name ASC LIMIT 10");
		while($r_pps=mysqli_fetch_array($q_pps)){
	?>
	  {               
        label: "<?php echo $r_pps['user_id'];?>",
		icon: "<?php echo $r_pps['user_name'];?>",
      },
	<?php	}	?>
	 ];
    $( "#name123" ).autocomplete({
      minLength: 0,
      source: clients,
      focus: function( event, ui ) {
        $( "#name123" ).val( ui.item.label );
		return false;
      },
      select: function( event, ui ) {			
        $( "#name123" ).val( ui.item.label );        
        return false;
      }
    })
    .autocomplete( "instance" )._renderItem = function( ul, item ) {		
	 return $( "<li>" )
       .append( "<a>" + item.label + "<br><span style='color:#808080; font-size:10px;'>" + item.icon + "</span></a>" )
	   // .append( "<a>" + item.icon + "</a>" )
        .appendTo( ul );
		};
  });  
</script>
<style>
[class*=" pe-7s-"], [class^=pe-7s-] {
    display: inline-block;
    font-family: Pe-icon-7-stroke;
    speak: none;
    font-style: normal;
    font-weight: 400;
    font-variant: normal;
    text-transform: none;
    line-height: 1;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}
* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
user agent stylesheet
i, cite, em, var, address, dfn {
    font-style: italic;
}
.content-header .header-icon {
    font-size: 55px;
    color: #37a000;
    width: 68px;
    float: left;
    margin-top: -4px;
    line-height: 0;
}
.link {padding: 10px 15px;background: transparent;border:#bccfd8 1px solid;border-left:0px;cursor:pointer;color:#607d8b}
.disabled {cursor:not-allowed;color: #bccfd8;}
.current {background: #bccfd8;}
.first{border-left:#bccfd8 1px solid;}
.question {font-weight:bold;}
.answer{padding-top: 10px;}
#pagination{margin-top: 20px;padding-top: 30px;border-top: #F0F0F0 1px solid;}
.dot {padding: 10px 15px;background: transparent;border-right: #bccfd8 1px solid;}
#overlay {background-color: rgba(0, 0, 0, 0.6);z-index: 999;position: absolute;left: 0;top: 0;width: 100%;height: 100%;display: none;}
#overlay div {position:absolute;left:50%;top:50%;margin-top:-32px;margin-left:-32px;}
.page-content {padding: 20px;margin: 0 auto;}
.pagination-setting {padding:10px; margin:5px 0px 10px;border:#bccfd8  1px solid;color:#607d8b;}
</style>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
  <header class="main-header">
    <?php include "common/header.php";?>
  </header> 
  <aside class="main-sidebar">    
    <section class="sidebar">
      <?php include "common/LEFT_sidebar.php";?>
  </aside>  
  <div class="content-wrapper">    
    <section class="content-header">
	<div class="header-icon">
	 <i class="fa fa-file-text-o" style="color:#37a000; font-size:50px;"></i>
	    </div>
      <h1 style="fonyt-family:'Alegreya Sans',sans-serif; margin: 0; font-size: 24px">Credit Request<br><span style="font-size: 13px; display: inline-block; padding-left: 4px; font-weight: 600; color: #7a7a7a;">Credit Request</span></h1>
      <ol class="breadcrumb">
        <li><a href="home.php"><i class="fa fa-dashboard"></i>Home</a></li>        
        <li class="active" style="color:#37a000; font-weight:bold;">Credit Request</li>
      </ol>
    </section>    
    <section class="invoice">      
      <div class="row">
        <div class="col-xs-12">
          
			<!--<div style="clear:both;">
				<div style="float:left;"><input type="text" class="form-control name" id='name' placeholder="Name"  name="name"> </div>
				<div style="float:left;">&nbsp;<button class="btn btn-sm btn-primary" type="submit" id='item-search' style="margin-top:2px;"><i class="fa fa-search"></i></button></div>
				
				<div style="float:left; margin-left:10px;"><input type="text" class="form-control" id='name123' placeholder="Name"  name="name"> </div>
				<div style="float:left;">&nbsp;<button class="btn btn-sm btn-success" type="submit" id='item-search123' style="margin-top:2px;"><i class="fa fa-search"></i></button></div>
			</div>
			
            <div class="form-inline" role="form">				
			<div style="display: inline-block" class="pull-right">
			<div class="form-group">
				<label>From</label>
				<input type="text" class="form-control datepicker" id='from' placeholder="Start date"  name="from">
			</div>
			<div class="form-group">
				<label>To</label>
				<input type="text" class="form-control datepicker" id='to' placeholder="End date"  name="to">                         
				<button class="btn btn-sm btn-primary" style="" type="submit" id='date-search'>Search</button>
			</div> 
			</div>                       
            </div>   -->
        </div>        
      </div>     
      <div class="row">
        <div class="col-xs-12 table-responsive">
		<div id="result" style="margin:15px 0;"></div>
          <table id="MyTable" class="table" border="1" style="width:100%;" >
            
            <thead style="font-family:sans; background:#008080; color:#fff;">
			 <tr>
                                                <th>#</th>
                                                <th>Member Name</th>
                                                <th>Member Id</th>
                                                <th>Amount</th>
                                                <th>Bank name</th>
                                                <th>Account</th>
                                                <th>Tnx No</th>
                                                <th>Action</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
									<?php $co1 = 1; 
									 //$sno = 1; 
									
									//$flag = 1; ?>
            <?php 
				while($row=mysqli_fetch_array($q_query)){
				
				?>
				
				  <tr>			       
			  <td><?php echo $co1 ?></td>
              <td><?php echo $row['name'];?></td>
			  <td><?php echo $row['user_unique_id'];?></td>
			  <td><?php echo $row['amt'];?></td>
			  <td><?php echo $row['bank_name'];?></td>
			  <td><?php echo $row['acc_no'];?></td>
			  <td><?php echo $row['txn_number'];?></td>
			  <td class="text-left"><a class="btn btn-success"  href="credit_request.php?active=true&idd=<?php echo $row['id']; ?>" >Approve</a></td>
			  <td class="text-left">
			  
			  	<a class="btn btn-danger" db_id ='<?php echo $co1; ?>' href="#modal-container-666931" role="button" onclick="get_CompData('<?php echo $row['id']; ?>');" data-toggle="modal" title="Edit">Rejection</a></td>
			   
            </tr>
			
			
				<?php 
				
				   } // end while 
			?>
           
                                 </tbody>
          </table>	
						
        </div>       
      </div>        
       
    </section>
    <div class="clearfix"></div>
  </div>
  <div class="modal fade" id="modal-container-666931" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header" style="background:#605ca8; color:#FFF;">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="color:#FFF;">×</button>
				<h4 class="modal-title" id="myModalLabel">Reason <span id="edit123"style="font-weight:bold;"></span></h4>
			</div>
			<div class="modal-body">
				<div id='ajax_edit'></div>                        
				<form class="form-horizontal" role="form" id="editComp" action="#" method="POST" enctype="multipart/form-data" >
					<input type="hidden" name="edit_reason" value="Reason" />			
				<div class="form-group">					
					<div class="col-sm-6">
						<label class="control-label" id="text_chnge1">Enter Reason <span style="color:red;">*</span></label>						
						<input type="text" class="form-control" name="reason" id="reason" placeholder="Reason" autocomplete="off" required>
					</div>
					
				</div>				
				<input type="hidden" name="user_id" id="user_id" value=""/>
				<div class="form-group">
				  
				  <div class="col-sm-6">
					<div class=" col-sm-12">
					<label class="control-label">&nbsp;&nbsp;&nbsp;	</label>
					<label class="control-label">&nbsp;&nbsp;&nbsp;	</label> </div>
					  <button type="submit" class="btn btn-success" name="submit" id="submit">Reject</button>
				 
					</div>
				  
				</div>                          
			</form>
			</div>
			</div>                    
		</div>
	</div>       
       
  
  <footer class="main-footer">
    <?php include "common/footer.php";?>
  </footer>
</body>
</html> 
<script>
function get_CompData(id){
					
			document.getElementById('user_id').value=id;
			
}
</script>
<script src="assets/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="assets/bower_components/raphael/raphael.min.js"></script>
<script src="assets/bower_components/morris.js/morris.min.js"></script>
<script src="assets/bower_components/jquery-sparkline/dist/jquery.sparkline.min.js"></script>
<script src="assets/plugins/jvectormap/jquery-jvectormap-1.2.2.min.js"></script>
<script src="assets/plugins/jvectormap/jquery-jvectormap-world-mill-en.js"></script>
<script src="assets/bower_components/jquery-knob/dist/jquery.knob.min.js"></script>
<script src="assets/bower_components/moment/min/moment.min.js"></script>
<script src="assets/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js"></script>
<script src="assets/bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<script src="assets/bower_components/fastclick/lib/fastclick.js"></script>
<script src="assets/dist/js/adminlte.min.js"></script>
<script src="assets/dist/js/pages/dashboard.js"></script>
<script src="assets/dist/js/demo.js"></script>