<?php
// require_once('basic-function.php');
class withdraw extends basicFunction 
{
    use table_names;

    public function interest($txn_id,$amount)
    {
        $vals = [$txn_id,$amount];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }

        $user = $this->selectWhereDlt($this->user_tb,'unique_id',$_SESSION[$this->user_session])[0];
        $balance = $this->get_balance_for_txn($txn_id);
        if($balance < 1 || $amount > $balance){
            return $this->errorMsgs(21);
        }
        $txn_exist = $this->getDataDlt($txn_id,$this->transaction_tb);
        $user_address = $this->selectWhere2CondDlt($this->user_wallet_tb,'user_id',$user['unique_id'],'coin_id',$txn_exist['coin_type_id']);
        if(empty($user_address[0]['address'])){
            return $this->errorMsgs(22);
        }
        
        $unique_id = $this->rand_id();
        $date_created = $this->todayDate();
        $query = "INSERT INTO $this->withdrawal_tb VALUES (null,
        '".mysqli_real_escape_string($this->connectDb(),$unique_id)."',
        '" . mysqli_real_escape_string($this->connectDb(), $user['unique_id']) . "',
        '".mysqli_real_escape_string($this->connectDb(),'INTEREST')."',
        '".mysqli_real_escape_string($this->connectDb(),$txn_id)."',
        '".mysqli_real_escape_string($this->connectDb(),$amount)."',
        '".mysqli_real_escape_string($this->connectDb(),'PENDING')."',
        '".mysqli_real_escape_string($this->connectDb(),0)."',
        '".mysqli_real_escape_string($this->connectDb(),$date_created)."',
        '".mysqli_real_escape_string($this->connectDb(),$date_created)."') ";
        mysqli_close($this->connectDb());
        if($this->sqlQuery($query)){
            
            $updated_interest = $balance - $amount;
            $fields = ['interest_in_usd'];
            $values = [$updated_interest];
            $update_interest = $this->update_tbs( $this->transaction_tb,$fields,$values,$txn_id);
            if($update_interest['error'] == 1){
                return $update_interest;
            }

            return $this->successMsg('Withdrawal');
        }else{
            return $this->errorMsgs(14);
        }

    }
    public function referrer_bonus($txn_id,$amount)
    {
        $vals = [$txn_id,$amount];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }

        $txn_exist = $this->getDataDlt($txn_id,$this->referrer_bonus_tb);

        $user = $this->selectWhereDlt($this->user_tb,'unique_id',$_SESSION[$this->user_session])[0];
        $balance = $this->get_balance_for_bonus($txn_id);
        if($balance < 1 || $amount > $balance){
            return $this->errorMsgs(21);
        }
        
        $user_address = $this->selectWhere2CondDlt($this->user_wallet_tb,'user_id',$user['unique_id'],'coin_id',$txn_exist['coin_id']);
        if(empty($user_address[0]['address'])){
            return $this->errorMsgs(22);
        }
        
        $unique_id = $this->rand_id();
        $date_created = $this->todayDate();
        $query = "INSERT INTO $this->withdrawal_tb VALUES (null,
        '".mysqli_real_escape_string($this->connectDb(),$unique_id)."',
        '" . mysqli_real_escape_string($this->connectDb(), $user['unique_id']) . "',
        '".mysqli_real_escape_string($this->connectDb(),'BONUS')."',
        '".mysqli_real_escape_string($this->connectDb(),$txn_id)."',
        '".mysqli_real_escape_string($this->connectDb(),$amount)."',
        '".mysqli_real_escape_string($this->connectDb(),'PENDING')."',
        '".mysqli_real_escape_string($this->connectDb(),0)."',
        '".mysqli_real_escape_string($this->connectDb(),$date_created)."',
        '".mysqli_real_escape_string($this->connectDb(),$date_created)."') ";
        mysqli_close($this->connectDb());
        if($this->sqlQuery($query)){
            
            $updated_interest = $balance - $amount;
            $fields = ['amt_in_usd'];
            $values = [$updated_interest];
            $update_interest = $this->update_tbs( $this->referrer_bonus_tb,$fields,$values,$txn_id);
            if($update_interest['error'] == 1){
                return $update_interest;
            }

            return $this->successMsg('Withdrawal');
        }else{
            return $this->errorMsgs(14);
        }

    }
    public function get_balance_for_txn($txn_id)
    {
        $txn_exist = $this->selectWhereDlt($this->transaction_tb,'unique_id',$txn_id);
        if(count($txn_exist) > 0){
                $balance = $txn_exist[0]['interest_in_usd'];
            return $balance;
        }
        return 0;
        
    }
    public function get_balance_for_bonus($txn_id)
    {
        $txn_exist = $this->selectWhereDlt($this->referrer_bonus_tb,'unique_id',$txn_id);
        if(count($txn_exist) > 0){
                $balance = $txn_exist[0]['amt_in_usd'];
            return $balance;
        }
        return 0;
        
    }

    public function get_total_interest_balance($user_id)
    {
        $balance = 0;
        $txn_exist = $this->selectWhereDlt($this->transaction_tb,'user_id',$user_id);
        if(count($txn_exist) > 0){
            foreach ($txn_exist as $e ) {
                $balance += $e['interest_in_usd'];
            }
            return $balance;
        }
        return $balance;
        
    }

    public function paid($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $withdraw_exist = $this->selectWhereDlt($this->withdrawal_tb, 'unique_id', $id);
        if (count($withdraw_exist) < 1) {
            return $this->errorMsgs(15);
        }
        $fields = ['status'];
        $values = ['PAID'];
        $update_txn = $this->update_tbs( $this->withdrawal_tb,$fields,$values,$id);
        if($update_txn['error'] == 1){
            return $update_txn;
        }
        return $this->successMsg2('Withdrawal Confirmation');
    }
    public function pending($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $withdraw_exist = $this->selectWhereDlt($this->withdrawal_tb, 'unique_id', $id);
        if (count($withdraw_exist) < 1) {
            return $this->errorMsgs(15);
        }
        $fields = ['status'];
        $values = ['PENDING'];
        $update_txn = $this->update_tbs( $this->withdrawal_tb,$fields,$values,$id);
        if($update_txn['error'] == 1){
            return $update_txn;
        }
        return $this->successMsg2('Withdrawal Unconfirmation');
    }
    public function delete($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $fields = ['is_deleted'];
        $values = ['1'];
        $delete_txn = $this->update_tbs( $this->withdrawal_tb,$fields,$values,$id);
        if($delete_txn['error'] == 1){
            return $delete_txn;
        }
        return $this->dltSuccessMsg();
    }
    
}
$withdraw = new withdraw();