<?php

/**
 * 
 */
class Data_barang extends CI_Controller{
	public function __construct(){
		parent::__construct();

		if($this->session->userdata('role_id') != '1'){
			$this->session->set_flashdata('pesan','<div class="alert alert-danger alert-dismissible fade show" role="alert">
				  Anda Belum Login!
				  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
				  <span aria-hidden="true">&times; </span>
				  </button>
				</div>');
			redirect('auth/login');
		}
	}
	public function index()
	{
		$cari = $this->input->post('search');
		if($cari){
			$pencarian = $this->model_kategori->cari($cari);
		}else{
			$pencarian = $this->model_barang->tampil_data();
		}
		$data = [
			'str'=>'barang',
			'barang' => $pencarian->result()]; 
		$this->load->view('templates_admin/header');
		$this->load->view('templates_admin/sidebar',$data);
		$this->load->view('admin/data_barang', $data);
		$this->load->view('templates_admin/footer');
	}

	public function tambah_aksi()
	{
		$nama_brg	= $this->input->post('nama_brg');
		$keterangan	= $this->input->post('keterangan');
		$kategori	= $this->input->post('kategori');
		$laba		= $this->input->post('laba');
		$harga		= $this->input->post('harga');
		$stok		= $this->input->post('stok');
		$gambar		= $_FILES['gambar']['name'];
		if ($gambar	!= null){
			$config ['upload_path'] = './uploads/';
			$config ['allowed_types'] = 'jpg|jpeg|png';

			$this->load->library('upload', $config);
			if (!$this->upload->do_upload('gambar')){
				echo "Gambar Gagal diupload!";
			}else {
				$gambar = $this->upload->data('file_name');
			}

		}

		$data = array(
			'nama_brg' 	=> $nama_brg,
			'keterangan'=> $keterangan,
			'kategori' 	=> $kategori,
			'keuntungan'=> $laba,
			'harga' 	=> $harga,
			'stok' 		=> $stok,
			'gambar' 	=> $gambar
		);
		$this->model_barang->tambah_barang($data, 'tb_barang');
		redirect('admin/data_barang/index');
	}
	public function detail($id)
	{
		$where = array('id_brg' =>$id);
		$data = [
			'str'=>'barang',
			'barang' => $this->model_barang->detail_barang($where,'tb_barang')
		];
		$this->load->view('templates_admin/header');
		$this->load->view('templates_admin/sidebar',$data);
		$this->load->view('admin/detail_barang', $data);
		$this->load->view('templates_admin/footer');
		// redirect('admin/data_barang/index');
	}
	public function edit($id)
	{
		$where = array('id_brg' =>$id);
		$data=[
			'str'=>'barang',
			'barang' => $this->model_barang->edit_barang($where, 'tb_barang')->result()
		];
		$this->load->view('templates_admin/header');
		$this->load->view('templates_admin/sidebar',$data);
		$this->load->view('admin/edit_barang', $data);
		$this->load->view('templates_admin/footer');
	}
	public function update(){
		$id 		= $this->input->post('id_brg');
		$nama_brg 	= $this->input->post('nama_brg');
		$keterangan = $this->input->post('keterangan');
		$kategori 	= $this->input->post('kategori');
		$laba 		= $this->input->post('laba');
		$harga 		= $this->input->post('harga');
		$stok 		= $this->input->post('stok');

		$data = array(

			 'nama_brg'		=> $nama_brg,
			 'keterangan'	=> $keterangan,
			 'kategori'		=> $kategori,
			 'keuntungan'	=> $laba,
			 'harga'		=> $harga,
			 'stok'			=> $stok
		);

		$where =array(
			'id_brg' => $id
		);

		$this->model_barang->update_data($where,$data, 'tb_barang');
		redirect('admin/data_barang/index');
	}

	public function hapus ($id)
	{
		$unling = $this->model_barang->get1($id);
		if(file_exists('./uploads/'.$unling['gambar']))
		{
			unlink('./uploads/'.$unling['gambar']);
			$this->model_barang->hapus_data(['id_brg' => $id], 'tb_barang');
		}
		redirect('admin/data_barang/index');
	}
}
