<?php

class UserModel extends CI_Model
{
    private $_table = "tb_user";

    public function doLogin(){
		$post = $this->input->post();

        // cari user berdasarkan email dan username
        $this->db->where('username', $post["username"]);
        $user = $this->db->get($this->_table)->row();

        // jika user terdaftar
        if($user){
            // periksa password-nya
            
            $isPasswordTrue = md5($post["password"])==$user->password;
            // jika password benar dan dia admin
            if($isPasswordTrue){ 
                // login sukses yay!
                $this->session->set_userdata(['user_logged' => $user]);
                return true;
            }
        }
        
        // login gagal
		return false;
    }

    public function getUsers(){
        $this->db->where('level', 'pemilik');
        return  $user = $this->db->get($this->_table)->result();
    }

    public function getUsersById($id_user){
        $this->db->where('id_user', $id_user);
        return  $user = $this->db->get($this->_table)->row();
    }

    public function isNotLogin(){
        return $this->session->userdata('user_logged') === null;
    
    }

    public function isAdmin(){
        return $this->session->userdata('user_logged')->level === 'admin';
    }

    public function isPemilik(){
        return $this->session->userdata('user_logged')->level === 'pemilik';
    }

    public function save($foto_user)
    {
        $post = $this->input->post();
        $this->nama = $post["nama"];
        $this->username = $post["username"];
        $this->password = md5($post["password"]);
        $this->alamat_user = $post["alamat"];
        if($post['kelurahan']!=='luar'){
            $kelurahan = $post['kelurahan'];
            $kecamatan = $this->KelurahanModel->getKecamatanByKelurahan($post['kelurahan']);
            $kota = 'Kota Magelang';
        } else {
            $kelurahan = 'Luar Kota Magelang';
            $kecamatan = 'Luar Kota Magelang';
            $kota = 'Luar Kota Magelang';
        }
        $this->kelurahan_user = $kelurahan;
        $this->kecamatan_user = $kecamatan;
        $this->kota_user = $kota;
        $this->telp_user = $post["telp"];
        $this->npwp = $post["npwp"];
        $this->foto_user = $foto_user;
        $this->level = 'pemilik';
        
        return $this->db->insert($this->_table, $this);
    }

    public function update($id_pemilik,$foto_user)
    {
        $post = $this->input->post();
        $this->nama = $post["nama"];
        $this->alamat_user = $post["alamat"];
        if($post['kelurahan']!=='luar'){
            $kelurahan = $post['kelurahan'];
            $kecamatan = $this->KelurahanModel->getKecamatanByKelurahan($post['kelurahan']);
            $kota = 'Kota Magelang';
        } else {
            $kelurahan = 'Luar Kota Magelang';
            $kecamatan = 'Luar Kota Magelang';
            $kota = 'Luar Kota Magelang';
        }
        $this->kelurahan_user = $kelurahan;
        $this->kecamatan_user = $kecamatan;
        $this->kota_user = $kota;
        $this->telp_user = $post["telp"];
        $this->npwp = $post["npwp"];
        $this->foto_user = $foto_user;
        return $this->db->update($this->_table, $this, array('id_user' => $id_pemilik));
    }

    public function resetPassword($id_user)
    {
        $post = $this->input->post();
        $this->password = md5($post["password"]);
        return $this->db->update($this->_table, $this, array('id_user' => $id_user));
    }

    public function delete($id_pemilik)
    {
        return $this->db->delete($this->_table, array("id_user" => $id_pemilik));
    }
    


}