<?php 
class Model_users extends CI_model{
	function users(){
		return $this->db->query("SELECT A.*,B.*,D.nama_lengkap AS nama_dosen,D.photo AS photodosen,D.email AS emaildosen FROM tb_pengguna A 
					LEFT JOIN tb_mahasiswa B ON B.mahasiswa_id=A.user_id
					LEFT JOIN tb_dosen D ON D.dosen_id=A.user_id
					ORDER BY A.username");
	}

	function users_tambah(){
        $config['upload_path'] = 'asset/foto_user/';
        $config['allowed_types'] = 'gif|jpg|png|JPG|JPEG';
        $config['max_size'] = '10000'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('fileku');
        $hasil=$this->upload->data();
            if ($hasil['file_name']==''){
                    $datadb = array('user_id'=>$this->db->escape_str($this->input->post('id')),
									'username'=>$this->db->escape_str($this->input->post('username')),
									'pengguna_id'=>$this->db->escape_str($this->input->post('a')),
                                    'password'=>hash("sha512", md5(123)),
                                    'nama_lengkap'=>$this->db->escape_str($this->input->post('b')),
                                    'level'=>$this->db->escape_str($this->input->post('d')),
                                    'blokir'=>'N',
									'tanggal_input'=>date('Y-m-d H:i:s'),
                                    'username_input'=>$this->session->username,
                                    'id_session'=>md5(123));
            }else{
                    $datadb = array('username'=>$this->db->escape_str($this->input->post('username')),
									'user_id'=>$this->db->escape_str($this->input->post('id')),
                                    'password'=>hash("sha512", md5(123)),
									'pengguna_id'=>$this->db->escape_str($this->input->post('a')),
                                    'nama_lengkap'=>$this->db->escape_str($this->input->post('b')),
                                    'foto'=>$hasil['file_name'],
                                    'level'=>$this->db->escape_str($this->input->post('d')),
                                    'blokir'=>'N',
									'id_session'=>md5(123),
									'username_input'=>$this->session->username,
                                    'tanggal_input'=>date('Y-m-d H:i:s'));
            }
        $this->db->insert('tb_pengguna',$datadb);
    }
	function pegawai_tambah(){
		$datadb = array('nama_pegawai'=>$this->db->escape_str($this->input->post('b')),
						'nip'=>$this->db->escape_str($this->input->post('a')),
						'jk'=>$this->db->escape_str($this->input->post('c')),
						'jabatan'=>$this->db->escape_str($this->input->post('d')),
						'blokir'=>'N',
						'username_input'=>$this->session->username,
						'tanggal_input'=>date('Y-m-d H:i:s'));
            
        $this->db->insert('pegawai',$datadb);
    }

    function users_edit($id){
        return $this->db->query("SELECT A.*,B.* FROM tb_pengguna A 
					LEFT JOIN tb_mahasiswa B ON B.mahasiswa_id=A.user_id where A.user_id='$id'");
    }

    function users_update(){
                    $datadb = array('username'=>$this->db->escape_str($this->input->post('username')),
                                    'password'=>hash("sha512", md5($this->input->post('password'))));
           			$this->db->where('user_id',$this->input->post('iduser'));
			$this->db->update('tb_pengguna',$datadb);
    }
	 function simpan_reset_mahasiswa($id){
       
		$datadb = array('password'=>hash("sha512", md5('123')));
			$this->db->where('user_id',$id);
			$this->db->update('tb_pengguna',$datadb);
    }
	function simpan_reset_admin($id){
       
		$datadb = array('username'=>'admin',
						'password'=>hash("sha512", md5('admin')));
			$this->db->where('user_id',$id);
			$this->db->update('tb_pengguna',$datadb);
    }
	function simpan_blokir_admin($id){
       	$datadb = array('blokir'=>'Y');
			$this->db->where('user_id',$id);
			$this->db->update('tb_pengguna',$datadb);
    }
	function simpan_aktif_admin($id){
       	$datadb = array('blokir'=>'N');
			$this->db->where('user_id',$id);
			$this->db->update('tb_pengguna',$datadb);
    }
    function users_delete($id){
        return $this->db->query("DELETE FROM users where username='$id'");
    }

}