<?php
require_once(LIB_PATH . DS . 'database.php');

class Biodata extends DatabaseObject
{
    protected static $table_name = "admitted_student_details";
    public $id;
    public $pix;
    public $formno;
    public $surname;
    public $othernames;
    public $dob;
    public $nationality;
    public $state;
    public $lga;
    public $hometown;
    public $sex;
    public $maritalStatus;
    public $religion;
    public $phone;
    public $email;
    public $programme;
    public $courses;
    public $status;
    public $session;
    public $reg_date;
    public $level;
    public $department;
    public $names;
    public $faculty; 
    public $amount;
    public $serviceTypeId;
    public $apiKey;
    public $orderId;
    public $rrr;


      public static function updatebankpayment($stid,$amount,$message,$paymentDate,$transactiontime,$status )
   {
         global $database; $connection=''; 
       
        $stid = $database->escape_value($stid);
        $amount = $database->escape_value($amount);
        $paymentDate = $database->escape_value($paymentDate);
        $transactiontime = $database->escape_value($transactiontime);
        $status = $database->escape_value($status);

       $sql = "UPDATE acceptance_tranzact_remita SET returned_amount='$amount',paymentDate='$paymentDate',transactiontime='$transactiontime', statusMessage='$message' WHERE id= $stid LIMIT 1";
         if ($database->query($connection,$sql)) {
           
            return true;
          // return 'Success';
        } else {
           return false;
            // return 'Error'; 
        }
   }
    
     public static function get_transaction_bank_details($rrr)
    {
        global $database;
        $sql="SELECT * FROM acceptance_tranzact_remita WHERE rrr='$rrr' LIMIT 1";
         
         $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;
      
    }
     public static function updatepayment($stid,$amount,$paymentReference,$transactionId,$curDate )
   {
        global $database;
       
        $stid = $database->escape_value($stid);
        $amount = $database->escape_value($amount);
        $paymentReference = $database->escape_value($paymentReference);
        $transactionId = $database->escape_value($transactionId);

       $sql = "UPDATE acceptance_tranzact_remita SET returned_amount='$amount',paymentReference='$paymentReference',transactionId='$transactionId', statusMessage='Successful',paymentDate='$curDate' WHERE id= $stid LIMIT 1";
         if ($database->query($sql)) {
           
            return true;
          // return 'Success';
        } else {
           return false;
            // return 'Error'; 
        }
   }
     public static function getrrr($stid='')
    {
        global $database;
         $sql = "SELECT * FROM acceptance_tranzact_remita ";
        $sql .= " WHERE id= $stid limit 1";
        
         $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;
      
        
    }

     public static function updatejson($stid,$rrr,$orderID)
   {
       global $database; $connection=''; 
       
        $stid = $database->escape_value($stid);
        $rrr = $database->escape_value($rrr);
        $orderID = $database->escape_value($orderID);
        
       $sql = "UPDATE acceptance_tranzact_remita SET rrr='$rrr', orderId='$orderID' WHERE id= $stid  limit 1";
   

        if ($database->query($sql)) {
           
            return true;
          // return 'Success';
        } else {
           return false;
            // return 'Error'; 
        }
       
   }
   
   
      public static function updatejsonerror($stid,$rrr)
   {
       global $database; $connection=''; 
       
        $stid = $database->escape_value($stid);
        $rrr = $database->escape_value($rrr);
       $sql = "UPDATE acceptance_tranzact_remita SET rrr='$rrr',statusMessage='Unsuccessful' WHERE id= $stid LIMIT 1";
   
        if ($database->query($sql)) {
           
            return true;
          // return 'Success';
        } else {
           return false;
            // return 'Error'; 
        }
       
   }
    
    
  public static function checkorderID($a)
  {
      global  $database;
       $sql = "SELECT * FROM acceptance_tranzact_remita ";
        $sql .= " WHERE orderId= '$a' order by id desc limit 1";
        
         $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;
      
  }
    
   
    public static function getpaymenttogenrrr($paymentid)
    {
        global $database;
         $paymentid = $database->escape_value($paymentid);
        $sql="SELECT * FROM acceptance_tranzact_remita WHERE id= $paymentid LIMIT 1";
         $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;
    }
    
     public static function getcredentials($type='')
    {
        global $database;
        
        $sql="SELECT * FROM paymentitem WHERE type=$type LIMIT 1";
         
         $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;
      
        
    }
    
