<?php
defined('BASEPATH') OR exit('No direct script access allowed');
date_default_timezone_set("Asia/Jakarta");

class BobotPreferensi extends MY_Controller{
  
  function __construct()
  {
    parent::__construct();
    $this->load->model('login_m'); 
    $this->load->model('BobotPreferensi_m');       
  }

  public function index()
  { 
    // Memeriksa apakah pengguna telah login sebelum melanjutkan eksekusi fungsi
    // memanggil MY_controller di folder core
    $this->check_login();
    
    $email = $this->session->userdata('email');
    $data['profil'] = $this->login_m->get_row(['email' =>$email ]); 
    
    $data['list_bobotpreferensi'] = $this->BobotPreferensi_m->get_all_data();   
    $data['title']  = 'Kelola Data Bobot Preferensi';
    $data['menu'] = 4;
    
    $this->load->view('admin/template/header',$data);
    $this->load->view('admin/template/sidebar',$data);
    $this->load->view('admin/template/navbar');
    $this->load->view('admin/bobotpreferensi/v_bobotpreferensi',$data);
    $this->load->view('admin/template/footer');
  }

// fungsi untuk proses tambah , edit dan hapus
  public function prosesbobotpreferensi(){
    if ($this->POST('tambah')) {
      $data = [ 
        'keterangan' => $this->input->post('keterangan'), // Mengambil nilai 'keterangan' dari input POST
        'nilai' => $this->input->post('nilai'), // Mengambil nilai 'nilai' dari input POST
        
      ];
      $this->BobotPreferensi_m->insert($data);
      $this->flashmsg('Data Bobot Preferensi berhasil ditambah!', 'success');
      redirect('bobotpreferensi');
      exit();
    }

    if ($this->POST('edit')) {
      $id = $this->POST('id_bobot');
      $data = [ 
        'keterangan' => $this->input->post('keterangan'), // Mengambil nilai 'keterangan' dari input POST
        'nilai' => $this->input->post('nilai'), // Mengambil nilai 'nilai' dari input POST
      ];

      $this->BobotPreferensi_m->update($id,$data);    
      $this->flashmsg('Data Bobot Preferensi berhasil disimpan!', 'success');
      redirect('bobotpreferensi');
      exit();
    }

    if ($this->POST('hapus')) {
      $this->BobotPreferensi_m->delete($this->POST('id_bobot'));
      $this->flashmsg('Data Bobot Preferensi berhasil dihapus!', 'success');
      redirect('bobotpreferensi');
      exit();

    } 
        
  } 
  
} 
?>