<?php
	if (!isset($_COOKIE["user"]))
	{
		header("Location: ../login.html");
		exit();
	}
	
	if (isset($_GET["action"])) {
		if ($_GET["action"] == "logout")
		{
			setcookie("user", "logout", time() - (24 * 60 * 60 * 24), "/");
			header("Location: ../login.html");
			exit();
		}
	}
	
	$user = $_COOKIE["user"];
	$j_data = json_decode(file_get_contents("../u.j"), false);
	$withdraw_message = "";
    $admin_text = "";
    $email = $j_data->users->$user->email;
    $status = $j_data->users->$user->status;
    $interest_rate = $j_data->users->$user->interest_rate;
    $investment_date = $j_data->users->$user->investment_date;
    $investment_plan = $j_data->users->$user->investment_plan;
    $account_balance = $j_data->users->$user->account_balance;
    $currency = $j_data->users->currency;
    $investment_profits = $j_data->users->$user->investment_profits;
    $duration = $j_data->users->$user->duration;
    $daily_rate =($interest_rate/100) / ($duration * 7);
    $seconds_past = time() - $investment_date;
        $ref = $j_data->users->$user->refs_earnings;
$statm = " Please Contact Support For Account Upgrade to activate withdrawal";

  $pin = 792042;
	if (isset($j_data -> users -> $user))
	{
		if ($j_data -> users -> $user -> admin) {
			$admin_text = '<li>
                            <a href="admin.php" class="waves-effect">
                                <i class="fas fa-user-cog"></i><span> ADMIN Dashboard </span>
                            </a>
                        </li>';
		}
		$investment_profits = (float)$j_data -> users -> $user -> investment_profits;
		if (isset($_POST["amount"]) && isset($_POST["email"]) && isset ($_POST["address"]) && isset($_POST["method"]))
		{
			if ($status === 1  ) {
		
				
				if ( $_POST["pin"] != $pin ) {
					$withdraw_message = "<p style='color:#fc5454'>**contact support for withdrawal pin **</p>";

				} 				
				else {
					$withdraws_info = json_decode("{}", false);
					$withdraws_2_info = json_decode("{}", false);

					$withdraws_info -> ID = crypt(mt_rand(), mt_rand());
					$withdraws_info -> payment_method = $_POST["method"];
					$withdraws_info -> amount = $_POST["amount"];
					$withdraws_info -> address = $_POST["address"];
					$withdraws_info -> datee = date("d/m/Y H:m");
					$withdraws_info -> status = "PENDING";
					$withdraws_info -> email = $j_data -> users -> $user -> email;

					$withdraws_2_info -> ID = $withdraws_info -> ID;
					$withdraws_2_info -> user = $user;

					array_push($j_data -> users -> $user -> withdraws, $withdraws_info);
					array_push($j_data -> WITHDRAWS, $withdraws_2_info);

					$j_data -> users -> $user -> investment_profits = $investment_profits  - (float)$_POST["amount"];
					$j_data -> users -> $user -> num_withdraws += 1;
					$account_balance = (float)$j_data -> users -> $user -> account_balance;
					$file = fopen("../u.j", "w");
					fwrite($file, json_encode($j_data, JSON_PRETTY_PRINT));
					fclose($file);
				}
			} 
        
        
			else {
				$withdraw_message = "<p style='color:#fc5454'>**contact support for account upgrade to activate withdrawal**</p>";
			}
		}
		$email = $j_data -> users -> $user -> email;
		
		$withdraws_data = "";
		foreach (array_reverse($j_data -> users -> $user -> withdraws) as $var)
		{
			$withdraws_data .= '<tr> <th scope="row">'.$var -> ID.'</th> <td>'.$var -> payment_method.'</td> <td>'.$var -> amount.'</td> <td>'.$var -> datee.'</td>  <td>'.$var -> status.'</td> <td>'.$var -> address.'</td>  </tr>';
		}
		if ($withdraws_data == "") {
			$withdraws_data = "No withdraws have been made.";
		}
		else {
			$withdraws_data = '<table class="table table-striped mb-0"><thead> <tr> <th>Payment ID</th> <th>Payment Method</th><th>Amount</th> <th>Date</th><th>Status</th><th>address</th></tr></thead><tbody>'.$withdraws_data.'</tbody> </table>';
		}
	}
	else
	{
		setcookie("user", "logout", time() - (24 * 60 * 60 * 24), "/");
		header("Location: ../login.html");
		exit();
	}
?>

<!DOCTYPE html>
<html lang="en">


<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <title> Dashboard</title>
   
    <link rel="shortcut icon" href="ass/images/favicon.png">

    
    <link href="ass/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="ass/css/metismenu.min.css" rel="stylesheet" type="text/css">
    <link href="ass/css/icons.css" rel="stylesheet" type="text/css">
    <link href="ass/css/style.css" rel="stylesheet" type="text/css">

</head>

