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

    public function create($full_name,$email,$password,$c_password,$level)
    {

        $vals = [$full_name,$email,$password,$level];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }

        // valid email
        $valid_email = $this->validate_email($email);
        if(!$valid_email){
            return $this->errorMsgs(18);
        }

        $if_exist_email = $this->selectWhereDlt($this->admin_tb,'email',$email);
        // return $if_exist_email;
        if(count($if_exist_email) > 0){
            return $this->errorMsgs(8);
        }
        if($password !== $c_password){
            return $this->errorMsgs(2);
        }
        $hash_pass = $this->hashPass($password);
        $unique_id = $this->rand_id();
        $date_created = $this->todayDate();
        $query = "INSERT INTO $this->admin_tb VALUES (null,
        '".mysqli_real_escape_string($this->connectDb(),$unique_id)."',
        '".mysqli_real_escape_string($this->connectDb(),$email)."',
        '".mysqli_real_escape_string($this->connectDb(),$full_name)."',
        '".mysqli_real_escape_string($this->connectDb(),null)."',
        '".mysqli_real_escape_string($this->connectDb(),$hash_pass)."',
        '".mysqli_real_escape_string($this->connectDb(),$level)."',
        '".mysqli_real_escape_string($this->connectDb(),null)."',
        '".mysqli_real_escape_string($this->connectDb(),0)."',
        '".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)){
            return $this->successMsg('Administrator');

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

    public function block($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $fields = ['is_blocked'];
        $values = ['1'];
        $block_user = $this->update_tbs( $this->admin_tb,$fields,$values,$id);
        if($block_user['error'] == 1){
            return $block_user;
        }
        return $this->successMsg3('Admin Blocked');
    }
    public function unblock($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $fields = ['is_blocked'];
        $values = ['0'];
        $block_user = $this->update_tbs( $this->admin_tb,$fields,$values,$id);
        if($block_user['error'] == 1){
            return $block_user;
        }
        return $this->successMsg3('Admin Account Activated');
    }
    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_user = $this->update_tbs( $this->admin_tb,$fields,$values,$id);
        if($delete_user['error'] == 1){
            return $delete_user;
        }
        return $this->dltSuccessMsg();
    }

    public function count_users()
    {
        $all_users = $this->selectAllDlt($this->user_tb);
        return count($all_users);
    }
    public function count_investments()
    {
        $all_deposits = $this->selectWhereDlt($this->transaction_tb,'status','confirmed');
        return count($all_deposits);
    }
    public function amt_in_investments()
    {
        $amt = 0;
        $all_deposits = $this->selectWhereDlt($this->transaction_tb,'status','confirmed');
        foreach ($all_deposits as $e ) {
            $amt += $e['amt_in_usd'];
        }
        return $amt;
    }
    public function confirmed_deposits()
    {
        $amt = 0;
        $all_deposits = $this->selectWhere2CondDlt($this->wallet_tb,'status','confirmed','description','my_deposit');
        foreach ($all_deposits as $e ) {
            $amt += $e['amt_in_usd'];
        }
        return $amt;
    }
    public function amt_withdrawals_to_pay()
    {
        $amt = 0;
        $all_deposits = $this->selectWhere2CondDlt($this->wallet_tb,'action_type','withdrawals','status','pending');
        foreach ($all_deposits as $e ) {
            $amt += $e['amt_in_usd'];
        }
        return $amt;
    }
    public function support()
    {
        $all_support = $this->selectWhereDlt($this->support_tb,'main',1);
        return count($all_support);
    }
    public function total_withdrawals_cashout()
    {
        $all_deposits = $this->selectWhere2CondDlt($this->wallet_tb,'action_type','withdrawals','status','pending');
        return count($all_deposits);
    }
    public function paid_interest_cashout()
    {
        $all_support = $this->selectWhere2CondDlt($this->wallet_tb,'description','investment_interest','status','confirmed');
        return count($all_support);
    }

    public function get_total_Wallet_balance()
    {
        $top_ups = 0;
        $withdraws = 0;
        $top_up_exist = $this->selectWhere2CondDlt($this->wallet_tb, 'action_type', 'top_up', 'status', 'confirmed');
        if (count($top_up_exist) > 0) {
            foreach ($top_up_exist as $e) {
                $top_ups += $e['amt_in_usd'];
            }
        }

        $withdrawals_exist = $this->selectWhereDlt($this->wallet_tb, 'action_type', 'withdrawal');
        if (count($withdrawals_exist) > 0) {
            foreach ($withdrawals_exist as $e) {
                $withdraws += $e['amt_in_usd'];
            }
        }
        if ($top_ups > 0) {
            $balance = $top_ups - $withdraws;
            return $balance;
        } else {
            return 0;
        }
    }
    
}
$admin = new admin();