<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Bahan_habis_pakai extends MY_Controller {

    protected $template = "app";
    protected $module = "bahan_habis_pakai";

    public function __construct() {
        parent::__construct();
        $this->load->model('m_bahan_habis_pakai', 'model');
    }

    public function index() {
        $this->data['halaman'] = "Daftar Lab Pakai Habis";
        $this->data['sub_halaman'] = '';
        $this->data['data']= $this->model->get_data();

        /*//
        $data_keys = array_keys($this->data['data'][0]);
        $this->data['data_keys'] =  array();
        foreach($data_keys as $r){
          $this->data['data_keys'][] = ucwords(str_replace('_',' ',$r));
        }
        $x=0;
        foreach($this->data['data_keys'] as $r){
          if($r == 'Merk') $this->data['data_keys'][$x]='Merek';
          $x++;
        }
        */

        //var_dump($this->data['data_keys']); die();
        $this->render('index');
    }

    public function create_edit($id=null) {
      $this->data['halaman'] = is_null($id) ? "Tambah Data Lab Pakai Habis" : "Edit Data Lab Pakai Habis";
      if(!is_null($id)){
        $this->data['edit'] = $this->model->get_detail($id);
        $this->data['total_pengambilan'] =  $this->model->total_pengambilan($id);
        //var_dump($this->data['total_pengambilan']);
      }else {
        $this->data['sub_halaman'] = '';
      }

      $data = $this->input->post();

      if($data){
        if(is_null($id)){
          $data['sisa_stok'] = $data['jumlah_stok'];
        }
        $data['last_modified_user'] = $this->id_user;

        $param_keys = array_keys($data);

        if($id) $status = $this->model->set_data($data, $param_keys, $id);
        else $status = $this->model->set_data($data, $param_keys);

        if($status){
            if(is_null($id))$this->session->set_flashdata('msg', 'data bahan habis pakai berhasil ditambahkan');
            else $this->session->set_flashdata('msg', 'data bahan habis pakai berhasil diubah');
        }else{
            $this->session->set_flashdata('msg', 'data bahan habis pakai gagal ditambahkan');
        }
        redirect('/inventaris/bahan_habis_pakai/index');
      }
      $this->render('create_edit');
    }

    function hapus($id){
      $data['is_active'] = '0';
      $data['last_modified_user'] = $this->id_user;

      $param_keys = array_keys($data);
      $status = $this->model->set_data($data, $param_keys, $id);

      if($status){
          $this->session->set_flashdata('msg', 'data bahan habis pakai berhasil dihapus');
      }else{
          $this->session->set_flashdata('msg', 'data bahan habis pakai gagal dihapus');
      }
      redirect('/inventaris/bahan_habis_pakai/index');
    }
}
