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

class UserRegistration extends OWN_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    function __construct() {
        parent::__construct();
        $this->admin_login_check();
        $this->load->model('userregistrationmodel/userregistrationmodel', 'UREGModel');
    }

    public function index() {
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/user/userRegistration');
            $this->load->view('common/footer');
        }
        else{
            redirect(base_url('admin','refresh'));
        }
    }

    public function insert_user(){
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            
              if(isset($_POST['btnSave']))
              {
                $this->load->helper(array('form', 'url'));
                $this->load->library('form_validation');
                $config = array(
                        array(
                            'field' => 'data[userName]',
                            'label' => ' Name',
                            'rules' => 'required|xss_clean|trim|alpha_spaces'
                        ),
                        array(
                            'field' => 'data[nationalId]',
                            'label' => 'National Id',
                            'rules' => 'trim|xss_clean'
                        ),
                        array(
                            'field' => 'data[fatherName]',
                            'label' => 'Father Name',
                            'rules' => 'xss_clean|trim|alpha_spaces'
                        ),
                        array(
                            'field' => 'data[motherName]',
                            'label' => 'Mother Name',
                            'rules' => 'xss_clean|trim|alpha_spaces'
                        ),
                       
                        array(
                            'field' => 'data[mobile]',
                            'label' => 'Mobile',
                            'rules' => 'xss_clean|trim'
                        ),
                        array(
                            'field' => 'data[email]',
                            'label' => 'Email',
                            'rules' => 'trim|xss_clean'
                        ),
                       
                        array(
                            'field' => 'data[presentAddress]',
                            'label' => 'Present Address',
                            'rules' => 'required|xss_clean|trim'
                        ),
                        array(
                            'field' => 'data[parmanentAddress]',
                            'label' => 'Permanent Address',
                            'rules' => 'required|xss_clean|trim'
                        ),
                        array(
                            'field' => 'data[joiningdate]',
                            'label' => 'joining date',
                            'rules' => 'trim|xss_clean'
                        ),
                        
                        array(
                            'field' => 'data[religion]',
                            'label' => 'Religion',
                            'rules' => 'xss_clean|trim|numeric'
                        ),
                        array(
                            'field' => 'data[gender]',
                            'label' => 'Gender',
                            'rules' => 'trim|xss_clean|numeric'
                        ),
                        array(
                            'field' => 'data[userType]',
                            'label' => 'User Type',
                            'rules' => 'trim|xss_clean|numeric'
                        )
                    );

                    $config['upload_path'] = './uploadimage/userImage/';
                    $config['allowed_types'] = 'png|jpeg|jpg|gif';
                    $config['max_size'] = '800';
                    $config['max_width'] = '800';
                    $config['max_height'] = '600';
                    $this->load->library('upload', $config);
                    $yes_upload = $this->upload->do_upload('photo');
            //
                    $this->form_validation->set_rules($config);
                    if ($this->form_validation->run() == FALSE) {
                        $this->load->view('common/header');
                        $this->load->view('common/menu');
                        $this->load->view('admin/user/userRegistration');
                        $this->load->view('common/footer');
                    } elseif (!$yes_upload) {
                        $errormessage['errormessage'] = "Image Not Uploaded";
                        $this->session->set_userdata($errormessage);
                        $this->index();
                    } else {
                        $data = $this->input->post('data', TRUE);
                        $img_data = $this->upload->data();
                        $data['photo'] = "uploadimage/userImage/" . $img_data['file_name'];
                        //------------------- generate userId-------------------
                            $year=date("Y");
                            $lastTwoNumber=substr($year,-2);
                            $mdate=date('md');
                            $data['userId'] =$lastTwoNumber.$mdate.UseridGenerate(); 
                            //print_r($data['userId']);die();
                            /*$new_stt = sprintf('%02d', $data['religion']);
                            $datae = date('Ymd');
                            $data['userId'] = $datae . "-" . $new_stt .UseridGenerate();*/
                            //$data['userId'] = $datae . "-" . $new_stt . "-" . UseridGenerate();
                        //------------------------------------------------------
                            if(!empty($data)){
                            if($data['userType']==1 || $data['userType']==2 || $data['userType']==3)
                            {
                                if($data['userType']==1){ $admin_name='admin';}elseif($data['userType']==2){ $admin_name='employee';}else{$admin_name='Director';}
                                if($data['userType']==1){ $userName='administrator';}elseif($data['userType']==2){ $userName='employee';}else{$userName='subadmin';}
                                $password=strtoupper(substr($data['userName'],-2)).$data['userId'] ;
                                if($data['userType']==1){ $admin_level=1;}elseif($data['userType']==2){ $admin_level=2;}else{$admin_level=3;}
                                $datax=array(
                                      'employeeId'=>$data['userId'],
                                      'admin_name'=>$admin_name,
                                      'admin_email'=>$data['email'],
                                      'userName'=>$userName,
                                      'password'=>$password,
                                      'admin_level'=>$admin_level,
                                      'admin_access'=>1,
                                      'status'=>1
                                    );
                                $excution1=$this->UREGModel->saveAccessInfo($datax); 
                                $excution2=$this->UREGModel->saveUserInfo($data);
                                if(!empty($excution1) && !empty($excution2))
                                {
                                    $message['message']=$admin_name." Registration Succesfully Complete!</br></br><p><b>Your Id = ".$data['userId'].", Username = ".$datax['userName']." ,Password = ".$password."</b></p>";
                                    $this->session->set_userdata($message);
                                    redirect(admin_url().'/userRegistration/index','refresh');
                                }
                                else
                                {
                                     $errormessage['errormessage']="Registration Not complete......!";
                                    $this->session->set_userdata($errormessage);
                                    redirect(admin_url().'/userRegistration/index','refresh');
                                }

                            }
                            else{
                                
                                     $errormessage['errormessage']="User Type Is Not Valid";
                                    $this->session->set_userdata($errormessage);
                                    redirect(admin_url().'/userRegistration/index','refresh');
                            }
                        }
                        else{
                            $errormessage['errormessage']="User Information Missing......!";
                            $this->session->set_userdata($errormessage);
                            redirect(admin_url().'/userRegistration/index','refresh');
                    
                        }
                    }
              }
              else
              {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/user/userRegistration');
                $this->load->view('common/footer');
                
              }
        }
        else{
            redirect(base_url('admin','refresh'));
        }
    }
}