<?php 

class Admin extends CI_Controller{

	function __construct(){
		parent::__construct();
	 	$this->load->model('m_kontak');/* sesuaikan dengan nama file pada model */
		$this->load->model('m_login'); 
		$this->load->model('m_admin'); 
		$this->load->model('m_kantor'); 
		$this->load->library('session');
		$this->load->library('upload');
		 
		$this->load->helper(array('form', 'url','tanggal'));
		if($this->session->userdata('status') != "loginbro"  and empty($this->session->userdata('email')) ){
			$this->session->set_flashdata('msg','<div class="alert alert-danger alert-dismissible">
						<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
						<strong> Admin Bukan Admin </strong>Ulangi Kembali
						</div> ');

			redirect(base_url("")); 
			
		}
	 
	 
	}

	 
	function index(){		
		$data['title1'] = 'Data Admin'; 	
		$data['title2'] = 'Ubah Data'; 	
		$data['title3'] = 'Tambah Data';
		$data['kontak'] = $this->m_kontak->kontak();
		$data['kantor'] = $this->m_kantor->kantor();
		$data['admin'] = $this->m_admin->data_admin(); 
		$data['admin_login'] = $this->m_admin->data_admin_login();  
		 
	
		$this->load->view('master/header',$data); 
		$this->load->view('master/admin/v_admin');
		$this->load->view('master/footer',$data); 
	}
	function profile(){		
		$data['title1'] = 'Data Profil'; 	
		$data['title2'] = 'Profil Data'; 	
		$data['title3'] = 'Profil  Data Admin';
		$data['kontak'] = $this->m_kontak->kontak();
		$data['kantor'] = $this->m_kantor->kantor();
		$data['admin'] = $this->m_admin->data_admin(); 
		$data['admin_login'] = $this->m_admin->data_admin_login();  
		 
	
		$this->load->view('master/header',$data); 
		$this->load->view('master/admin/v_profil');
		$this->load->view('master/footer',$data); 
	}
	function v_ubah_pass(){		
		$data['title1'] = 'Ubah Password Profil'; 	
		$data['title2'] = 'Profil Data'; 	
		$data['title3'] = 'Ubah Password  Admin';
		$data['kontak'] = $this->m_kontak->kontak();
		$data['kantor'] = $this->m_kantor->kantor();
		$data['admin'] = $this->m_admin->data_admin(); 
		$data['admin_login'] = $this->m_admin->data_admin_login();  
		 
	
		$this->load->view('master/header',$data); 
		$this->load->view('master/admin/v_ubah_pass');
		$this->load->view('master/footer',$data); 
	}
	function tampil($id){		
		$id=$this->uri->segment(4);
		$data['data_admin']=$this->db->query("SELECT * FROM tb_admin WHERE id='$id'")->result();
		
		$data['kontak'] = $this->m_kontak->kontak();
		$data['log'] = $this->m_admin->data_log();
		$data['admin'] = $this->m_admin->data_admin(); 
		$this->load->view('master/header',$data);
		$this->load->view('master/admin/tampil',$data);
		$this->load->view('master/footer_tabel',$data);
	}
	public function code_hapus(){
		$id = $this->input->post('id');  
		$this->db->where('id',$id);
	    $query = $this->db->get('tb_admin');
	    $row = $query->row();
	 
	    unlink("./images/admin/$row->foto");

	    $this->db->delete('tb_admin', array('id' => $id)); 
	  	$this->session->set_flashdata('msg','success-hapus');
	    redirect('admin');
		
	}
	 
	public function code_tambah()
	{	 
		    $config['upload_path'] = './images/admin/'; //path folder
	        $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
	        $config['max_size']      = 2024;
	        $config['encrypt_name'] = TRUE; //Enkripsi nama yang terupload
	        $this->upload->initialize($config);
	        if(!empty($_FILES['foto']['name'])){
	        	
	           if ($this->upload->do_upload('foto')){
	        		$gbr = $this->upload->data();
	        		
	        		$gambar=$gbr['file_name'];
	        	}
	        	
	        	$data=array(
	        		'id'=>  null,
	        		'username'=> $this->input->post('email'),
	        		'email'=>  $this->input->post('email'),
	        		'nama_admin'=>  $this->input->post('nama_admin'),
	        		'level'=>  $this->input->post('level'), 
	        		'id_kantor'=>  $this->input->post('id_kantor'), 
	        		'password'=> get_hash($this->input->post('password')),
	        		'foto'=> $gambar
	        	);

	        	$this->db->insert('tb_admin',$data);
	        	$this->session->set_flashdata('msg','success');
	        	redirect('admin');
	        }else{
	        	
	        	$this->session->set_flashdata('msg','error-img');
	        	redirect('admin');
	        } 

        
        
    }  
     
	public function code_ubah() {
      
       	$id = $this->input->post('id');
        
        $config['upload_path'] = './images/admin/'; //path folder
	        $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
	        $config['max_size']      = 2024;
	        $config['encrypt_name'] = TRUE; //Enkripsi nama yang terupload
	        $this->upload->initialize($config);
	        if(!empty($_FILES['foto']['name'])){
	        	
	           if ($this->upload->do_upload('foto')){
	        		$gbr = $this->upload->data();
	        		
	        		$gambar=$gbr['file_name'];
	        	}
	        	
	        	$data=array(
	        		 
	        		'nama_admin'=>  $this->input->post('nama_admin'),
	        		'level'=>  $this->input->post('level'), 
	        		'id_kantor'=>  $this->input->post('id_kantor'), 
	        		 
	        		'foto'=> $gambar
	        	);

	        	$this->db->where('id',$this->input->post('id'));
                $this->db->update('tb_admin',$data);
	        	$this->session->set_flashdata('msg','success');
	        	redirect('admin');
	        }else{
	        	
	        	$data=array(
	        	 
	                'nama_admin'=>  $this->input->post('nama_admin'),
	        		'level'=>  $this->input->post('level'), 
	        		'id_kantor'=>  $this->input->post('id_kantor'), 
	        		 
	        		 
	        	);

	        	$this->db->where('id',$this->input->post('id'));
                $this->db->update('tb_admin',$data);
	        	redirect('admin');
	        } 
 
	
	}

	public function code_profil() {
      
       	$id = $this->input->post('id');
        
        $config['upload_path'] = './images/admin/'; //path folder
	        $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
	        $config['max_size']      = 2024;
	        $config['encrypt_name'] = TRUE; //Enkripsi nama yang terupload
	        $this->upload->initialize($config);
	        if(!empty($_FILES['foto']['name'])){
	        	
	           if ($this->upload->do_upload('foto')){
	        		$gbr = $this->upload->data();
	        		
	        		$gambar=$gbr['file_name'];
	        	}
	        	
	        	$data=array(
	        		 
	        		 
	        		'level'=>  $this->input->post('level'), 
	        		'id_kantor'=>  $this->input->post('id_kantor'), 
	        		 
	        		'foto'=> $gambar
	        	);

	        	$this->db->where('id',$this->input->post('id'));
                $this->db->update('tb_admin',$data);
	        	$this->session->set_flashdata('msg','success');
	        	redirect('admin/profile');
	        }else{
	        	
	        	$data=array(
	        	 
	                 
	        		'level'=>  $this->input->post('level'), 
	        		'id_kantor'=>  $this->input->post('id_kantor'), 
	        		 
	        	);

	        	$this->db->where('id',$this->input->post('id'));
                $this->db->update('tb_admin',$data);
                $this->session->set_flashdata('msg','success');
	        	redirect('admin/profile');
	        } 
 
	
	}
	public function form_ubah_password(){
		$id=$this->uri->segment(4);
		$data['data_admin']=$this->db->query("SELECT * FROM tb_admin WHERE id='$id'")->result();
		
		$data['kontak'] = $this->m_kontak->kontak();
		$data['admin'] = $this->m_admin->data_admin(); 
		$this->load->view('master/header',$data);
		$this->load->view('master/admin/ubah_pass',$data);
		$this->load->view('master/footer_tabel',$data);


			 
	}
	public function code_ubah_pass() {
		$id = $this->input->post('id'); 
   	 	$password = $this->input->post('password');  
   	 	$old_password = $this->input->post('old_password');

   	 	  
   	 	$password_new1 = $this->input->post('new_password1');  
   	 	$password_new2 = $this->input->post('new_password2');  
      	if(hash_verified($this->input->post('old_password'),$password))
      		{
      			if($password_new1 == $password_new2)	
      			{
      					$data=array(

			      	 
						'password'=>  get_hash($password_new1)
			        	
			        );
			      	$this->db->where('id',$id);
					$this->db->update('tb_admin',$data);

			         $this->session->set_flashdata('msg','success');
	        	redirect('admin/profile');
      			}else{
      				 $this->session->set_flashdata('msg','success');
	        	redirect('admin/profile');
      			}

      			
      		}else {
      			   $this->session->set_flashdata('msg','success');
	        	redirect('admin/profile');
      		}	
			      

			   
	}
	public function code_reset_password() {
		$id = $this->input->post('id'); 

		$password_new = $this->input->post('new_password');  

		$data=array(

			'password'=>  get_hash($password_new)

		);
		$this->db->where('id',$id);
		$this->db->update('tb_admin',$data);

		$this->session->set_flashdata('msg','success');
		redirect('admin');
 
			   
	}
	 
}