<?php include("../Header/Session.php"); ?>
<?php include("../Header/Connection.php"); ?>


<!DOCTYPE html>
<html lang="en">
<?php include ("../Header/head.php"); ?>
<body class="skin-default fixed-layout">

<!-- Main wrapper - style you can find in pages.scss -->
<div id="main-wrapper">    
<?php include ("../Header/header.php"); ?>
<?php include ("../Header/left_menu.php"); ?>
<?php include ("../Header/right_sidebar.php"); ?>

<?php
$type_wallet = $_GET['type_wallet'];
//$type_wallet = "e_wallet";

date_default_timezone_set("Asia/Kuala_Lumpur");

$todayDay = (int)date('j');           // 1..31
$monthName = date('F');              // February
$yearNum   = date('Y');              // 2026

$withdraw_day_1_display  = "1st $monthName $yearNum";
$withdraw_day_16_display = "16th $monthName $yearNum";

$isWithdrawDay = in_array($todayDay, [1, 16], true);

$first_day_of_this_month = date("Y-m-01");
$last_day_of_this_month = date("Y-m-t");
$last_day_of_this_month_display = date('d-m-Y', strtotime($last_day_of_this_month));
$today = date('Y-m-d');
      
$sql_wallet = "SELECT * 
FROM wallet 
WHERE ref_code='$ref_code' LIMIT 1";
$result_wallet = $mysqli->query($sql_wallet);
if ($result_wallet->num_rows > 0)
{
	$row_wallet = $result_wallet ->fetch_assoc();
	$e_wallet = $row_wallet['cash_wallet'];
    $gold_wallet = $row_wallet['product_wallet'];
    $pins_wallet = $row_wallet['pins_wallet'];
}

if ( $type_wallet == "e_wallet")
{
    $wallet = $e_wallet ;
    $walletname = "e-Wallet";
    $type_of_transaction = "CW";
}
else  
if ( $type_wallet == "gold_wallet")
{
    $wallet = $gold_wallet;
    $walletname = "Gold Wallet";
    $type_of_transaction = "PW";
}
else  
if ( $type_wallet == "pins_wallet")
{
    $wallet = $pins_wallet;
    $walletname = "Capital Wallet";
    $type_of_transaction = "DW";
}

$sql_curr_rate = "SELECT *
				    FROM currency_rate_setting
				    Where status='1'
				    order by id Asc";
$result_curr_rate = $mysqli-> query($sql_curr_rate);
if ($result_curr_rate -> num_rows > 0)
{
	$row_curr_rate = $result_curr_rate ->fetch_assoc();
	$withdrawal_rm = $row_curr_rate['withdrawal_rm'];
}

$wallet_rm = $wallet * $withdrawal_rm;

$sql_user = "SELECT * 
FROM user 
WHERE ref_code='$ref_code'";
$result_user = $mysqli->query($sql_user);
if ($result_user ->num_rows > 0)
{
	$row_user = $result_user ->fetch_assoc();
	$name = $row_user['name'];
    $last_name = $row_user['last_name'];
    $phone_number = $row_user['phone_number'];
    $address = $row_user['address'];
    $address_2 = $row_user['address_2'];
    $state = $row_user['state'];
    $area = $row_user['area'];
    $postcode = $row_user['postcode'];
    $country = $row_user['country'];
    $verification_status = $row_user['verification_status'];
    $withdraw_block_status = $row_user['withdraw_block_status'];
}

$sql_curr_rate = "SELECT *
				  FROM currency_rate_setting
				  Where status='1'
				  order by id Asc";
$result_curr_rate = $mysqli->query($sql_curr_rate);
if ($result_curr_rate ->num_rows > 0)
{
    $row_curr_rate = $result_curr_rate  ->fetch_assoc();
    $deposit_rm = $row_curr_rate['deposit_rm'];
    $withdrawal_rm = $row_curr_rate['withdrawal_rm'];
    $withdraw_fees_usd = $row_curr_rate['withdraw_fees_usd'];
    $withdraw_fees_usdt_percent = $row_curr_rate['withdraw_fees_usdt_percent'];
}

