<?php
require_once '../includes/core.php';
require '../includes/connect.php';


$user_id = mysql_real_escape_string($_GET['userid']);

$result1 = mysql_query("SELECT * FROM users WHERE user_id = '$user_id' ");
$test1 = mysql_fetch_array($result1);

if (!$result1) 
		{
		die("Error: Data not found..");
		}
		
		$balance1  =$test1['balance']; 

		$accountno  =$test1['accountno'];

		$bankname  =$test1['bankname'];

		$accountname  =$test1['accountname'];


		$firstname  =$test1['firstname'];


		$lastname  =$test1['lastname'];

$totalamount = @current(@mysql_fetch_assoc(@mysql_query("SELECT SUM(totalprofit) FROM trading WHERE user_id = '$user_id' AND DATE(tdate) =CURDATE()")));

$result2 = mysql_query("SELECT SUM(totalprofit) FROM trading WHERE user_id = '$user_id' AND DATE(tdate) =CURDATE() ");
$test2 = mysql_fetch_array($result2);

if (!$result2) 
		{
		die("Error: Data not found..");
		}
		
		

		$totalprofit  =$test2['totalprofit'];





$id =mysql_real_escape_string($_GET['id']);


$result = mysql_query("SELECT * FROM wallet WHERE id = '$id' ");
$test = mysql_fetch_array($result);

if (!$result) 
		{
		die("Error: Data not found..");
		}
		
		$amount  =$test['amount']; 








		 
		
		
		



if (loggedinadmin()){


$adminname = getfield1('name','admin','admin-id');
$admin_id = getfield1('admin-id','admin','admin-id');
$password = getfield1('password','admin','admin-id');

}else{ 
	header('Location:../loginexec.php');
}



?>





<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admin  - Dashboard</title>

<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/datepicker3.css" rel="stylesheet">
<link href="css/styles.css" rel="stylesheet">

<!--Icons-->
<script src="js/lumino.glyphs.js"></script>

<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->

</head>

<body>

    <?php include ('includes/header.php')  ?>

    <?php include ('includes/leftnav.php')  ?>
	
	
		
	<div class="col-sm-9 col-sm-offset-3 col-lg-10 col-lg-offset-2 main" style="margin-top: 20px">			
		<div class="row">
			<ol class="breadcrumb">
				<li><a href="#"><svg class="glyph stroked home"><use xlink:href="#stroked-home"></use></svg></a></li>
				<li class="active">home</li>
			</ol>
		</div><!--/.row-->
		
		<div class="row">
			<div class="col-lg-12">
				<h1 class="page-header"></h1>
			</div>
		</div><!--/.row-->


		
		<div class="row">
			<div class="col-lg-12">
				<div class="panel panel-default">
					<div class="panel-heading">WITHDRAWAL</div>
					<div class="panel-body">
						<div class="col-md-6">

<form role="form" action="" method="POST">
							
								
																
							<div class="form-group">
									<label for="amount">Name: <?php echo $firstname?> <?php echo $lastname?></label>
									
								</div>
								
								<div class="form-group">
									<label for="amount">Amount</label>
									<input type="text" id="amount" name="amount" value="<?php echo $amount ?>" max="<?php echo $totalprofit ?>" class="form-control">
								</div>

								<div class="form-group">
									<label for="bankname">Total User profit</label>
									<input type="number" id="balance" name="balance" value="<?php echo $totalamount ?>" class="form-control" readonly>
								</div>



								<button id="send"  type="submit" class="btn btn-primary">Submit</button>
								
		
       
						</form>

	

					</div>
<div class="col-md-6">
	
<div class="panel-heading">USER BANK DETAILS</div>

 <table class="table table-bordered table-sm">
                                        <tbody>
                                            <tr>
                                                <td style="color: #000">Bank:</td>
                                                <td class="text-left"><?php echo $bankname ?></td>
                                            </tr>
                                            <tr>
                                                <td style="color: #000">Acc Name:</td>
                                                <td class="text-left"><?php echo $accountname ?></td>
                                            </tr>
                                            <tr style="color: #000">
                                                <td>Acc Number:</td>
                                                <td class="text-left"><?php echo $accountno ?></td>
                                            </tr>
                                        </tbody>


                                    </table>