    public static function getFac($department)
    {
         global $database; 
         
        $department = $database->escape_value($department);
        $sql = "SELECT * FROM department_tbl ";
        $sql .= " WHERE department='{$department}' LIMIT 1 ";
    
        $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;
        
    }
    public static function savePaymentDetails($formno,$surname,$othernames,$faculty,$department,$level,$phone,$amount,$session,$email)
    {
         global $database; 
        $formno = $database->escape_value($formno);
        $surname = $database->escape_value($surname);
        $othernames = $database->escape_value($othernames);
        $faculty = $database->escape_value($faculty);
        $department = $database->escape_value($department);
        $level = $database->escape_value($level);
        $phone = $database->escape_value($phone);
        $amount = $database->escape_value($amount);
        $session = $database->escape_value($session);
        $email = $database->escape_value($email);
        
    
      
        $sql = "INSERT INTO acceptance_tranzact_remita (";
        $sql .= "formno, surname, othernames, faculty,department,phone,level,amount,session,email";
        $sql .= ") 	VALUES ('";
        $sql .= $formno . "', '";
        $sql .= $surname . "', '";
        $sql .= $othernames . "', '";
        $sql .= $faculty . "', '";
        $sql .= $department . "', '";
        $sql .= $phone . "', '";
        $sql .= $level . "', '";
        $sql .= $amount . "', '";
        $sql .= $session . "', '";
        $sql .= $email . "')";
        
        if ($database->query($sql)) {
          return  $paymentid = $database->insert_id();
           // return true;
        } else {
            return false;
        }
    }
    // public static function getAcceptanceAmount($feeCode,$level)
    // {
    //     global $database; 
    //     $feeCode = $database->escape_value($feeCode);
    //     $level = $database->escape_value($level);
    //      $sql = "SELECT * FROM payment_settings ";
    //     $sql .= " WHERE code= $feeCode ";
    //     $sql .= "AND level='$level' LIMIT 1";

    //     $result_array = self::find_by_sql($sql);
    //     return !empty($result_array) ? array_shift($result_array) : false;
    // }
    public static function authenticate($formno = "", $email = "")
    {
        global $database;
        $formno = $database->escape_value($formno);
        $email = $database->escape_value($email);
        $sql = "SELECT * FROM " . self::$table_name . " ";
        $sql .= " WHERE formno='{$formno}' ";
        $sql .= "AND email='{$email}' LIMIT 1";

        $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;
//return $result_array;
    }


   
    public function fullname()
    {

        if (isset($this->formno) && isset($this->username)) {
            return $this->formno . " " . $this->username;
        } else {
            return "";
        }
    }


//Common database Methods
    public static function find_all()
    {
        global $database;
        return self::find_by_sql("SELECT * FROM " . self:: $table_name);
        //return $result_set;

    }

       
    public static function find_by_id($formno = '', $email = '', $table='')
    {
        global $database;
        
        if (substr($formno,2,2)=='19'){
            $table ="admitted_student_details_2019";
            //$table="admission_list";
             $result_array = self::find_by_sql("SELECT a.pix,a.surname,a.othernames,a.formno,a.level,a.email,a.phone,p.department FROM  admitted_student_details_2019 a INNER JOIN admission_list p ON a.formno=p.formno WHERE p.formno='{$formno}' and a.email='{$email}' LIMIT 1");
             
             //$result_array = self::find_by_sql("SELECT * FROM  $table WHERE formno='{$formno}' LIMIT 1");
             
             
        }else {
        $result_array = self::find_by_sql("SELECT s.surname,s.othernames,a.formno,a.department,s.level,f.phone,a.session,s.email FROM admission_list a INNER JOIN $table f ON f.formno = a.formno INNER JOIN student_account s ON s.id=f.acc_id WHERE a.formno='$formno' and   s.email='$email' LIMIT 1");
        }
        //$found = $database->fetch_array($result_set);
        //return $found;
        return !empty($result_array) ? array_shift($result_array) : false;

    }
    

    public static function find_by_sql($sql = "")
    {
        global $database;
        $result_set = $database->query($sql);
        $object_array = array();
        while ($row = $database->fetch_array($result_set)) {
            $object_array[] = self::instantiate($row);
        }
        return $object_array;

    }

    private static function instantiate($record)
    {

        $object = new self;
        foreach ($record as $attribute => $value) {
            if ($object->has_attribute($attribute)) {
                $object->$attribute = $value;
            }

        }
        return $object;
    }

    private function has_attribute($attribute)
    {

        $object_vars = get_object_vars($this);
        return array_key_exists($attribute, $object_vars);
    }

    public function save()
    {
        return isset($this->id) ? $this->update() : $this->create();
    }

    public function create()
    {
        global $database;
        $sql = "INSERT INTO " . self::$table_name . " (";
        $sql .= "formno, email";
        $sql .= ") 	VALUES ('";
        $sql .= $database->escape_value($this->formno) . "', '";
        $sql .= $database->escape_value($this->email) . "') '";
        if ($database->query($sql)) {
            $this->id = $database->insert_id();
            return true;
        } else {
            return false;
        }

    }

    public function update()
    {

        global $database;
        $sql = "UPDATE " . self::$table_name . "(";
        $sql .= "formno = '" . $database->escape_value($this->formno) . "',";
        $sql .= "email ='" . $database->escape_value($this->email) . "', ";
        $sql .= "WHERE id=" . $database->escape_value($this->id);

        $database->query($sql);
        return ($database->affected_rows() == 1) ? true : false;
    }

    public function delete()
    {

        global $database;
        $sql = "DELETE FROM " . self::$table_name . " ";
        $sql .= "WHERE id=" . $database->escape_value($this->id);
        $sql .= " LIMIT 1";
        $database->query($sql);
        return ($database->affected_rows() == 1) ? true : false;
    }

//Closing tags for user's class
}

$biodata = new Biodata();
?>