//$max_withdraw_usdt = $wallet - (($withdraw_fees_usdt_percent/$wallet)*100) ;
//$max_withdraw_usd = $wallet - $withdraw_fees_usd ;

$q_rules = "SELECT *
            FROM system_setting
            Where status='1'";
$result_rules = $mysqli->query($q_rules);
if ($result_rules ->num_rows > 0)
{
    $row_rules = $result_rules ->fetch_assoc();
    $id = $row_rules['id'];
    $withdraw_button = $row_rules['withdraw_button'];
}

$sql_downline = "SELECT COUNT(id) As num 
FROM user 
WHERE sponsor_code ='$ref_code'
AND created_date BETWEEN '$first_day_of_this_month' AND '$last_day_of_this_month'
AND status='1'";
$result_downline = $mysqli->query($sql_downline);
$row_downline = $result_downline->fetch_assoc();
$num_downline = $row_downline['num'];		

?>
        
<!-- Page wrapper  -->
<div class="page-wrapper">
<!-- Container fluid  -->
<div class="container-fluid">

    <!-- Bread crumb and right sidebar toggle -->
    <!--?php include ("../Header/breadcumb.php"); ?-->
    <br><br>
    <!-- End Bread crumb and right sidebar toggle -->
    
    <!-- Start Page Content -->
    <!-- ============================================================== -->

    <?php

    $ref_code = $_GET['ref_code'];
    if ( $ref_code == '')
    {
        $ref_code=$_SESSION['ref_code'];
	}

    $q = "SELECT *
    FROM user
    WHERE ref_code='$ref_code'";
    $result = $mysqli->query($q);
    if ($result->num_rows > 0)
    {
        while($record = $result -> fetch_assoc())
        {           
            $id = $record['id'] ;
	        $sponsor_code = $record['sponsor_code'] ;
            $ref_code = $record['ref_code'] ;
            $name = $record['name'];
	        $email = $record['email'];
	        $password = $record['password'];
	        $phone_number = $record['phone_number'];
            $paid = $record['paid'];
            $address = $record['address'];
            $bank_type = $record['bank_type'];
            $acc_no = $record['acc_no'];
            $acc_holder = $record['acc_holder'];
			   
    ?> 

    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-body">
                    <form method="post" action="../Controller/Database_sql.php" onsubmit="return confirm('Do you want to proceed?');" enctype="multipart/form-data">
                        <div class="form-body">

                            <h3 class="card-title">Withdraw <?php echo $walletname; ?> : $<?php echo number_format($wallet,2); ?> (RM <?php echo number_format($wallet_rm,2); ?>)</h3>          
                            <hr>
                            <?php if ($withdraw_button == '0')
                            {
                            ?>
                            <div class="alert alert-info"> <i class="ti-user"></i> <b>Withdraw Notice</b><br>
                            Withdraw is unable for a moment. Please try again later within 10 minutes from now
                            </div>
                            <?php
		                    }
                            ?>
                            <br>
                            <?php if ($withdraw_block_status == '1')
                            {
                            ?>
                            <div class="alert alert-info"> <i class="ti-user"></i> <b>Withdraw Notice</b><br>
                            Withdraw is disable for your account. Please contact admin for more info.
                            </div>
                            <?php
		                    }
                            ?>

                            <font color="red">Important Notice. </font><br>
                            <?php
                            if ($type_wallet == "e_wallet")
                            {
                            ?>
                                Withdrawal Are Allowed Only at <b><?php echo $withdraw_day_1_display; ?></b> and <b><?php echo $withdraw_day_16_display; ?></b>. <br>
                                The Minimum Daily Withdrawal is USD10. <br>
                                Withdrawal Request Will Be Processed in 3 to 5 days (Exclude Saturday & Sunday). <br>
                                USD 1 = RM <?php echo $withdrawal_rm; ?> <br>
                            <?php
                            }
                            else
                            if ($type_wallet == "pins_wallet")
                            {
                            ?>
                                The Minimum Daily Withdrawal is USD10 and No limit for Maximum withdraw. <br>
                                Withdrawal Request Will Be Processed in 7 to 14 days (Exclude Saturday & Sunday). <br>
                            <?php
							}
                            ?>
                            
                            <div class="row p-t-20">

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label">Withdraw To</label>
                                        <br>
                                        <select class="form-control" name="withdraw_way" id="withdraw_way" onchange="myCalculation()" required>
                                            <option value="">Please Select Withdraw To</option>
                                            <option value="bank">Bank Account</option>
                                            <!--option value="usdt">USDT Wallet</option-->  
                                        </select>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label">Amount To Withdraw($) | Max $ <span id="max_withdrawn"></span></label>
                                        <input type="number" step="0.01" min="10" max="" class="form-control"  name="amount" id="amount" onkeyup="myCalculation()"  required>
                                        <input type="hidden" class="form-control" id="wallet" onkeyup="myCalculation()" value="<?php echo $wallet; ?>">
                                    </div>
                                </div>

                                <div id="bank_type_div" class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label">Type Of Bank</label>
                                        <input type="text" class="form-control"  name="bank" value="<?php echo $bank_type; ?>" readonly>
                                    </div>
                                </div>

                                <div id="acc_no_div" class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label">Account Bank Number</label>
                                        <input type="text" class="form-control"  name="acc_no" value="<?php echo $acc_no; ?>" readonly>
                                    </div>
                                </div>

                                <div id="acc_holder_name_div" class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label">Account Holder Name</label>
                                        <input type="text" class="form-control"  name="acc_holder_name" value="<?php echo $acc_holder; ?>" readonly>
                                    </div>
                                </div>

                                <div id="withdraw_fees_usd_div" class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label">Fees($)</label>
                                        <input type="text" class="form-control" id="withdraw_fees_usd" value="<?php echo $withdraw_fees_usd; ?>" readonly> 
                                        <input type="hidden" class="form-control" id="withdraw_fees_usd" onkeyup="myCalculation()" value="<?php echo $withdraw_fees_usd; ?>">
                                    </div>
                                </div>

                                <input type="hidden" class="form-control" id="withdraw_fees" readonly>
                                <input type="hidden" class="form-control" id="withdraw_fees_usdt_percent" onkeyup="myCalculation()" value="<?php echo $withdraw_fees_usdt_percent; ?>">
        
                            </div>
                            <input type="hidden" class="form-control"  name="id" value="<?php echo $id; ?>">
                            <input type="hidden" class="form-control"  name="ref_code" value="<?php echo $ref_code; ?>">
                            <input type="hidden" class="form-control"  name="type_wallet" value="<?php echo $type_wallet; ?>">

                        </div>
                        <div class="form-actions">
                        <?php if ($verification_status == "1")
                        {
                           if ($withdraw_button == '1' && $withdraw_block_status == '0')
                           {
                           ?>
                                <?php
                                $canWithdraw =
                                    ($verification_status == "1") &&
                                    ($withdraw_button == '1') &&
                                    ($withdraw_block_status == '0') &&
                                    ($isWithdrawDay === true);

                                // Optional: only enforce day-rule for e_wallet
                                // If you want the rule only for e_wallet, use:
                                // && ($type_wallet != "e_wallet" || $isWithdrawDay === true);
                                ?> 

                                <?php if ($canWithdraw) { ?>
                                    <button name="withdraw" class="btn btn-success"><i class="fa fa-check"></i> Withdraw</button>
                                <?php } else { ?>
                                    <button name="withdraw" class="btn btn-success" disabled><i class="fa fa-check"></i> Withdraw</button>

                                    <?php if ($verification_status != "1") { ?>
                                        &nbsp;&nbsp;
                                        <a href="user_detail.php?user_ref_code=<?php echo $ref_code; ?>">
                                            Please Upload Your ID/Passport Image For Verification To Enable Your Withdrawal Request. Click Here To Upload
                                        </a>
                                    <?php } ?>

                                    <?php if ($verification_status == "1" && $withdraw_button == '1' && $withdraw_block_status == '0' && !$isWithdrawDay && $type_wallet == "e_wallet") { ?>
                                        <div class="note error" style="margin-top:10px;">
                                            Withdraw is only available on <b><?php echo $withdraw_day_1_display; ?></b> and <b><?php echo $withdraw_day_16_display; ?></b>.
                                        </div>
                                    <?php } ?>

                                <?php } ?>
                           <?php
                           }
                           else
                           if ($withdraw_button == '0')//Cannot withdraw
                           {
                           ?>
                               <button name="withdraw" class="btn btn-success" disabled><i class="fa fa-check"></i> Withdraw</button>
                           <?php
						   }
                           else
                           if ($withdraw_block_status == '1')//Cannot withdraw
                           {
                           ?>
                               <button name="withdraw" class="btn btn-success" disabled><i class="fa fa-check"></i> Withdraw</button>
                           <?php
						   }
                           ?>
                        
                        <?php
						}
                        else
                        {
                        ?>
                           <button name="withdraw" class="btn btn-success" disabled><i class="fa fa-check"></i> Withdraw</button>
                           &nbsp;&nbsp;
                           <a href="user_detail.php?user_ref_code=<?php echo $ref_code; ?>">Please Upload Your ID/Passport Image For Verification To Enable Your Withdrawal Request. Click Here To Upload</a>
                        <?php
						}
                        ?>
                            
                        </div>
                    </form>
                </div>
            </div>                     
        </div>
    </div>
    <?php
    }
    }   
    ?>				
    <!-- ============================================================== -->
    <!-- End PAge Content -->

