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

class Alpha extends CI_Controller {
	//Mendeklarasikan kelas controller "Alpha"

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct()
    {
        parent::__construct();
		$this->load->model('User_model');
		//Memuat model 'User_model' agar dapat digunakan dalam controller ini.
        $this->load->model('Alpha_model');
		$this->load->model('Hasil_forecast_model');
		is_logged_in();
		//Memanggil fungsi untuk memeriksa apakah pengguna telah login.
    }
	public function index()
	{
		$data['nama'] = "Parameter Alpha";
		//Menyiapkan variabel $data['nama'] dengan nilai "Parameter Alpha" untuk ditampilkan di halaman
		$id_user=$this->session->userdata('id_user');
		//Mendapatkan id pengguna dari session
        $data['user']=$this->User_model->get_user($id_user);
		$data['alpha']=$this->Alpha_model->get_alpha();
		//Mendapatkan data alpha dari model Alpha dan disimpan dalam variabel
		$this->form_validation->set_rules('alpha', 'Alpha', 'required');
		if ($this->form_validation->run() == false) {
			//Memeriksa apakah validasi form gagal.
        $this->load->view('Templates/header.php', $data);
		//Memuat view 'header.php' dari folder "Templates" 
        $this->load->view('Templates/navbar.php', $data);
        $this->load->view('Templates/leftmenu.php', $data);
        $this->load->view('Alpha/index.php', $data);
		$this->load->view('Templates/footer.php', $data);
	}else{
		$alpha =$this->input->post('alpha', true);
		//Mengambil nilai 'alpha' dari form

		$this->Alpha_model->update_alpha($alpha,1);
		$this->Hasil_forecast_model->update_alpha($alpha);

		$this->session->set_flashdata('flash', 'Diubah');
		$this->session->set_flashdata('data', 'Alpha');
		redirect('alpha');
	}
	}
}