<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Employee extends Admin_Core_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model', 'user');
    }

    public function view_all_employee()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_employee'] = $this->user->get_all_employee_by_id(user_id());
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/employee/view_all_employee');
        $this->load->view('admin/include/footer');
    }

    function add_new_employee()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/employee/add_new_employee');
        $this->load->view('admin/include/footer');
    }

    function add_employee()
    {
        if ($this->user->add_employee()) 
        {
            $this->session->set_userdata('success', "Employee Successfully Added");
            redirect(base_url().'admin/employee/view_all_employee');
        } else {
            $this->session->set_userdata('error', " Employee Added Failed");
            redirect($this->agent->referrer());
        }
    }

    function edit_employee($id)
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['employee'] = $this->user->get_employee_by_id($id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/employee/edit_employee');
        $this->load->view('admin/include/footer');
    }

    function update_employee($id)
    {
        if ($this->user->update_employee($id)) 
        {
            $this->session->set_userdata('success', "Employee Successfully Updated");
            redirect(base_url().'admin/employee/view_all_employee');
        } else {
            $this->session->set_userdata('error', " Employee Update Failed");
            redirect($this->agent->referrer());
        }
    }

    function delete_employee($id)
    {
        if ($this->user->delete_employee($id)) 
        {
            $this->session->set_userdata('success', "Employee Successfully Delete");
            redirect(base_url().'admin/employee/view_all_employee');
        } else {
            $this->session->set_userdata('error', " Employee Delete Failed");
            redirect($this->agent->referrer());
        }
    }


    function login($id)
    {
        $this->load->model('auth_model');
        $pw = $this->auth_model->direct_login_by_id($id);
        if(!$pw)
        {
            $error = 'Password is wrong!';
            $this->session->set_userdata('error',$error);
            redirect(base_url().'admin');
        }
        else
        {
            $rand = rand();
            $user = array(
                'sess_id' => $pw['user_id'],
                'sess_mobile' => $pw['user_mobile'],
                'sess_email' => $pw['user_email'],
                'sess_role' => $pw['user_role'],
                'sess_status' => $pw['user_status'],
                'sess_logged_in' => true,
                'sess_token' => rand(),
                'sess_app_id' => $this->config->item('app_key'),
            );
            $this->auth_model->active_token($pw['user_id'], $user['sess_token']);
            $this->session->set_userdata($user);
            redirect(base_url().'admin/admin/dashboard');
        }
    }

}
