<?php
require "connect.php";
if (isset($_POST['withdraw'])) {
    $amount = $_POST['amount'];
    $method = $_POST['method'];
    $coin = $_POST['coin'];
    $wallet_add = $_POST['wallet_add'];
    $bank_name = $_POST['bankName'];
    $acct_name = $_POST['accountName'];
    $acct_no = $_POST['accountNumber'];
    $acct_swift = $_POST['sort_code'];

    if ($method == "Bank") {
      $withdrawal_des = "Bank Name:$bank_name, Acct No:$acct_no, Account Name:$acct_name, Swift Code:$acct_swift";
      $t_mode = "$method";
    }else {
      $withdrawal_des = $wallet_add;
      $t_mode = "$method ($coin)";
    }


    if (empty($amount)) {
        $error = "<div class='alert alert-danger d-flex align-items-center' role='alert'>
          <i class='fa fa-warning'></i> &nbsp; enter withdrawable amount to complete withdrawal
        </div>";
    }else if ($method == "Bank" AND empty($bank_name) || empty($acct_name) || empty($acct_no)) {
      $error = "<div class='alert alert-danger d-flex align-items-center' role='alert'>
        Please enter Bank Details to complete withdrawal!
      </div>";

    }else if ($method == "Crypto" AND empty($wallet_add)) {
      $error = "<div class='alert alert-danger d-flex align-items-center' role='alert'>
        Please enter $coin wallet address to complete withdrawal
      </div>";

    }else if ($amount > $balance) {
      $error = "<div class='alert alert-danger d-flex align-items-center' role='alert'>
        insufficient balance, make a deposit!
      </div>";
    }else {

        $transaction_status = "pending";
        $transaction_type = "withdrawal";
        $trnx_date = date("d-m-Y");
        $d_amount = number_format($amount, 2);
        $character = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i=0; $i < 7; $i++) {
            $index = rand(0, strlen($character) -1);
            $randomString .=$character[$index];
        }
        $randomString;
        $trnx_hash = $randomString;


        $sql = "INSERT INTO transaction (trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, asset, transaction_amount, wallet_address, transaction_date) VALUES (?,?,?,?,?,?,?,?,?)";
        $stmt = $connection->prepare($sql);
        $stmt->bind_param("sssssssss", $trnx_hash, $user_id, $transaction_type, $transaction_status, $t_mode, $coin, $amount, $withdrawal_des, $trnx_date);
        if ($stmt->execute()){

          $new_balance = $balance - $amount;
          $sql_update = "UPDATE users SET balance = '$new_balance' WHERE user_id = '$user_id'";
          if ($connection->query($sql_update) === true) {
            // send mail
                $to = "$email";
                $subject = "Withdrawal Notification";
                $message = "<div style='background: #000; padding: 10px;'>
                <br>
                <center><img src='$website_url/images/$logo_img' width='200px;'></center>
                <br>
                <div style='width: 90%; font-family: sans-serif; padding: 10px 4%; background: ; margin: 5px auto; border-radius: 0px; color:#fff;'>
                <center>
                  <h3>Mail From $website_email</h3>
                  <h4>Successsful Withdrawal</h4>
                </center>
                <hr>
                <p>
                <b>Hi $fname</b>, <br>
                Your withdrawal request is pending, Please wait while the transaction is been process <br>
                <b>Funds will be disburse to your $t_mode details after Network confirmation</b>
              <br><br>

                <div style='width:100%; padding:12px 1.5% 8px 1.5%; margin: 0; background: #151030;'>
                  <h3 style='margin:0 0%;'>Transaction Summary</h3>
                  <p style='font-size:80%; line-height:35px; margin:; text-align:left;'>
                    Amount: <b> <span>$currency</span>$d_amount</b><br>
                    Withdrawal Method: <b> $t_mode</b><br>
                    $t_mode Details: <b> $withdrawal_des</b><br>
                    Transaction ID: <b> $trnx_hash </b><br>
                    IP address: <b> $userIP</b>
                    <br><br>
                  </p>
                </div>

                <br><br>
                Contact live support or email $website_email for any complain.
                </p>
                <hr>
                <br>

                </div>
                <br>
                <center><p style='font-size: 13px; color:#ccc'>$website_email<sup>TM</sup> </p></center>
                <br>
                </div>";

                // Always set content-type when sending HTML email
                $headers = "MIME-Version: 1.0" . "\r\n";
                $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

                // More headers
                $headers .= "From: $website_name <$website_email>" . "\r\n";
                $headers .= "Reply-To: $website_email \r\n";
                $headers .= "Return-Path: $website_email\r\n";
                // $headers .= "BCC: $website_email\r\n";

                if (mail($to,$subject,$message,$headers,"-f $website_email")){
                  // send mail
                  $to = "$website_email";
                  $subject = "User Withdrawal Request";
                  $message = "
                  <div style='background: #E4E9F0; padding: 10px;'>
                  <br>
                  <div style='font-family: arial; padding:10px 0 0 0; margin: 5px; background: white; margin: 5px 5%; border:1px solid rgba(0,0,0,0.4);'>
                  <center><img src='$website_url/images/$logo_img' width='200px;'></center>
                    <p style='font-size:100%; line-height:20px; margin: 0 3%'>
                      Hello Admin, someone made a Withdrawal Request
                    </p>

                    <br>

                    <div style='width:96%; padding:12px 0 8px 0; margin: 0 2%; background: #E8F2FA;'>
                    <h3 style='margin:0 3%;'>Transaction Summary</h3>
                        <p style='font-size:100%; line-height:35px; margin:0 3%;'>
                          Name: <b>$full_name ($username)</b><br>
                          Amount: <b><span>$currency</span>$d_amount</b><br>
                          Withdraw Method: <b> $method</b><br>
                          $t_mode Details: <b> $withdrawal_des</b><br>
                          Transaction ID: <b> $trnx_hash </b><br>
                          IP address: <b> $userIP</b><br>
                          Date: <b>$trnx_date</b>
                        </p>
                    </div>

                    <br><br>

                    <p style='font-size:100%; line-height:20px; margin:0 3%;'>
                      Best regards, <br>
                      $website_name.
                    </p>
                    <br><br>
                  </div><br>
                  </div>";

                  // Always set content-type when sending HTML email
                  $headers = "MIME-Version: 1.0" . "\r\n";
                  $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

                  // More headers
                  $headers .= "From: $website_name <$website_email>" . "\r\n";
                  $headers .= "Reply-To: $website_email \r\n";
                  $headers .= "Return-Path: $website_email\r\n";
                  // $headers .= "BCC: $website_email\r\n";

            if (mail($to, $subject, $message, $headers,"-f $website_email")) {
              echo "<script>
                window.location.href='withdrawal.php?status=success'
              </script>";
                } else {
                  echo "<script>
                    window.location.href='withdrawal.php?status=error'
                  </script>";
                }
            }else {
              echo "<script>
                window.location.href='withdrawal.php?status=error'
              </script>";
            }
          } else {
            echo "<script>
              window.location.href='withdrawal.php?status=error'
            </script>";
          }
      }else {
        echo "<script>
          window.location.href='withdrawal.php?status=error'
        </script>";
      }
    }

}
?>
