<?php
namespace App\Models;

use CodeIgniter\Model;
use CodeIgniter\Database\RawSql;


class baseModel extends Model
{
    protected $db;
    public $con_name = '$float_coredb';

    public function __construct()
    {
        parent::__construct();
        $this->db = \Config\Database::connect($this->con_name);
        log_message('critical', "BaseModel-Entered" );
    }

    public function insert_db($inserDataArray, $insertTable)
    {
        log_message('critical', "insert_db-WAS CALLED user 000=>" );
            $insert_id=0;
            $ic = 0;
            $fl_part = '';
            $val_part = '';
            $sqlQ = '';
      //  try {
           
            $fields = array_keys($inserDataArray);
            log_message('critical', "insert_db-WAS CALLED user 009a=>" );
            foreach ($fields as $field) {
                $fl_part .= $ic > 0 ? ',' : '';
                $fl_part .= $field;
    
                $val_part .= $ic > 0 ? ',' : '';
                $val_part .= "'" . $inserDataArray[$field] . "'";
                $ic++;
            }
            log_message('critical', "insert_db-WAS CALLED user 009b=>" );
            $sqlQ = "INSERT INTO $insertTable ($fl_part) VALUES ($val_part)";
            log_message('critical', "SQL-WAS CALLED user =>". $sqlQ );
            $query = $this->db->query($sqlQ);
            if(!$query){
                $insert_id=-1;
            }
            //$db->affectedRows();
           // $insert_id = $this->db->insert_id();
           // $insert_id = $this->db->getInsertID();
          //  $this->db->insert($insertTable,$inserDataArray);
          //  $insert_id = $this->db->insert_id();
     //   } catch (Exception $e) {
            //echo 'Caught exception: ',  $e->getMessage(), "\n";
       //     log_message('critical', "Caught exception: => ".$e->getMessage() );
       // }

      
        return $insert_id;
    }
    public function update_db($UupdateDataArray,$whereAray, $updateTable)
    {
        $ic = 0;
        $UP_part = '';
        $val_part = '';
        $sqlQ = '';
        $fields = array_keys($UupdateDataArray);
        foreach ($fields as $field) {
            $UP_part .= $ic > 0 ? ',' : '';
            $UP_part .= $field."='" . $UupdateDataArray[$field] . "'";
            $ic++;
        }

        $ic = 0;
        $fields = array_keys($whereAray);
        foreach ($fields as $field) {
            $val_part .= $ic > 0 ? ' AND ' : ' WHERE ';
            $val_part .= $field."='" . $whereAray[$field] . "'";
            $ic++;
        }

        $sqlQ = "UPDATE $updateTable SET $UP_part  $val_part"; 
        $query = $this->db->query($sqlQ);
        return 0;
    }

public function select_db($selectItems,$whereAray, $updateTable)
{
        $ic = 0;
        $UP_part = '';
        $val_part = '';
        $sqlQ = '';
        $status = 0;
        $error_msg = '';
        $record_count = -1;
        $result =[];
        //$fields = array_keys($UupdateDataArray);
        foreach ($selectItems as $item) {
            $UP_part .= $ic > 0 ? ',' : '';
            $UP_part .= $item;
            $ic++;
        }

        $ic = 0;
        $fields = array_keys($whereAray);
        foreach ($fields as $field) {
            $val_part .= $ic > 0 ? ' AND ' : ' WHERE ';
            $val_part .= $field."='" . $whereAray[$field] . "'";
            $ic++;
        }
    $sqlQ = "SELECT $UP_part FROM $updateTable  $val_part";      
    log_message('critical', "SQL:: ". $sqlQ );
    $query = $this->db->query($sqlQ);
    if($query){
    $status = 1;
    $result = $query->getResultArray();
    $record_count = count($result);
    }
    else{
        $status = -1;
        $error_msg = "Select falied";
    }
    return [
        'status'=>$status,
        'error_msg'=> $error_msg,
        'record_count' => $record_count,
        'result' => $result
    ];
}

    public function getIPAddress()
    {
        //whether ip is from the share internet
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
        //whether ip is from the proxy
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        //whether ip is from the remote address
        else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
}
