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

class Inventaris extends CI_Controller {

    public function __construct()
	{
		parent::__construct();
        $this->load->library("form_validation");
        $this->load->model('m_siplabs');
        $this->load->model('m_inventaris');
		if (null == $this->session->level) {
			redirect('auth');
		} else {
			if ($this->session->level != "Admin" && $this->session->level != "Kepala Sekolah") {
				redirect('auth', 'refresh');
			}
		}
	}
	public function index()
	{
        $data['root'] = "Inventaris";
        $data['title'] = "Inventaris";
        $data['user'] = $this->session->userdata('id_user');
		$data['username'] = $this->session->userdata('username');
        $data['inventaris'] = $this->db->get('inventaris')->result();
        $this->load->view('templates/header', $data);
        $this->load->view('admin/inventaris', $data);
        $this->load->view('templates/footer');
	}
    public  function del($q)
    {
        $this->m_siplabs->deleteinv($q);
        if($this->db->affected_rows() > 0){
           $this->session->set_flashdata(
                'notif',
                '<div class="alert alert-danger text-center" role="alert">
                Data Berhasil Dihapus!!</div>'
            );

        }
        echo "<script>window.location='".site_url('inventaris')."';</script>";
    }
   	
    public function proses_tambahdata()
    {
        
		$nama_inv = $this->input->post('nama_inv');
		$kondisi = $this->input->post('kondisi');
		$jenis = $this->input->post('jenis');
		$milik = $this->input->post('milik');
		$jumlah = $this->input->post('jumlah');
        $image = $image;
        $this->load->library('upload');
        $config['upload_path'] = './files/inventaris/'; // Sesuaikan sama folder dimana foto akan d simpan
        $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
        $config['max_size'] = '2048';
        $config['max_width']  = '1288';
        $config['max_height']  = '768';
        $config['encrypt_name'] = TRUE;

        $this->upload->initialize($config);
        if(!$this->upload->do_upload('image')){
            $image="";
        }else{
            $image=$this->upload->file_name;
        }
			
        $array = [
				'image' => $image,
				'nama_inv' => $nama_inv,
				'kondisi' => $kondisi,
				'jenis' => $jenis,
				'milik' => $milik,
				'jumlah' => $jumlah,
				
        ];
        
		$this->m_siplabs->proses_tambahdata($array);
        $this->session->set_flashdata(
            'notif',
            '<div class="alert alert-success text-center" role="alert">
            Data Berhasil Ditambahkan!!</div>'
        );
        redirect('inventaris');
    }

    public function edit($id_inv)
    {
        $data['root'] = "Inventaris";
        $data['title'] = "Edit Inventaris";
        $data['user'] = $this->session->userdata('id_user');
		$data['username'] = $this->session->userdata('username');
        $where = array('id_inv' => $id_inv );
        $data['inventaris'] = $this->m_siplabs->editdata($where,'inventaris')->result();
        $this->load->view('templates/header',$data);
        $this->load->view('admin/edit', $data);
        $this->load->view('templates/footer');
        
    }

    public function updatedata()
    {
        $id_inv = $this->input->post('id_inv');
        $nama_inv = $this->input->post('nama_inv');
        $kondisi = $this->input->post('kondisi');
        $jenis = $this->input->post('jenis');
        $milik = $this->input->post('milik');
        $jumlah = $this->input->post('jumlah');
        $image = $image;
        $this->load->library('upload');
        $config['upload_path'] = './files/inventaris/'; // Sesuaikan sama folder dimana foto akan d simpan
        $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
        $config['max_size'] = '2048';
        $config['max_width']  = '1288';
        $config['max_height']  = '768';
        $config['encrypt_name'] = TRUE;

        $this->upload->initialize($config);
        if(!$this->upload->do_upload('image')){
            $image="";
        }else{
            $image=$this->upload->file_name;
        }
        $data = array (
            'image'     => $image,
            'nama_inv' => $nama_inv,
            'kondisi' => $kondisi,
            'jenis' => $jenis,
            'milik' => $milik,
            'jumlah' => $jumlah
            
        );

        $where = array (
            'id_inv' => $id_inv
        );

        $this->m_siplabs->update_data($where,$data,'inventaris');
        $this->session->set_flashdata(
            'notif',
            '<div class="alert alert-success text-center" role="alert">
            Data Berhasil Diedit!!</div>'
        );
        redirect('inventaris/index');
    }
    //peminjaman inv
    public function peminjaman_inv()
    {
        $data['root'] = "Inventaris";
        $data['title'] = "Peminjaman inventaris";
        $data['user'] = $this->session->userdata('id_user');
		$data['username'] = $this->session->userdata('username');
        $this->load->view('templates/header', $data);
        $this->load->view('admin/peminjaman_inv', $data);
        $this->load->view('templates/footer');
    }

    public function accinv($id_pinjam)
    {
        $user = $this->db->get_where('user',['id_user'=> '27'])->row_array();
        $data = [
			'status'  => 1,
			'yg_melayani' => $user['nama_lengkap']
        ];
        $where = [
            'id_pinjam' => $id_pinjam

        ];
        $this->m_siplabs->update_data($where,$data,'pinjam_inv');
        redirect('inventaris/peminjaman_inv');
    }