</div>
<!-- End Container fluid  -->
</div>
<!-- End Page wrapper  -->

<script>

function myCalculation()
{
    amount = document.getElementById("amount").value;
    wallet = document.getElementById("wallet").value;
    withdraw_way = document.getElementById("withdraw_way").value;
    withdraw_fees_usdt_percent = document.getElementById("withdraw_fees_usdt_percent").value;
    withdraw_fees_usd = document.getElementById("withdraw_fees_usd").value;

    withdraw_fees = Number(amount) * (Number(withdraw_fees_usdt_percent)/100);
    withdraw_max_fees = Number(wallet) * (Number(withdraw_fees_usdt_percent)/100);
   
    if (withdraw_way == 'bank')
    {
        withdraw_max = Number(wallet) - Number(withdraw_fees_usd);
	}
    else
    if (withdraw_way == 'usdt')
    {
        withdraw_max = Number(wallet) - Number(withdraw_max_fees);
	}

    console.log(withdraw_max);
    

    document.getElementById("withdraw_fees").value = withdraw_fees.toFixed(2);
    document.getElementById("amount").max = withdraw_max.toFixed(2);
    document.getElementById("max_withdrawn").innerHTML = withdraw_max.toFixed(2);

    if (withdraw_way == "bank")
      {
	    $("#withdraw_fees_div").hide();

        $("#acc_no_div").show();
	    $("#acc_holder_name_div").show();
	    $("#withdraw_fees_usd_div").show();	

        document.getElementById("blockchain_address").required = false;
      }
      else
      if (withdraw_way == "usdt")
      {
	    $("#bank_type_div").hide();
        $("#acc_no_div").hide();
	    $("#acc_holder_name_div").hide();
	    $("#withdraw_fees_usd_div").hide();	
	
        $("#withdraw_fees_div").show();	
	
	    document.getElementById("blockchain_address").required = true;
      }

}
</script>


<!-- Footer Start-->
<?php include ("../Footer/footer.php"); ?>
<!-- Footer End-->

</div>
<!-- End Wrapper -->
<!-- All Jquery -->
    

<!-- All Jquery Start-->
<?php include ("../Header/all_jquery.php"); ?>
<!-- All Jquery End-->

    

</body>

</html>