<?php

class Data_barang extends CI_Controller
{
   public function index()
   {
      $data['title'] = 'Data Barang';
      $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
      $data['barang'] = $this->Model_barang->tampil_admin();
      $this->load->view('admin_templates/header', $data);
      $this->load->view('admin_templates/sidebar');
      $this->load->view('admin_templates/topbar', $data);
      $this->load->view('admin/data_barang', $data);
      $this->load->view('admin_templates/footer');
   }

   public function tambah()
   {
      $nama_blok = $this->input->post('nama_blok');
      $latlng = $this->input->post('latlng');
      $ket = $this->input->post('ket');
      $harga = $this->input->post('harga');
      $gambar = $_FILES['gambar']['name'];
      if ($gambar) {
            $config['allowed_types'] = 'gif|jpg|png';
            $config['upload_path'] = './public/img/upload/';

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

            if ($this->upload->do_upload('gambar')) {
               $new_image = $this->upload->data('file_name');
                  $data = [
                  'blok' => $nama_blok,
                  'harga' => $harga,
                  'latlng' => $latlng,
                  'keterangan' => $ket,
                  'gambar' => $new_image
                  ];

                  $this->Model_barang->insert($data, 'area_camping');
                  redirect('data_barang');
            } else {
               echo $this->upload->display_errors();
            }

      } else {
         $data = [
         'blok' => $nama_blok,
         'harga' => $harga,
         'latlng' => $latlng,
         'keterangan' => $ket
         ];

         $this->Model_barang->insert($data, 'area_camping');
         redirect('data_barang');
      }

      
   }

   public function hapus($id)
   {
      $this->Model_barang->delete($id);
      redirect('data_barang');
   }

   public function detail($id)
   {
      $data['title'] = 'Detail Barang';
      $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
      $data['barang'] = $this->Model_barang->tampilById($id);
      $this->load->view('admin_templates/header', $data);
      $this->load->view('admin_templates/sidebar');
      $this->load->view('admin_templates/topbar', $data);
      $this->load->view('admin/detail_barang', $data);
      $this->load->view('admin_templates/footer');
   }

   public function edit($id_barang)
   {
      $data['title'] = 'Edit';
      $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
      $data['barang'] = $this->Model_barang->getById($id_barang);
      $this->form_validation->set_rules('nama_blok', 'Nama nama_blok', 'required');
      if ($this->form_validation->run() == false) {
         $this->load->view('admin_templates/header', $data);
         $this->load->view('admin_templates/sidebar');
         $this->load->view('admin_templates/topbar', $data);
         $this->load->view('admin/edit_barang', $data);
         $this->load->view('admin_templates/footer');
      } else {
       

      }
   }

   public function simpanedit(){
       $id_barang = $this->input->post('id_barang');
       $nama_blok = $this->input->post('nama_blok');
       $latlng = $this->input->post('latlng');
       $harga = $this->input->post('harga');
       $ket = $this->input->post('ket');
       $gambar = $_FILES['gambar']['name'];
      if ($gambar) {
            $config['allowed_types'] = 'gif|jpg|png';
            $config['upload_path'] = './public/img/upload/';

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

            if ($this->upload->do_upload('gambar')) {
               $new_image = $this->upload->data('file_name');
               $this->db->set([
                  'blok' => $nama_blok,
                  'latlng' => $latlng,
                  'keterangan' => $ket,
                  'harga' => $harga,
                  'gambar' => $new_image
               ]);

                  $this->db->where('id_area', $id_barang);
                  $this->db->update('area_camping');

                  redirect('data_barang');
            } else {
               echo $this->upload->display_errors();
            }

      } else {
         $this->db->set([
            'blok' => $nama_blok,
            'latlng' => $latlng,
            'keterangan' => $ket,
            'harga' => $harga
         ]);
         $this->db->where('id_area', $id_barang);
         $this->db->update('area_camping');
         redirect('data_barang');
      }

   }
}