    public function disaccinv($p)
    {
        $this->m_siplabs->deletepinjam($p);
        if($this->db->affected_rows() > 0){
           $this->session->set_flashdata(
                'notif',
                '<div class="alert alert-danger text-center" role="alert">
                Data Berhasil DiTolak!!</div>'
            );

        }
        echo "<script>window.location='".site_url('inventaris/peminjaman_inv')."';</script>";
    }

    public function termpengembalian($id_pinjam)
{
    date_default_timezone_set("Asia/Bangkok");
    $time =  Date('Y-m-d h:i:s');
    $user = $this->db->get_where('user',['id_user'=> '27'])->row_array();
    
    // Ambil data pinjam_inv berdasarkan id_pinjam
    $pinjam = $this->db->get_where('pinjam_inv', ['id_pinjam' => $id_pinjam])->row_array();
    
    // Cek apakah status pinjaman sudah 1 atau belum
    if ($pinjam['status'] == 1) {
        
        // Update status pinjaman menjadi 2
        $data = [
            'status' => 2,
        ];
        $where = [
            'id_pinjam' => $id_pinjam
        ];
        $this->m_siplabs->updatedata('pinjam_inv',$data,$where); 
        
        // Tambahkan data pengembalian ke tabel pengembalian_inv
        $array = [
            'id_pinjam' => $id_pinjam,
            'id_user' => $pinjam['id_user'],
            'id_inv' => $pinjam['id_inv'],
            'tgl_pengembalian' => $time,
            'yg_menerima' => $user['nama_lengkap']
        ];
        $this->db->insert('pengembalian_inv', $array);
        $this->session->set_flashdata(
            'notif',
            '<div class="alert alert-success text-center" role="alert">
            Pengembalian Sukses!!</div>'
        );
        
        redirect('inventaris/pengembalian_inv');
        
    } else {
        // Jika status pinjaman bukan 1, maka kembalikan ke halaman peminjaman_inv
        $this->session->set_flashdata(
            'notif',
            '<div class="alert alert-danger text-center" role="alert">
            Harap Terima Peminjaman Dahalu!!</div>'
        );
        redirect('inventaris/peminjaman_inv');
    }
}



    public function pengembalian_inv()
    {
        $data['root'] = "Inventaris";
        $data['title'] = "Pengembalian inventaris";
        $data['user'] = $this->session->userdata('id_user');
		$data['username'] = $this->session->userdata('username');
        $data['pengembalian'] = $this->db->query('SELECT * FROM `pengembalian_inv` INNER JOIN pinjam_inv ON pengembalian_inv.id_pinjam=pinjam_inv.id_pinjam INNER JOIN user ON pinjam_inv.id_user=user.id_user INNER JOIN inventaris ON pinjam_inv.id_inv=inventaris.id_inv')->result();
        $this->load->view('templates/header', $data);
        $this->load->view('admin/pengembalian_inv', $data);
        $this->load->view('templates/footer');
    }

    public function hapuspengembalianinv($p)
    {
        $this->m_siplabs->deletepengembalian($p);
        if($this->db->affected_rows() > 0){
           $this->session->set_flashdata(
                'notif',
                '<div class="alert alert-danger text-center" role="alert">
                Data Berhasil DiHapus!!</div>'
            );

        }
        echo "<script>window.location='".site_url('inventaris/pengembalian_inv')."';</script>";
    }

    public function laporanpinjam()
    {
        $data['title'] = "Laporan Peminjaman Inventaris #" . uniqid();
		$data['user'] = $this->session->userdata('id_user');
		$data['username'] = $this->session->userdata('username');
        $data['laporpinjam'] = $this->db->query("SELECT * FROM pengembalian_inv INNER JOIN pinjam_inv, inventaris, user WHERE pengembalian_inv.id_pinjam=pinjam_inv.id_pinjam
        AND pinjam_inv.id_inv=inventaris.id_inv
        AND pinjam_inv.id_user=user.id_user
        AND pinjam_inv.status=2
        ")->result();
        
		$this->load->view('templates/header', $data);
		$this->load->view('admin/laporanpinjam', $data);
		$this->load->view('templates/footer');
    }

    public function laporaninventaris()
    {
        $data['title'] = "Laporan Inventaris Rusak #" . uniqid();
		$data['user'] = $this->session->userdata('id_user');
		$data['username'] = $this->session->userdata('username');
        $data['laporinv'] = $this->db->query("SELECT * FROM inventaris
        WHERE kondisi='Rusak'")->result();
        $this->load->view('templates/header', $data);
		$this->load->view('admin/laporaninv', $data);
		$this->load->view('templates/footer');
    }

    public function filter($bulan)
    {
        $laporpinjam = $this->m_inventaris->filter($bulan);
        $data = array();
        $no = 1;
        foreach ($laporpinjam as $lp) {
            $row = array();
            $row['no'] = $no++;
            $row['nama_lengkap'] = $lp->nama_lengkap;
            $row['nama_inv'] = $lp->nama_inv;
            $row['tgl_pinjam'] = $lp->tgl_pinjam;
            $row['yg_melayani'] = $lp->yg_melayani;
            $row['tgl_pengembalian'] = date('d/m/Y', strtotime($lp->tgl_pengembalian));
            $row['yg_menerima'] = $lp->yg_menerima;
            $data[] = $row;
        }
        echo json_encode($data);
    }
}