<body>

    <!-- Begin page -->
    <div id="wrapper">

        <!-- Top Bar Start -->
        <div class="topbar">

            <!-- LOGO -->
            <div class="topbar-left">
                <a href="index.php" class="logo">
                    <span class="logo-light">
                            <img src="logo.png" width="100%">
                        </span>
                    <span class="logo-sm">
                            <i class="fas fa-home"></i>
                        </span>
                </a>
            </div>

            <nav class="navbar-custom">
                

                <ul class="list-inline menu-left mb-0">
                    <li class="float-left">
                        <button class="button-menu-mobile open-left waves-effect">
                            <i class="mdi mdi-menu"></i>
                        </button>
                    </li>
                   
                </ul>

            </nav>

        </div>
        <!-- Top Bar End -->

        <!-- ========== Left Sidebar Start ========== -->
        <div class="left side-menu">
            <div class="slimscroll-menu" id="remove-scroll">

                <!--- Sidemenu -->
                <div id="sidebar-menu">
                    <!-- Left Menu Start -->
                    <ul class="metismenu" id="side-menu">
                        <li class="menu-title">Menu</li>
						<?php echo $admin_text; ?>
                        <li>
                            <a href="index.php" class="waves-effect">
                                <i class="icon-accelerator"></i><span> Dashboard </span>
                            </a>
                        </li>

                        <li>
                            <a href="account.php" class="waves-effect"><i class="fas fa-book"></i><span> Account Overview</span></a>
                           
                        </li>

                        <li>
                            <a href="profile.php" class="waves-effect"><i class="fas fa-user"></i><span> Profile</span></a>
                           
                        </li>

                        <li>
                            <a href="deposit.php" class="waves-effect"><i class="fas fa-hand-holding-usd"></i><span> Deposit</span></a>
                           
                        </li>

                   
                        <li>
                            <a href="withdraw.php" class="waves-effect"><i class="fas fa-donate"></i><span> Withdraw</span></a>
                           
                        </li>
						
						<li>
                            <a href="support.php" class="waves-effect"><i class="fas fa-mail-bulk"></i><span> Customer Support</span></a>
                           
                        </li>
						
						<li>
                            <a href="index.php?action=logout" class="waves-effect"><i class="fas fa-power-off"></i><span> Logout</span></a>
                           
                        </li>

                        

                    </ul>

                </div>
                <!-- Sidebar -->
                <div class="clearfix"></div>

            </div>
            <!-- Sidebar -left -->

        </div>
        <!-- Left Sidebar End -->

        <!-- ============================================================== -->
        <!-- Start right Content here -->
        <!-- ============================================================== -->
        <div class="content-page">
            <!-- Start content -->
			<div class="col-sm-12">
                                <h2 class="page-title">
								
								<br>
								<br>
								Withdraw Funds</h2>
            </div>
			
			<div class="content">
				
                <div class="container-fluid">
                 
   

                <div class="row">
				<div class="col-lg-6">
                                <div class="card m-b-30" >
                                    
                                      <div class="card-body" style="<?php if($Status == 0){echo "display: block;";}if ($status ==1) {echo "display: none;";} ?> ">
                                        
 <h2 class="mt-0 header-title" style='color:#fc5454'>Notice: </h2>
 
                                        <?php 
                                        if($status === 0 ){
                                            echo $statm; 
                                        } if ($status === 2 ){
                                            echo $ttatm;
                                        }
                                        
                                        ?>
                                    
                                    </div>
                                    
                                    <div class="card-body" style="<?php if($Status == 0){echo "display: none;";}if ($status ==1) {echo "display: block;";} ?> ">
        
                                        <h4 class="mt-0 header-title" >Withdrawal Form</h4>
                                        <?php echo $withdraw_message; ?>
                                        <form action="withdraw.php" method="post">
                                            <div class="form-group">
                                                <label>Email</label>
                                                <input type="email" class="colorpicker-default form-control" name="email" value=" <?php echo $email; ?>">
                                                <input type="hidden" name="type" value="dashboard">
                                            </div>
                                            <div class="form-group">
                                                <label>Amount</label>
                                                <input type="number" class="colorpicker-default form-control" name="amount">
                                                </div>
                                                
                                                                                                                                                      <div class="form-group">
                                                <label>BTC Address</label>
                                                <input type="text" class="colorpicker-default form-control" name="address">
                                            
                                            </div>
                                            

                                        <div class="form-group">
                                                <label>Withdrawal Pin</label>
                                                <input type="number" class="colorpicker-default form-control" name="pin">
                                            
                                            </div>

                                            <div class="form-group">
                                                <label>Payment Method</label>
                                                <select class="colorpicker-default form-control" name="method">
													<option>Bitcoin</option>
													
												</select>
                                            </div>
											<div class="form-group">
                                              
                                                <input type="submit" id="butt" class="colorpicker-default form-control" value="Make Withdrawal">
                                            </div>
                                            
                                        </form>
        
                                    </div>
                                </div>
        
                       
                            </div>
							<div class="col-lg-6">
                            <div class="col-sm-6 col-xl-6">
                            <div class="card">
                                <div class="card-heading p-4">
                                    <div class="mini-stat-icon float-right">
                                        <i class="fas fa-money-bill-alt bg-danger  text-white"></i>
                                    </div>
                                    <div>
                                        <h5 class="font-16"> Balance </h5>
                                    </div>
                                    <h3 class="mt-4"><? echo $currency ?><?php

$profit= $investment_profits + $account_balance;
echo number_format(  $profit,00);

           

                                                        ?></h3>

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


                                <div class="card m-b-30">
                                    <div class="card-body">
        
                                        <h4 class="mt-0 header-title">Withdrawal History</h4>
                                      
                                        <div class="table-responsive">
                                            <?php echo $withdraws_data; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
				</div>
			</div>
		</div>
		




       
            <footer class="footer">
© 2020 All Rights Reserved 
            </footer>

        </div>
        <!-- ============================================================== -->
        <!-- End Right content here -->
        <!-- ============================================================== -->

    </div>
    <!-- END wrapper -->

    <!-- jQuery  -->
    <script src="ass/js/jquery.min.js"></script>
    <script src="ass/js/bootstrap.bundle.min.js"></script>
    <script src="ass/js/metismenu.min.js"></script>
    <script src="ass/js/jquery.slimscroll.js"></script>
    <script src="ass/js/waves.min.js"></script>

    <script src="ass/pages/dashboard.init.js"></script>

    <!-- App js -->
    <script src="ass/js/app.js"></script>

</body>
</html>