<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('inventory_settings_model', '', TRUE);
        $this->load->model('master_settings_model', '', TRUE);
        $this->load->model('service_settings_model', '', TRUE);
        $this->load->model('report_settings_model', '', TRUE);
        $this->load->model('login_details_model', '', TRUE);
    }
    
    public function index() {
        $data = array();
        $data['users'] = $this->user_model->list_all();
        
        if (!empty($this->session->flashdata('success'))) 
            $data['success'] = $this->session->flashdata('success');
        else 
            $data['error'] = $this->session->flashdata('error');
        
        $actions = $this->actions();
        $data['add_btn']    = 'Create New User';
        $data['view_link']  = $actions['view'] . '/';
        $data['add_link']   = $actions['add'];
        $data['edit_link']  = $actions['edit'] . '/';
        $data['delete_link'] = $actions['delete'] . '/';
        
        $service_menu_id = get_master_controller_id($actions['controller']);
        $data['service_menu_id'] = array('service_menu_id' => $service_menu_id, 'menu_type' => '1');
        $this->render($data, 'user/list');
    }
    
    public function add() {
        $data = array();
        $data['inventories'] = $this->inventory_settings_model->list_active();
        $data['inventory_str'] = $this->inventory_settings_model->controller_name_str();
        $data['masters'] = $this->master_settings_model->list_active();
        $data['masters_str'] = $this->master_settings_model->controller_name_str();
        $data['services'] = $this->service_settings_model->list_active();
        $data['service_str'] = $this->service_settings_model->controller_name_str();
        $data['reports'] = $this->report_settings_model->list_active();
        $data['report_str'] = $this->report_settings_model->controller_name_str();
        $data['statues'] = $this->inventory_settings_model->statue_list_active();
        $data['statues_str'] = $this->inventory_settings_model->statue_controller_name_str();
        $actions = $this->actions();
        $data['form_action'] = $actions['insert'];
       // echo '<pre>';print_r($data);exit;
        $this->render($data, 'user/add');
    }
    
    public function insert() {
        $code = $this->user_model->get_code();
        $user = array(
            'name' => $this->input->post('name'),
            'role' => $this->input->post('role'),
            'user_name' => $this->input->post('user_name'),
            'password' => $this->input->post('password'),
            'contact_no' => $this->input->post('contact_no'),
            'status' => $this->input->post('status'),
            'pass_code' => $this->user_model->get_pass_code($code),
            'created_on' => date('Y-m-d H:i:s'),
            'created_by' => $this->user_data['user_id']
        );
        if($this->input->post('role') == 2 || $this->input->post('role') == 3) {
            $user['permissions'] = serialize($this->input->post('permissions'));
            $user['master_permissions'] = serialize($this->input->post('master'));
            $user['inventory_permissions'] = serialize($this->input->post('inventory'));
            $user['billing_permissions'] = serialize($this->input->post('billing'));
            $user['report_permissions'] = serialize($this->input->post('reports'));
            $user['statue_permissions'] = serialize($this->input->post('statue'));
        } else if($this->input->post('role') == 1) {
            $reports = 'a:3:{s:20:"daily_summary_report";a:5:{i:0;s:3:"ALL";i:1;s:1:"1";i:2;s:1:"2";i:3;s:1:"3";i:4;s:1:"4";}s:15:"prasadam_report";a:5:{i:0;s:3:"ALL";i:1;s:1:"1";i:2;s:1:"2";i:3;s:1:"3";i:4;s:1:"4";}s:24:"kattalai_archanai_report";a:5:{i:0;s:3:"ALL";i:1;s:1:"1";i:2;s:1:"2";i:3;s:1:"3";i:4;s:1:"4";}}';
            $user['billing_permissions'] = serialize($this->input->post('billing'));
            $user['permissions'] = serialize($this->input->post('permissions'));
            $user['master_permissions'] = serialize();
            $user['inventory_permissions'] = serialize();
            $user['report_permissions'] = $reports;
            $user['statue_permissions'] = serialize();
        }
        if ($this->user_model->insert($user) > 0)
            $this->session->set_flashdata('success', 'User is added successfully. Please note this pass code: <b>"'.$code.'"</b>');
        else 
            $this->session->set_flashdata('error', 'User not added!');
        
        redirect('user', 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['inventories'] = $this->inventory_settings_model->list_active();
        $data['inventory_str'] = $this->inventory_settings_model->controller_name_str();
        $data['masters'] = $this->master_settings_model->list_active();
        $data['masters_str'] = $this->master_settings_model->controller_name_str();
        $data['services'] = $this->service_settings_model->list_active();
        $data['service_str'] = $this->service_settings_model->controller_name_str();
        $data['reports'] = $this->report_settings_model->list_active();
        $data['report_str'] = $this->report_settings_model->controller_name_str();
        $data['statues'] = $this->inventory_settings_model->statue_list_active();
        $data['statues_str'] = $this->inventory_settings_model->statue_controller_name_str();
        
        $data['user'] = $this->user_model->get_by_id($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $data['user']->id;
        
        $this->render($data, 'user/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        #echo '<pre>';print_r($_POST);die;
        $user = array(
            'name' => $this->input->post('name'),
            'role' => $this->input->post('role'),
            'user_name' => $this->input->post('user_name'),
            'password' => $this->input->post('password'),
            'contact_no' => $this->input->post('contact_no'),
            'status' => $this->input->post('status'),
            'permissions' => serialize($this->input->post('permissions')),
            'master_permissions' => serialize($this->input->post('master')),
            'inventory_permissions' => serialize($this->input->post('inventory')),
            'billing_permissions' => serialize($this->input->post('billing')),
            'report_permissions' => serialize($this->input->post('reports')),
            'updated_on' => date('Y-m-d H:i:s'),
            'updated_by' => $this->user_data['user_id']
        );
        
        if($this->input->post('role') == 2 || $this->input->post('role') == 3) {
            $user['permissions'] = serialize($this->input->post('permissions'));
            $user['master_permissions'] = serialize($this->input->post('master'));
            $user['inventory_permissions'] = serialize($this->input->post('inventory'));
            $user['billing_permissions'] = serialize($this->input->post('billing'));
            $user['report_permissions'] = serialize($this->input->post('reports'));
            $user['statue_permissions'] = serialize($this->input->post('statue'));
        } else if($this->input->post('role') == 1) {
            $reports = 'a:3:{s:20:"daily_summary_report";a:5:{i:0;s:3:"ALL";i:1;s:1:"1";i:2;s:1:"2";i:3;s:1:"3";i:4;s:1:"4";}s:15:"prasadam_report";a:5:{i:0;s:3:"ALL";i:1;s:1:"1";i:2;s:1:"2";i:3;s:1:"3";i:4;s:1:"4";}s:24:"kattalai_archanai_report";a:5:{i:0;s:3:"ALL";i:1;s:1:"1";i:2;s:1:"2";i:3;s:1:"3";i:4;s:1:"4";}}';
            $user['billing_permissions'] = serialize($this->input->post('billing'));
            $user['permissions'] = serialize($this->input->post('permissions'));
            $user['master_permissions'] = serialize();
            $user['inventory_permissions'] = serialize();
            $user['report_permissions'] = $reports;
            $user['statue_permissions'] = serialize();
        }
        
        if ($this->user_model->update($id, $user) > 0) {
            $this->session->set_flashdata('success', 'User updated successfully.');
        }
        else  {
            $this->session->set_flashdata('error', 'User not updated!');
        }
        
       redirect('user', 'refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['inventories'] = $this->inventory_settings_model->list_active();
        $data['inventory_str'] = $this->inventory_settings_model->controller_name_str();
        $data['masters'] = $this->master_settings_model->list_active();
        $data['masters_str'] = $this->master_settings_model->controller_name_str();
        $data['services'] = $this->service_settings_model->list_active();
        $data['service_str'] = $this->service_settings_model->controller_name_str();
        $data['reports'] = $this->report_settings_model->list_active();
        $data['report_str'] = $this->report_settings_model->controller_name_str();
        $data['statues'] = $this->inventory_settings_model->statue_list_active();
        $data['statues_str'] = $this->inventory_settings_model->statue_controller_name_str();
        
        $data['user'] = $this->user_model->get_by_id($id);
        $this->render($data, 'user/view');
    }
    
    public function delete($id) {
        if (empty($id)) {
            show_400_error();
        }
        
        $user = array(
            'status' => 2,
            'updated_on' => date('Y-m-d H:i:s'),
            'updated_by' => $this->user_data['user_id']
        );
        
        if ($this->user_model->update($id, $user) > 0)
            $this->session->set_flashdata('success', 'User deleted successfully');
        else 
            $this->session->set_flashdata('error', 'User not deleted!');
        
        redirect('user', 'refresh');
    }
    
    public function user_name_exists() {
        $data = array();
        $data['user_name_exists'] = $this->user_model->user_name_exists($this->input->post('user_name'), $this->input->post('user_id'));
        
        $this->ajax_response($data);
    }
    
    function profile($user_id = '') {
        
        $segments = $this->uri->segments;
        $controller = $segments[1];
        $actions = $this->actions();
        $post = $this->input->post();
        if( empty( $user_id ) ) :
            $user = $this->user_model->get_by_id();
            $user_id = $user->id;
        endif;
        
        $session_data = $this->session->userdata(TBL_PREFIX . 'user_logged_in');
        $token = $this->session->userdata( 'rtoken-'.$session_data['user_id'] );
        
        $data = array(
            'title'             => 'Edit Profile',
            'controller'        => $controller,
            'actions'           => $actions,
            'form_action'       => base_url() . $controller .'/profile/' . $user_id,
            'success'           => $this->session->flashdata('success'),
            'error'             => $this->session->flashdata('error'),
            
            'token'             => $this->auto_generation_code( rand( 999, 9999 ) ),
            
        );
        
        $this->session->set_userdata( 'rtoken-'.$session_data['user_id'], $data['token'] );
        
        if( !empty( $post['submit'] ) && $post['token'] == $token ) :
            // insert
            $insert_id = $this->user_model->profile($user_id);

            #$this->session->set_flashdata('success', "Information is updated successfully");
            $data['success'] = 'Information is updated successfully';
            // Redirect
           //redirect( base_url() . $controller . '/profile/'.$user_id );
        endif;

        $data['user']  = $this->user_model->get_by_id( $user_id ); 
        // Load Views
        $this->render( $data, 'user/profile' );
    }
}
?>