</div>





				</div>
			</div>
			</div>
			</div>




			<?php
					if (isset ($_POST['amount']) && isset ($_POST['balance'])  )
					{
						$errors = array();
						
							
							$amount = mysql_real_escape_string($_POST['amount']);

							$balance = mysql_real_escape_string($_POST['balance']);

							$balance2 = $balance - $amount ;
							
							
						
							

						if(!empty($amount))
							{
								
								
								
									if(strlen($amount) > 100 ) {
										$errors[] = 'Amount is too long';
									}else
									if($balance < $amount) {

										$errors[] = 'INSUFFICIENT FUNDS';
									

									}

								}

									else
								{
									$errors[] = 'Please fill the form before submitting it!!!';
								}
							if(!empty($errors))
							{
								foreach($errors as $error) {
									
								echo 

								'<div class="alert bg-danger" role="alert">
					<svg class="glyph stroked success"><use xlink:href="#stroked-success"></use></svg>',$error, '  <a href="#" class="pull-right"><span class="glyphicon glyphicon-remove"></span></a>
				</div>'




								;
								
								}
							
							}

							else 		
								{
									
									
	

// Fetch all the relevent rows using the below query statement.
$orders=mysql_query("SELECT * FROM trading WHERE `user_id` = '$user_id'")or die(mysql_error());

$data = [];


foreach($orders as $order) {

    $items = $this->db->where('totalprofit', $order['id']);
    $order_qty = $order['totalprofit'];

    foreach($items as $item){
        $item_qty = $item->amount - $order_qty;

        if ($item_qty <= 0) {
            // set amount to 0 because it is less than order quantity
            $data[] = ['id' => $item->id, 'amount' => 0];

            // Subtract amount from order quantity to get the remaining order
            $order_qty = $order_qty - $item->amount;

        } else {
             //If amount is more than order quantity set to calculated amount
             $data[] = ['id' => $item->id, 'amount' => $item_qty];

             // update order to 0
             $order_qty = 0;
        }

        //exit if order quantity is 0
        if ($order_qty === 0 ){
            break;
        }
    }
}

$this->db->update_batch('trading', $data, 'totalprofit');








									echo '<div class="alert bg-success" role="alert">
					<svg class="glyph stroked cancel"><use xlink:href="#stroked-cancel"></use></svg> The Account has been Credited successfully<a href="#" class="pull-right"><span class="glyphicon glyphicon-remove"></span></a>
				</div>';


									header('Location:withdrawprocess.php');
								}		
					}

			
					
					


				?>
 
 





			
								
	
	</div>	<!--/.main-->

	<script src="js/jquery-1.11.1.min.js"></script>
	<script src="js/bootstrap.min.js"></script>
	<script src="js/chart.min.js"></script>
	<script src="js/chart-data.js"></script>
	<script src="js/easypiechart.js"></script>
	<script src="js/easypiechart-data.js"></script>
	<script src="js/bootstrap-datepicker.js"></script>
	<script>
		$('#calendar').datepicker({
		});

		!function ($) {
		    $(document).on("click","ul.nav li.parent > a > span.icon", function(){          
		        $(this).find('em:first').toggleClass("glyphicon-minus");      
		    }); 
		    $(".sidebar span.icon").find('em:first').addClass("glyphicon-plus");
		}(window.jQuery);

		$(window).on('resize', function () {
		  if ($(window).width() > 768) $('#sidebar-collapse').collapse('show')
		})
		$(window).on('resize', function () {
		  if ($(window).width() <= 767) $('#sidebar-collapse').collapse('hide')
		})
	</script>	
</body>

</html>
