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

class user extends MY_Controller{
	function __construct(){
        parent::__construct();
        $this->load->model('user_model', '', TRUE);
        // if($this->data['userid']==''){
        //     redirect($this->data['base_url'] . 'home');   
        //     exit;
        // }
    }
    function index(){
        redirect($this->data['base_url'] . 'user/profile');   
        $this->data['user_details'] = $this->user_model->alluser_details();
        $this->data['message'] = $this->session->flashdata('message');
        $this->breadcrumb->add('Dashboard', $this->data['base_url']);
        $this->data['breadcrumb'] = $this->breadcrumb->output();
        $this->data['tpl_name']= "user.tpl";
        $this->smarty->assign('data', $this->data);
        $this->smarty->view('template.tpl');  
    } 
    function create(){
        if($this->input->post()){            
            $data = $this->input->post('data');
            $data['status']='1';
            $data['is_delete']='0';
            $data['pics']='';
            $data['create_datetime']=date('Y-m-d h:i:s');
            $data['created_ip']=$_SERVER['REMOTE_ADDR'];
            $userdata = $this->user_model->adduser($data);
            if($userdata['msg']=='alreadyexist'){
                $this->session->set_flashdata('message',"Email already exist");
                redirect($this->data['base_url'] . 'user/');
            }else{
                if($_FILES['pics']['name'] != ''){
                    $size['width']=100;
                    $size['height']=100;    
                    $img_uploaded=$this->do_upload_img('users','pics',$size);
                    $imgdata=array();
                    $imgdata['pics'] = $img_uploaded;
                    $imgdata['id'] = $userdata['msg'];
                    $edituser = $this->user_model->edituser($imgdata);
                }
                $this->session->set_flashdata('message',"user Registered Successfully.");
                redirect($this->data['base_url'] . 'user/');
            }
        } 
        $this->data['message'] = $this->session->flashdata('message');
      
        $this->data['breadcrumb']='New user';
        $this->data['tpl_name']= "user_create.tpl";
        $this->smarty->assign('data', $this->data);
        $this->smarty->view('template.tpl');  
    }
    function update(){
        if($this->input->post()){            
            $data = $this->input->post('data');
            $data['status']='1';
            $data['is_delete']='0';
            $data['created_ip']=$_SERVER['REMOTE_ADDR'];
            $userdata = $this->user_model->edituser($data);
            if($_FILES['pics']['name'] != ''){
                $size['width']=100;
                $size['height']=100;    
                $img_uploaded=$this->do_upload_img('users','pics',$size);
                $imgdata=array();
                $imgdata['pics'] = $img_uploaded;
                $imgdata['id'] = $data['id'];
                $edituser = $this->user_model->edituser($imgdata);
            }
            $this->session->set_flashdata('message',"User Update Successfully.");
            redirect($this->data['base_url'] . 'user/');            
        } 
        $userid = $this->uri->segment(3);
        $this->data['user_details'] = $this->user_model->single_user_details($userid);
        $this->data['message'] = $this->session->flashdata('message');
        $this->data['breadcrumb']='Edit user';
        $this->data['tpl_name']= "user_create.tpl";
        $this->smarty->assign('data', $this->data);
        $this->smarty->view('template.tpl');  
    }
    function delete(){
        $userid = $this->uri->segment(3);
        if($userid!=''){            
            $data['is_delete'] = '1';
            $data['id'] = $userid;
            $edituser = $this->user_model->edituser($data);
            $this->session->set_flashdata('message',"User delete Successfully.");
            redirect($this->data['base_url'] . 'user/');
        }else{
             redirect($this->data['base_url'] . 'user/');
        }
    } 
    function profile(){
        if($this->input->post()){            
            $data = $this->input->post('data');
            $data['status']='1';
            $data['is_delete']='0';
            $data['created_ip']=$_SERVER['REMOTE_ADDR'];
            $userdata = $this->user_model->edituser($data);
            if($_FILES['pics']['name'] != ''){
                $size['width']=100;
                $size['height']=100;    
                $img_uploaded=$this->do_upload_img('users','pics',$size);
                $imgdata=array();
                $imgdata['pics'] = $img_uploaded;
                $imgdata['id'] = $data['id'];
                $edituser = $this->user_model->edituser($imgdata);
            }
            $this->session->set_flashdata('message',"User Update Successfully.");
            redirect($this->data['base_url'] . 'dashbord/');            
        }
        $this->data['message'] = $this->session->flashdata('message');
        $this->data['user_details'] = $this->user_model->single_user_details($this->data['userid']);
        $this->data['breadcrumb']='Profile';
        $this->data['tpl_name']= "profile.tpl";
        $this->smarty->assign('data', $this->data);
        $this->smarty->view('template.tpl');  
    }
      
}
