<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Buku extends CI_Controller {

    function __construct()
    {
        parent::__construct();

        if (!$this->session->userdata('login')) {
            redirect('login');
        }

        $this->load->model('Buku_model');
        $this->load->model('Peminjaman_model');
    }

    public function index()
{
    $q = $this->input->get('q',true);
    $kategori = $this->input->get('kategori',true);

    $data['buku']        = $this->Buku_model->get($q,$kategori);
    $data['kategori']    = $this->Buku_model->kategori();
    $data['notif_count'] = $this->Peminjaman_model->count_notifikasi();

    $this->load->view('buku/index', $data);
}


    public function tambah()
    {
        if ($this->session->userdata('level') != 'superadmin') {
            show_error('Akses ditolak');
        }

        $config['upload_path']   = './uploads/';
        $config['allowed_types'] = 'jpg|png|jpeg';
        $config['file_name']     = time();

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

        $gambar = 'default.png';

        if (!empty($_FILES['gambar']['name'])) {
            if ($this->upload->do_upload('gambar')) {
                $gambar = $this->upload->data('file_name');
            }
        }

        $data = [
            'judul'       => $this->input->post('judul'),
            'id_kategori' => $this->input->post('kategori'),
            'penulis'     => $this->input->post('penulis'),
            'penerbit'    => $this->input->post('penerbit'),
            'tahun'       => $this->input->post('tahun'),
            'stok'        => $this->input->post('stok'),
            'gambar'      => $gambar
        ];

        $this->db->insert('buku', $data);

        redirect('buku');
    }

    public function edit($id)
    {
        $data['row']      = $this->Buku_model->edit($id);
        $data['kategori'] = $this->Buku_model->kategori();

        $this->load->view('buku/edit', $data);
    }

    public function update(){

$id = $this->input->post('id');

$data = [
'id_kategori'=>$this->input->post('kategori'),
'judul'=>$this->input->post('judul'),
'penulis'=>$this->input->post('penulis'),
'penerbit'=>$this->input->post('penerbit'),
'tahun'=>$this->input->post('tahun'),
'stok'=>$this->input->post('stok')
];


// =================
// UPLOAD GAMBAR
// =================
if(!empty($_FILES['cover']['name'])){

$config['upload_path']   = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|png|webp';
$config['max_size']      = 2048;
$config['file_name']     = 'cover_'.time();

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

if($this->upload->do_upload('cover')){

$file = $this->upload->data();

// simpan nama file ke database
$data['gambar'] = $file['file_name'];

}else{

echo $this->upload->display_errors();
return;

}
}


// =================
// UPDATE DATABASE
// =================
$this->db->where('id_buku',$id);
$this->db->update('buku',$data);

$this->session->set_flashdata('success','Data buku berhasil diupdate');
redirect('buku');
}

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