<?php

/*
 * Copyright (c) 2018 Earworks.
 * Codeigniter Project Template Modification
 * Please contact our email for more information :
 * contact@earworks.co.id
 */
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Description of User
 *
 * @author Fajar Dwi Rianto <fajar@earworks.co.id>
 * @since 1.0
 */
class User extends Base_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->template = 'main-login';
    }

    public function index() {
        // silent is golden
    }

    public function login() {
        if ($this->is_login) {
            redirect(parent::BACKOFFICE_DASHBOARD);
        }
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'lang:username', 'required');
        $this->form_validation->set_rules('password', 'lang:password', 'required');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('user/login', array(
                'title' => $this->lang->line('title'),
            ));
        } else {
            $this->load->library('backoffice/user_lib');
            $apps = $this->user_lib->login(array(
                'username' => $this->input->post('username', true),
                'password' => $this->input->post('password', true),
                'remember' => $this->input->post('remember', true)
            ));
            if ($apps) {
                redirect(parent::BACKOFFICE_DASHBOARD);
            } else {
                redirect(parent::BACKOFFICE_LOGIN);
            }
        }
    }

    public function logout() {
        if ($this->is_login) {
            $this->load->library('backoffice/user_lib');
            $this->user_lib->logout();
        }
        redirect(parent::BACKOFFICE_LOGIN);
    }

    public function forgot() {
        if ($this->is_login) {
            redirect(parent::BACKOFFICE_DASHBOARD);
        }
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('email', 'lang:email', 'required|valid_email');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('user/forgot', array(
                'title' => $this->lang->line('title'),
            ));
        } else {
            $this->load->library('backoffice/user_lib');
            $apps = $this->user_lib->request_password(array(
                'email' => $this->input->post('email', true),
            ));
            if ($apps) {
                redirect(parent::BACKOFFICE_LOGIN);
            } else {
                $this->session->set_flashdata('error', $this->lang->line('error_forgot_lib'));
                redirect(current_url());
            }
        }
    }

    public function reset() {
        if ($this->is_login) {
            redirect(parent::BACKOFFICE_DASHBOARD);
        }
        $this->load->helper(array('form', 'hash'));
        $this->load->library(array('form_validation', 'backoffice/user_lib'));
        $this->form_validation->set_rules('new_password', 'lang:new_password', 'required');
        $this->form_validation->set_rules('confirm', 'lang:confirm', 'required|matches[new_password]');
        if ($this->form_validation->run() == FALSE) {
            $user = $this->user_lib->check_code($this->input->get('code'));
            if (!$user) {
                $this->session->set_flashdata('error', $this->lang->line('error_invalid_code'));
                redirect(parent::BACKOFFICE_LOGIN);
            }
            $this->load->view('user/reset', array(
                'title' => $this->lang->line('title'),
                'userid' => hash_data($user['userid']),
                'code' => $this->input->get('code')
            ));
        } else {
            $apps = $this->user_lib->reset_password(array(
                'new_password' => $this->input->post('new_password', true),
                'userid' => read_data($this->input->post('code', true))
            ));
            if ($apps) {
                redirect(parent::BACKOFFICE_LOGIN);
            } else {
                redirect(current_url());
            }
        }
    }

}
