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

    public function index() {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->library('backoffice/users_lib');
        $this->load->view('users/index', array('data' => $this->users_lib->get()));
    }

    public function create() {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'lang:username', 'trim|required');
        $this->form_validation->set_rules('useremail', 'lang:useremail', 'trim|required|valid_email');
        $this->form_validation->set_rules('userpassword', 'lang:userpassword', 'trim|required');
        $this->form_validation->set_rules('confirm', 'lang:confirm', 'trim|required|matches[userpassword]');
        $this->form_validation->set_rules('profilename', 'lang:profilename', 'trim|required');
        $this->form_validation->set_rules('profileaddress', 'lang:profileaddress', 'trim');
        $this->form_validation->set_rules('profilemobile', 'lang:profilemobile', 'trim');
        $input = array(
            'username' => $this->input->post('username', true),
            'useremail' => $this->input->post('useremail', true),
            'userpassword' => $this->input->post('userpassword', true),
            'confirm' => $this->input->post('confirm', true),
            'profilename' => $this->input->post('profilename', true),
            'profileaddress' => $this->input->post('profileaddress', true),
            'profilemobile' => $this->input->post('profilemobile', true)
        );
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('users/create', array('input' => $input));
        } else {
            $this->load->library('backoffice/users_lib');
            $apps = $this->users_lib->create($input);
            if ($apps) {
                redirect('backoffice/users/index');
            } else {
                redirect(current_url());
            }
        }
    }

    public function update($id = null) {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->library('backoffice/users_lib');
        $admin = $this->users_lib->get($id);
        if (!$admin || !$id) {
            redirect('backoffice/users/index');
        }
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'lang:username', 'trim|required');
        $this->form_validation->set_rules('useremail', 'lang:useremail', 'trim|required|valid_email');
        $this->form_validation->set_rules('userpassword', 'lang:userpassword', 'trim');
        $this->form_validation->set_rules('confirm', 'lang:confirm', 'trim|matches[userpassword]');
        $this->form_validation->set_rules('profilename', 'lang:profilename', 'trim|required');
        $this->form_validation->set_rules('profileaddress', 'lang:profileaddress', 'trim');
        $this->form_validation->set_rules('profilemobile', 'lang:profilemobile', 'trim');
        $input = array(
            'username' => $this->input->post('username', true, $admin['username']),
            'useremail' => $this->input->post('useremail', true, $admin['useremail']),
            'userpassword' => $this->input->post('userpassword', true),
            'confirm' => $this->input->post('confirm', true),
            'profilename' => $this->input->post('profilename', true, $admin['profilename']),
            'profileaddress' => $this->input->post('profileaddress', true, $admin['profileaddress']),
            'profilemobile' => $this->input->post('profilemobile', true, $admin['profilemobile'])
        );
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('users/update', array('input' => $input, 'id' => $id));
        } else {
            $apps = $this->users_lib->update($input, $id);
            if ($apps) {
                redirect('backoffice/users/index');
            } else {
                redirect(current_url());
            }
        }
    }

    public function delete($id = null) {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->library('backoffice/users_lib');
        if ($this->users_lib->delete($id)) {
        }
        redirect('backoffice/users/index');
    }

    public function suspend($id = null) {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->library('backoffice/users_lib');
        if ($this->users_lib->suspend($id, 0)) {
        }
        redirect('backoffice/users/index');
    }
    public function unsuspend($id = null) {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->library('backoffice/users_lib');
        if ($this->users_lib->suspend($id)) {
        }
        redirect('backoffice/users/index');
    }
}
