<?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 Dashboard
 *
 * @author Fajar Dwi Rianto <fajar@earworks.co.id>
 * @since 1.0
 */
class Dashboard extends Base_Controller {

    public function index() {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $user = $this->session->userdata(CI_NAME . 'log');
        if ($user['roleid'] == 3) {
            redirect('backoffice/testride');
        } elseif ($user['roleid'] == 4) {
            redirect('backoffice/register');
        } elseif ($user['roleid'] == 5) {
//            redirect('backoffice/');
        }
        $this->load->model(array('offroad_model', 'register_model', 'userrole_model'));
        $this->load->view('dashboard/index', array(
            'register' => $this->register_model->check(),
            'offroad' => $this->offroad_model->check(array('status' => 2)),
            'admin' => $this->userrole_model->check(array('roleid' => 2)),
            'u_register' => $this->userrole_model->check(array('roleid' => 4)),
            'u_testride' => $this->userrole_model->check(array('roleid' => 3)),
            'u_testdrive' => $this->userrole_model->check(array('roleid' => 5))
        ));
    }

    public function profile() {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('profilename', 'lang:profilename', 'required');
        $this->form_validation->set_rules('profileaddress', 'lang:profileaddress', 'trim');
        $this->form_validation->set_rules('profilemobile', 'lang:profilemobile', 'trim');
        $this->form_validation->set_rules('profilepathtitle', 'lang:profilepathtitle', 'trim');
        $this->form_validation->set_rules('profilepathalt', 'lang:profilepathalt', 'trim');
        if ($this->form_validation->run() == FALSE) {
            $user = $this->session->userdata(CI_NAME . 'log');
            $this->load->view('dashboard/profile', array('input' => $user));
        } else {
            $this->load->library('backoffice/user_lib');
            $this->load->helper('upload');
            $apps = $this->user_lib->change_profile(array(
                'profilename' => $this->input->post('profilename', true),
                'profileaddress' => $this->input->post('profileaddress', true),
                'profilemobile' => $this->input->post('profilemobile', true),
                'profilepathtitle' => $this->input->post('profilepathtitle', true),
                'profilepathalt' => $this->input->post('profilepathalt', true),
                'profileupload' => do_upload('profilepath'),
            ));
            if ($apps) {
                $this->session->set_flashdata('success', $this->lang->line('dashboardprofile_success'));
                redirect(parent::BACKOFFICE_DASHBOARD);
            } else {
                $this->session->set_flashdata('error', $this->lang->line('dashboardprofile_error'));
                redirect(current_url());
            }
        }
    }

    public function password() {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('old_password', 'lang:old_password', 'required');
        $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) {
            $this->load->view('dashboard/password');
        } else {
            $this->load->library('backoffice/user_lib');
            $apps = $this->user_lib->change_password(array(
                'old_password' => $this->input->post('old_password', true),
                'new_password' => $this->input->post('new_password', true),
                'confirm' => $this->input->post('confirm', true)
            ));
            if ($apps) {
                $this->session->set_flashdata('success', $this->lang->line('dashboardpassword_success'));
                redirect(parent::BACKOFFICE_DASHBOARD);
            } else {
                $this->session->set_flashdata('error', $this->lang->line('dashboardpassword_error'));
                redirect(current_url());
            }
        }
    }

    public function language($lang = null) {
        $this->session->set_userdata('language', $lang);
        $language = array('id' => 'indonesian', 'us' => 'english');
        setcookie(CI_NAME . '_lang', $language[$lang], strtotime("+30 days", time()), '/');
        $this->load->library('user_agent');
        redirect($this->agent->referrer());
    }

}
