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

class Hasil extends MY_Controller{

    function __construct()
    {
    parent::__construct();
    $this->load->model('login_m'); 
    $this->load->model('Balita_m');
    $this->load->model('Kriteria_m');
    $this->load->model('Alternatif_m');
    $this->load->model('BobotPreferensi_m');
    $this->load->model('Hasil_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_hasil'] = $this->Hasil_m->get_all();     
    $data['title']  = 'Data Hasil Perhitungan';
    $data['menu'] = 5;

    $this->load->view('admin/template/header',$data);
    $this->load->view('admin/template/sidebar',$data);
    $this->load->view('admin/template/navbar');
    $this->load->view('admin/hasil/v_hasil',$data);
    $this->load->view('admin/template/footer');
    }

    public function detailhasil($id)
    { 
    // 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['data_hasil'] = $this->Hasil_m->get_by_id($id);  
    $data['list_kriteria'] = $this->Kriteria_m->get_all_data(); 
    $data['list_bobotpreferensi'] = $this->BobotPreferensi_m->get_all_data();
    $data['list_alternatif'] = $this->Alternatif_m->get_all_data();
    $data['title']  = 'Detail Data Perhitungan';
    $data['id_perhitungan']  = $id;
    $data['menu'] = 5;

    $this->load->view('admin/template/header',$data);
    $this->load->view('admin/template/sidebar',$data);
    $this->load->view('admin/template/navbar');
    $this->load->view('admin/hasil/v_proses_saw',$data);
    $this->load->view('admin/template/footer');
    } 

    // perhitungan metode SAW Tiap Balita
    public function simpanperhitungan()
    { 
        $id = $this->POST('id_perhitungan');
        $nama_alternatif = $this->POST('nama_alternatif');
        
        $data = [
        'hasil' => $nama_alternatif
        ];

        $this->Hasil_m->update($id,$data);    
        $this->flashmsg('Hasil SAW berhasil disimpan!', 'success');
        // echo '<script>
        //         // Membuka halaman cetak hasil dalam tab baru (_blank), memicu dialog cetak, dan kemudian melakukan redirect setelah mencetak
        //         var newWindow = window.open("' . site_url("hasil/cetakhasil/$id") . '", "_blank");
        //         newWindow.onload = function() {
        //             newWindow.print();
        //             newWindow.onafterprint = function() {
        //                 window.location.href = "' . site_url("hasil") . '";
        //             };
        //         };
        //     </script>';
        redirect('hasil');
        exit();
    } 


    public function cetakhasil($id)
    { 
    $this->check_login();

    $email = $this->session->userdata('email');
    $data['profil'] = $this->login_m->get_row(['email' =>$email ]); 

    $data['data_hasil'] = $this->Hasil_m->cetak($id);
    $data['menu'] = 5;

    $this->load->view('admin/hasil/v_cetak_hasil',$data);
    }

} 
?>