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

class Kopi extends CI_Controller {


	public function __construct()
    {
        parent::__construct();
		$this->load->model('User_model');
        $this->load->model('Alpha_model');
        $this->load->model('Data_kopi_model');
        $this->load->model('Jenis_model');
        $this->load->model('Hasil_forecast_model');
		is_logged_in();
    }
    public function index()
	{

		$data['data_kopi']=$this->Data_kopi_model->get_all_data_kopi();
		$id_user=$this->session->userdata('id_user');
        $data['user']=$this->User_model->get_user($id_user);
	
		$data['nama'] = "Data Produksi Kopi";
		
        $this->load->view('Templates/header.php',$data);
        $this->load->view('Templates/navbar.php',$data);
        $this->load->view('Templates/leftmenu.php',$data);
        $this->load->view('Minyak/index.php',$data);
        $this->load->view('Templates/footer.php',$data);
    }
    public function tambah_kopi()
	{
	
        $data['jenis_kopi']=$this->Jenis_model->get_all_data();
		$id_user=$this->session->userdata('id_user');
        $data['user']=$this->User_model->get_user($id_user);
        $data['nama'] = "Tambah Data Produksi Kopi";
        $this->form_validation->set_rules('jumlah_kopi', 'jumlah_kopi', 'required');
        $this->form_validation->set_rules('bulan_tahun', 'bulan_tahun', 'required');
        $this->form_validation->set_rules('jenis_kopi', 'jenis_kopi', 'required');
		if ($this->form_validation->run() == false) {
        $this->load->view('Templates/header.php',$data);
        $this->load->view('Templates/navbar.php',$data);
        $this->load->view('Templates/leftmenu.php',$data);
        $this->load->view('Minyak/tambah_minyak.php',$data);
        $this->load->view('Templates/footer.php',$data);
        }else{
         
		 
		$bulan_tahun=date($this->input->post('bulan_tahun', true));
		$bulan=(integer)date("m",strtotime($bulan_tahun));
		$tahun=date("Y",strtotime($bulan_tahun));
		$jumlah_kopi=$this->input->post('jumlah_kopi', true);
        $jenis_kopi =$this->input->post('jenis_kopi', true);

		$this->Data_kopi_model->add_data_kopi($tahun,$bulan,$jumlah_kopi, $jenis_kopi);
        $insert_id = $this->db->insert_id();
        $this->Hasil_forecast_model->create($insert_id,$jenis_kopi,$jumlah_kopi);


		$this->session->set_flashdata('flash', 'Ditambahkan');
		$this->session->set_flashdata('data', 'Data Kopi');

		redirect('kopi');
        }
    }
    public function hapus_kopi($id)
	{
	
		$this->Data_kopi_model->hps_data_kopi($id);
        $this->Data_kopi_model->delete_hasil_ses($id);
		$this->session->set_flashdata('flash', 'dihapus');
        $this->session->set_flashdata('data', 'Data Kopi');
	
		redirect('kopi');
    }
    public function edit_kopi($id)
	{
	
        $data['jenis_kopi']=$this->Jenis_model->get_all_data();
		$data['data_kopi']=$this->Data_kopi_model->get_data_kopi_byid($id);
		$id_user=$this->session->userdata('id_user');
        $data['user']=$this->User_model->get_user($id_user);
        $data['nama'] = "Edit Produksi Kopi";
   
        $this->load->view('Templates/header.php',$data);
        $this->load->view('Templates/navbar.php',$data);
        $this->load->view('Templates/leftmenu.php',$data);
        $this->load->view('Minyak/edit_minyak.php',$data);
        $this->load->view('Templates/footer.php',$data);
       }
        public function save_edit()
        {
            $id=$this->input->post('id', true);

            $tahun=date($this->input->post('tahun', true));
            $bulan=date($this->input->post('bulan', true));

            $jumlah_kopi=$this->input->post('jumlah_kopi', true);

            $this->Data_kopi_model->update_data_kopi($id,$jumlah_kopi,$tahun,$bulan);
            $this->Hasil_forecast_model->update($id,$jumlah_kopi);

            $this->session->set_flashdata('flash', 'Diupdate');
            $this->session->set_flashdata('data', 'Data Kopi');
            redirect('kopi');
        }
}