<?php

/*
 * *************************************************
 * Copyright (C) 2020 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 Login
 *
 * @author andhy
 */
class Login extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('user_model');
        Ci::set_layout_file('main_login');
        Ci::helper('form');
    }

    public function index() {
        $data = [
            'username' => $this->post('username'),
            'password' => $this->post('password'),
            'rememberme' => $this->post('rememberme')
        ];

        $validate = $this->validate_form(['username', 'password'], false);

        if ($validate === true) {
            $model = new User_model();
            $result = $model->login($data, User_model::LOGIN_TYPE_PUBLIC);
            if ($result['status'] === 1) {
                redirect('dashboard');
            } else {
                $this->session->set_flashdata('error', $result['message']);
            }
        } elseif ($validate[static::$field_status] === static::CODE_INVALID_PARAMETER) {
            $this->_show_alert($validate);
        }

        Ci::view(__METHOD__, [
            'data' => $data,
            'redir' => $this->get_redir(true)
        ]);
    }

    public function change() {
        if (!$this->user) {
            redirect('login');
        }
        $model = Ci::model('model_usrmaster');
        $model->data([
            'newPassword' => [
                'data' => Ci::post('newPassword'),
                'rules' => 'trim|required',
                'label' => 'lang:newPassword'
            ],
            'confirmPassword' => [
                'data' => Ci::post('confirmPassword'),
                'rules' => 'trim|required|matches[newPassword]',
                'label' => 'lang:confirmPassword'
            ],
        ]);
        if ($model->validate() && $model->change_password($this->user)) {
            redirect('home/index');
        } else {
            if (($error = validation_errors())) {
                $this->session->set_flashdata('error', $error);
            }
            Ci::view(__METHOD__, ['model' => $model]);
        }
    }

    public function logout() {
        $user = new User_model();
        $user->logout($this->user['user']['id']);
        redirect('login');
    }

    public function forgot() {
        $model = Ci::model('model_usrmaster');
        $model->data([
            'userEmail' => Ci::post('userEmail')
        ]);
        if ($model->validate() && $model->send_forgot(1)) {
            redirect('login');
        } else {
            if (($error = validation_errors())) {
                $this->session->set_flashdata('error', $error);
            }
            Ci::view(__METHOD__, ['model' => $model]);
        }
    }

    public function reset($token = null) {
        $model = Ci::model('model_usrmaster');
        if (!$model->fill($model->find_by_reset_token($token))) {
            redirect('login');
        }

        $model->data([
            'newPassword' => [
                'data' => Ci::post('newPassword'),
                'rules' => 'trim|required',
                'label' => 'lang:newPassword'
            ],
            'confirmPassword' => [
                'data' => Ci::post('confirmPassword'),
                'rules' => 'trim|required|matches[newPassword]',
                'label' => 'lang:confirmPassword'
            ]
        ]);
        if ($model->validate() && $model->reset_password(1)) {
            redirect('login');
        } else {
            if (($error = validation_errors())) {
                $this->session->set_flashdata('error', $error);
            }
            Ci::view(__METHOD__, ['model' => $model]);
        }
    }

}
