<?php 

class Home 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_barang'); 
		$this->load->model('m_grafik'); 
		$this->load->library('session');
 		$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(){		
		$username = $this->session->userdata("username");  
		$this->db->where('username',$username);
		$query = $this->db->get('tb_admin');
		$row = $query->row();
		$data['id_admin']= $row->id;
		$data['nama_admin']= $row->nama_admin;
		$data['id_kantor']= $row->id_kantor;
		$id_admin = $row->id;
		$level = $row->level;
		$id_kantor = $row->id_kantor;
		
		$data['barang_expired'] = $this->m_barang->barang_expired($id_admin,$id_kantor,$level); 
		$data['barang_terjual'] = $this->m_barang->data_barang_jual($id_admin,$id_kantor,$level);

		$data['kontak'] = $this->m_kontak->kontak();
		$data['admin'] = $this->m_admin->data_admin(); 
		$data['admin_login'] = $this->m_admin->data_admin_login(); 
		
		$data['grafik_bulan']=$this->m_grafik->grafik_bulan();
		$data['grafik_tahun']=$this->m_grafik->grafik_tahun();

		 $data['graphik'] = $this->m_grafik->graph();
		 $data['graphik2'] = $this->m_grafik->graph2(); 
		$this->load->view('master/header',$data); 
		$this->load->view('master/home',$data);
		$this->load->view('master/footer',$data); 
	}
	
	public function code_ubah() {
    	
		$this->db->where('id',$this->input->post('id'));
		if ($_FILES['berkas']['error'] <> 4) 
		{
				    	$query = $this->db->get('tb_kontak');
					    $row = $query->row();
					 
					    unlink("./images/kontak/$row->logo");

					  

				    	$config['upload_path']          = './images/kontak/';
						$config['allowed_types']        = 'gif|jpg|png';
					 	$config['file_name']  = url_title($this->input->post('berkas',TRUE));
					    $config['remove_space'] = TRUE;

						$this->load->library('upload', $config);

						if ( ! $this->upload->do_upload('berkas')){
						 $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> Data Gagal Diubah  !</strong>
										</div> ');
					    			redirect('administrator/kontak/index');
						}else{
							$data=array(

							      		'nama_toko'=>  $this->input->post('nama_toko'),
							      		'fb'=>  $this->input->post('fb'),
							      		'twiter'=>  $this->input->post('twiter'),
							      		'propinsi'=>  $this->input->post('propinsi'),
							      		'kabupaten'=>  $this->input->post('kabupaten'),
							      		'key_api_rajaongkir'=>  $this->input->post('twikey_api_rajaongkirter'),
							      		'alamat'=>  $this->input->post('alamat'),
							      		'no_hp'=>  $this->input->post('no_hp'),
							      		'email'=>  $this->input->post('email'),
							      		'ig'=>  $this->input->post('ig'),
							      		'website'=>  $this->input->post('website'),
							      		'pajak'=>  $this->input->post('pajak'),
							      		'logo_tampil'=>  $this->input->post('logo_tampil'),
							      	 
							      		 'logo'=>  $this->upload->file_name

						            );
							      	$this->db->where('id',$this->input->post('id'));
				            		$this->db->update('tb_kontak',$data);
				            
							$data = array('upload_data' => $this->upload->data());
							 $this->session->set_flashdata('msg','<div class="alert alert-info alert-dismissible">
										  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
										  <strong> Data Berhasil Diubah  !</strong>
										</div> ');
					    			redirect('administrator/kontak/index');
						}

		}else{

			$data=array(

							      		'nama_toko'=>  $this->input->post('nama_toko'),
							      		'fb'=>  $this->input->post('fb'),
							      		'twiter'=>  $this->input->post('twiter'),
							      		'alamat'=>  $this->input->post('alamat'),
							      		'propinsi'=>  $this->input->post('propinsi'),
							      		'kabupaten'=>  $this->input->post('kabupaten'),
							      		'key_api_rajaongkir'=>  $this->input->post('twikey_api_rajaongkirter'),
							      		'no_hp'=>  $this->input->post('no_hp'),
							      		'email'=>  $this->input->post('email'),
							      		'ig'=>  $this->input->post('ig'),
							      		'pajak'=>  $this->input->post('pajak'),
							      		'logo_tampil'=>  $this->input->post('logo_tampil'),
							      		'website'=>  $this->input->post('website')
						            );
							      	$this->db->where('id',$this->input->post('id'));
				            		$this->db->update('tb_kontak',$data);
				            
						 
									echo $this->session->set_flashdata('msg','success');

					    			redirect('administrator/kontak/index');
		}	
	} 
	 
}