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

        class karyawan_model extends CI_Model {

  var $table = 'akun';
  var $order = array('id' => 'desc'); // default order 
            public function __construct() {
                parent::__construct();
                $this->load->database();
            }
function ceklogin(){
    $username=$this->input->post('username');
    $password=md5($this->input->post('password'));
    $query=$this->db->query("select * from karyawan WHERE username = '$username' AND password = '$password'");
       if ($query->num_rows() > 0) 
        foreach ($query->result() as $data) {
          $data=array('LOGIN'=>TRUE,'USERNAME'=>$data->username,'LEVEL'=>$data->level,'ID_KAR'=>$data->id_kar);
          $this->session->set_userdata($data);  
          echo json_encode(array("status" => TRUE));  
        
      } else {
       echo json_encode(array("status" => FALSE));
      }     
  }
  function cek($id_kar,$pass){
               $this->db->select('*');
              $this->db->where('id_kar', $id_kar);
              $this->db->where('password', $pass);
              $this->db->from('karyawan');
              $query = $this->db->get();
                  if($query->num_rows() > 0){
                  return 1;
                  }else{    
                  return 0;   
                  }  
  }
function all() {
return $query=$this->db->get('karyawan')->result();
} 
function mykaryawan() {
  $id_kar=$this->session->userdata('ID_KAR'); 
 $this->db->from('karyawan');
                $this->db->where('id_kar',$id_kar);
                $query = $this->db->get();
                return $query->row();
} 
public function save($data)
{
  $this->db->insert('karyawan', $data);
}

           public function get_by_id($id_kar)
  {
                $this->db->from('karyawan');
                $this->db->where('id_kar',$id_kar);
                $query = $this->db->get();
                return $query->row();
  }
  public function update($where, $data)
  {
    $this->db->update('karyawan', $data, $where);
    return $this->db->affected_rows();
  }
            public function delete($id_kar)
  {
    $this->db->where('id_kar', $id_kar);
    $this->db->delete('karyawan');
  }
 
public function checkusername($username)
{  

   $this->db->select('username');
   $this->db->where('username', $username);
   $this->db->from('karyawan');
   $query = $this->db->get();

   if( $query->num_rows() > 0 ){     return 1;    }
   else{    return 0;   }               
} 
public function checkbenerpass($id_kar){
$this->db->where('id_kar', $id_kar);
$this->db->from('karyawan');
$query = $this->db->get();
return $query->row();   
}
public function checkpassword($password)
{  

   $this->db->select('password');
   $this->db->where('password', $password);
   $this->db->from('karyawan');
   $query = $this->db->get();

   if( $query->num_rows() > 0 ){     return 1;    }
   else{    return 0;   }               
} 
        }       
        