<?php
defined('BASEPATH') OR exit('No direct script access allowed');
        
date_default_timezone_set("Asia/Jakarta");
class Dashboard extends MY_Controller
{
  function __construct()
  {
    parent::__construct();
    $this->load->model('login_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['totalbalita'] = $this->login_m->totalbalita(); 
    $data['totalkriteria'] = $this->login_m->totalkriteria(); 
    $data['totalalternatif'] = $this->login_m->totalalternatif(); 
    $data['totalperhitungan'] = $this->login_m->totalperhitungan();
    $data['title']  = 'Beranda'; 
    $data['menu'] = 0;
    
    $this->load->view('admin/template/header',$data);
    $this->load->view('admin/template/sidebar',$data);
    $this->load->view('admin/template/navbar');
    $this->load->view('admin/dashboard',$data);
    $this->load->view('admin/template/footer');
  }
  
  public function profil()
  {
    // 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['title']  = 'Profil';
    $data['menu'] = 7;
    
    $this->load->view('admin/template/header',$data);
    $this->load->view('admin/template/sidebar',$data);
    $this->load->view('admin/template/navbar');
    $this->load->view('admin/profil',$data);
    $this->load->view('admin/template/footer');
  }
  
  public function proses_edit_profil(){
      if ($this->POST('edit')) {
      
          if ($this->login_m->get_num_row(['email' => $this->POST('email')]) != 0 && $this->POST('email') != $this->POST('email_x')) { 
            $this->flashmsg('Email telah digunakan!', 'warning');
            redirect('admin/profil');
            exit();
          }
          $this->login_m->update($this->POST('email_x'),['email' => $this->POST('email')]);    
          $user_session = [
            'email' => $this->POST('email'),  
          ];
          $this->session->set_userdata($user_session);
 
  
          $this->flashmsg('PROFIL BERHASIL DISIMPAN!', 'success');
          redirect('admin/profil');
          exit();

          }

        elseif ($this->POST('uploadfoto')) {
          if ($_FILES['foto']['name'] !== '') { 
              $files = $_FILES['foto'];
              $_FILES['foto']['name'] = $files['name'];
              $_FILES['foto']['type'] = $files['type'];
              $_FILES['foto']['tmp_name'] = $files['tmp_name'];
              $_FILES['foto']['size'] = $files['size'];

              $id_foto = rand(1,9);
              for ($j=1; $j <= 11 ; $j++) {
                $id_foto .= rand(0,9);
              } 

              if ($data['profil']->foto != 'foto/default.jpg' && $data['profil']->foto != 'foto/default-l.jpg' && $data['profil']->foto != 'foto/default-p.jpg') {
                @unlink(realpath(APPPATH . '../assets/' . $data['profil']->foto));
              }
              $this->upload($id_foto, 'foto/','foto');    
              $this->login_m->update($data['profil']->email,['foto' => 'foto/'.$id_foto.'.jpg']);
              $this->flashmsg('Foto Profil berhasil diupload!', 'success');
              redirect('admin/profil');
              exit();
            }else{
              redirect('admin/profil');
              exit(); 
            }
        } 
        elseif ($this->POST('hapusfoto')) { 
              @unlink(realpath(APPPATH . '../assets/' . $data['profil']->foto)); 
              $this->login_m->update($data['profil']->email,['foto' => 'foto/default.jpg']);
              $this->flashmsg('Foto Profil berhasil dihapus!', 'success');
              redirect('admin/profil');
              exit();
            
         } 
          elseif ($this->POST('edit2')) { 
            $data = [ 
              'password' => md5($this->POST('pass1')) 
            ];
            
            $this->login_m->update($data['email'],$data);
        
            $this->flashmsg('PASSWORD BARU TELAH TERSIMPAN!', 'success');
            redirect('admin/profil');
            exit();    
          }  

          else{

          redirect('admin/profil');
          exit();
          }

    }
    public function cekpasslama(){ echo $this->login_m->cekpasslama($data['email'],$this->input->post('pass')); } 
    public function cekpass(){ echo $this->login_m->cek_password_length($this->input->post('pass1')); }
    public function cekpass2(){ echo $this->login_m->cek_passwords($this->input->post('pass1'),$this->input->post('pass2')); }
} ?>