<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Admin_model extends CI_Model
{
    public function get($table, $data = null, $where = null)
    {
        if ($data != null) {
            return $this->db->get_where($table, $data)->row_array();
        } else {
            return $this->db->get_where($table, $where)->result_array();
        }
    }

    public function getOrder($table, $data = null, $where = null, $oder_by = null, $order = 'ASC')
    {
        if ($oder_by != null) {
            $this->db->order_by($oder_by . ' ' . $order);
        }

        if ($data != null) {
            return $this->db->get_where($table, $data)->row_array();
        } else {
            return $this->db->get_where($table, $where)->result_array();
        }
    }

    public function update($table, $pk, $id, $data)
    {
        $this->db->where($pk, $id);
        return $this->db->update($table, $data);
    }

    public function update2($table, $pk1, $id1, $pk2, $id2, $data)
    {
        $this->db->where($pk1, $id1);
        $this->db->where($pk2, $id2);
        return $this->db->update($table, $data);
    }

    public function countConditions($table, array $conditions = [])
    {   
        foreach ($conditions as $k => $v) 
        {
            if (is_array($v)) {
                if (!$v) {
                    continue; // avoid breaking built query
                }
                $this->db->where_in($k, $v);
            } elseif (endsWith($k, ' LIKE')) {
                $this->db->like(substr($k, 0, -5), $v);
            } else {
                $this->db->where($k, $v);
                //log_message ('error', $k . ' ' . $v);
            }
        }

        $result = $this->db->get($table)->result_array();
        $count = 0;
        foreach($result as $row)
        {
            $count=$count+1;
        }
        
        return $count;
    }

    public function insert($table, $data, $batch = false)
    {
        return $batch ? $this->db->insert_batch($table, $data) : $this->db->insert($table, $data);
    }

    public function delete($table, $pk, $id)
    {
        return $this->db->delete($table, [$pk => $id]);
    }

    public function getUsers($id)
    {
        //$this->db->where('id_user !=', $id);
        return $this->db->get('tbl_user')->result_array();
    }    
    
    public function getMax($table, $field, $kode = null)
    {
        $this->db->select_max($field);
        if ($kode != null) {
            $this->db->like($field, $kode, 'after');
        }
        return $this->db->get($table)->row_array()[$field];
    }

    public function count($table)
    {
        return $this->db->count_all($table);
    }

    public function sum($table, $field)
    {
        $this->db->select_sum($field);
        return $this->db->get($table)->row_array()[$field];
    }

    public function min($table, $field, $min)
    {
        $field = $field . ' <=';
        $this->db->where($field, $min);
        return $this->db->get($table)->result_array();
    }

    public function chartClientes1($bulan)
    {

        return count($this->db->get('tbl_clientes')->result_array());
    }

    public function chartClientes2($bulan)
    {
        
        return count($this->db->get('tbl_clientes')->result_array());
    }


    function getLastLoginInfo($userId)
    {
        $this->db->select('createdDtm');
        $this->db->where('userId', $userId);
        $this->db->order_by('id', 'DESC');
        $this->db->limit(1);

        return $this->db->get('tbl_last_login')->row_array();
    }

    

}
