<?php

class barang extends Controller{
    public function index(){
        $data['judul']='Daftar barang';
        $data['brg']=$this->model('barang_model')->getbarangAll();
        $this->view('templates/header', $data);
        $this->view('barang/index', $data);
        $this->view('templates/footer');
    }
    public function detail($IDBRG){
        $data['judul']='Detail barang';
        $data['brg']=$this->model('barang_model')->getbarangbyid($IDBRG);
        $this->view('templates/header', $data);
        $this->view('barang/detail', $data);
        $this->view('templates/footer');
    }
    public function tambah(){
        if($this->model('barang_model')->tambahdatabrg($_POST)>0){
            Flashnotif::setFlash('berhasil','ditambah','alert alert-success');
            header('Location:'. BASEURL .'/barang');
            exit;
        }else{
            Flashnotif::setFlash('gagal','ditambah','alert alert-danger');
            header('Location:'. BASEURL .'/barang');
            exit;
        }
    }
    public function hapus($IDBRG){
        if($this->model('barang_model')->hapusdatabrg($IDBRG)>0){
            Flashnotif::setFlash('berhasil','dihapus','alert alert-success');
            header('Location:'. BASEURL .'/barang');
            exit;
        }else{
            Flashnotif::setFlash('gagal','dihapus','alert alert-danger');
            header('Location:'. BASEURL .'/barang');
            exit;
        }
    }
    public function editbrg(){
    echo json_encode($this->model('barang_model')->getbarangbyid($_POST['IDBRG']));
    }

    public function ubah() {
        if ($this->model('barang_model')->ubahdatabrg($_POST) > 0) {
            Flashnotif::setFlash('berhasil', 'diubah', 'alert alert-success');
            header('Location: ' . BASEURL . '/barang');
            exit;
        } else {
            Flashnotif::setFlash('gagal', 'diubah', 'alert alert-danger');
            header('Location: ' . BASEURL . '/barang');
            exit;
        }
    }
    public function cari(){
        $data['judul']='Daftar barang';
        $data['brg']=$this->model('barang_model')->caridatabrg();
        $this->view('templates/header', $data);
        $this->view('barang/index', $data);
        $this->view('templates/footer');
    }
    
    
}    
