<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Lupapassword extends MY_Controller {
  function __construct() {
    parent::__construct();
    
    require APPPATH.'libraries/classes/class.phpmailer.php';
    $this->data['email'] = $this->session->userdata('email');
    $this->data['id_role']  = $this->session->userdata('id_role');
    if (isset($this->data['email'], $this->data['id_role']))
    {
      switch ($this->data['id_role'])
      {
        case 1:
          redirect('admin');
          break;
      }
      exit;
    }
    $this->load->model('Login_m');
    date_default_timezone_set("Asia/Jakarta");  
  }

  public function index()
  { 
    $this->load->view('lupapassword');
  }

  public function resetpassword() { 
    $email  = $this->POST('email'); 
    if ($this->Login_m->get_num_row(['email' => $email]) == 0) {
      $this->flashmsg('Email tidak terdaftar', 'danger');
      redirect('lupapassword/');
      exit();  
    }
    $akun = $this->Login_m->get_row(['email' => $email]);
    $data['email'] = $email;
    $this->load->view('resetpassword', $data);
  }

  public function proresreset() { 
    if ($this->POST('proses')) { 
      $email = $this->POST('email');

      if ($this->POST('password') != $this->POST('password2')) {
        $this->flashmsg('Password tidak sama', 'danger');
        redirect('lupapassword/resetpassword');
        exit();
      }
      $user = $this->Login_m->get_row(['email' => $email]);
      if ($this->Login_m->update($email, ['password' => md5($this->POST('password'))])) {
        $user_session = [
          'email' => $user->email, 
          'id_role' => $user->role 
        ];
        $this->session->set_userdata($user_session);
        $this->flashmsg('Password berhasil diganti', 'success');
        redirect('dashboard');
        exit();
      } 
    }else{
      redirect('lupapassword/');
      exit(); 
    } 
  }
}

?>