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

    public function index() {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->library('backoffice/admins_lib');
        $this->load->view('admins/index', array('data' => $this->admins_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('admins/create', array('input' => $input));
        } else {
            $this->load->library('backoffice/admins_lib');
            $apps = $this->admins_lib->create($input);
            if ($apps) {
                $this->session->set_flashdata('success', $this->lang->line('adminscreate_success'));
                redirect('backoffice/admins/index');
            } else {
                $this->session->set_flashdata('error', $this->lang->line('adminscreate_error'));
                redirect(current_url());
            }
        }
    }

    public function update($id = null) {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->library('backoffice/admins_lib');
        $admin = $this->admins_lib->get($id);
        if (!$admin || !$id) {
            redirect('backoffice/admins/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('admins/update', array('input' => $input, 'id' => $id));
        } else {
            $apps = $this->admins_lib->update($input, $id);
            if ($apps) {
                $this->session->set_flashdata('success', $this->lang->line('adminsupdate_success'));
                redirect('backoffice/admins/index');
            } else {
                $this->session->set_flashdata('error', $this->lang->line('adminsupdate_error'));
                redirect(current_url());
            }
        }
    }

    public function delete($id = null) {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->library('backoffice/admins_lib');
        if ($this->admins_lib->delete($id)) {
            $this->session->set_flashdata('success', $this->lang->line('adminsdelete_success'));
        }
        redirect('backoffice/admins/index');
    }

    public function suspend($id = null) {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->library('backoffice/admins_lib');
        if ($this->admins_lib->suspend($id, 0)) {
            $this->session->set_flashdata('success', $this->lang->line('adminssuspend_success'));
        }
        redirect('backoffice/admins/index');
    }
    public function unsuspend($id = null) {
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->library('backoffice/admins_lib');
        if ($this->admins_lib->suspend($id)) {
            $this->session->set_flashdata('success', $this->lang->line('adminsunsuspend_success'));
        }
        redirect('backoffice/admins/index');
    }
}
