<?php

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

class EmployeeSetup extends OWN_Controller {

    function __construct() {
        parent::__construct();
        $this->admin_login_check();
        $this->load->model('employeemodel/employeemodel', 'employeemodel');
        $this->load->model('landcustomerregistrationmodel/landcustomerregistrationmodel', 'CREGModel');
    }

    public function viewEmployeeList(){
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $data['allEmployee']=$this->employeemodel->getAllEmployee();
            if(!empty($data['allEmployee']))
            {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/employee/viewEployeelist',$data);
                $this->load->view('common/footer');
            }
            else
            {
                $errormessage['errormessage']=" In This Time There Are No Employee Available";
                $this->session->set_userdata($errormessage);
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('dashbord');
                $this->load->view('common/footer');
            }
        }
        else{
            redirect(base_url('admin','refresh'));
        }
    }

    public function edit_employee($userId,$logic="")
    {
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $data['employeeByid']=$this->employeemodel->getEmployeeById($userId);
            if(!empty($data['employeeByid']))
            {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                if(!empty($logic))
                {
                    //echo $logic;die();
                    $this->load->view('admin/employee/ChangeOwnProfile',$data);
                }
                else{
                    $this->load->view('admin/employee/edit_employee',$data);
                } 
                $this->load->view('common/footer');
            }
            else
            {
                redirect(admin_url().'/employeesetup/viewEmployeeList','refresh');
            }
        }
        else
        {
            redirect(base_url('admin','refresh'));
        }

    }

    public function update_employee(){
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            if(isset($_POST['btnUpdate'])) {

                    $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|valid_email'
                        ),
                       
                        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);
                    $data = $this->input->post('data', TRUE);
                   // $data['userId']=$this->input->post('userId', TRUE);
                   $userId=$this->input->post('userId', TRUE);
                    $yes_upload = $this->upload->do_upload('photo');
                    $img_data = $this->upload->data();

                    if (empty($img_data['file_name'])) {                
                        unset($img_data['file_name']);
                    } else {

                        $data['photo'] = "uploadimage/userImage/".$img_data['file_name']; 
                    }
                    $execution=$this->employeemodel->updateEmployeeById($userId,$data);
                    //print_r($execution[0]);die();
                    if(!empty($execution[0]) || !empty($execution[1]))
                    {
                        $message['message']="Employee Info Updated";
                        $this->session->set_userdata($message);
                        redirect(admin_url().'/employeesetup/viewEmployeeList','refresh');
                    }
                    else
                    {
                        $errormessage['errormessage']="Employee Info Not Updated";
                        $this->session->set_userdata($errormessage);
                        redirect(admin_url().'/employeesetup/viewEmployeeList','refresh');
                    }
              }
              else
              {
                    redirect(admin_url().'/employeesetup/viewEmployeeList','refresh');
              }
        }
        else
        {
            redirect(base_url('admin','refresh'));
        }
    }

    public function delete_employee($userId) 
    {
        $editdata=$this->employeemodel->getEmployeeById($userId);
        if(!empty($editdata))
        {
            $dlt_file=$editdata['photo']; 
             if(file_exists($dlt_file))
                {                 
                    unlink($dlt_file);
                }
            $execution=$this->employeemodel->deleteemployeeById($userId);
            if(!empty($execution))
            {
                $message['message']="Employee Info deleted";
                $this->session->set_userdata($message);
                redirect(admin_url().'/employeesetup/viewEmployeeList','refresh');
            }
            else
            {
                $errormessage['errormessage']="Employee Not Deleted";
                $this->session->set_userdata($errormessage);
                redirect(admin_url().'/employeesetup/viewEmployeeList','refresh');
            } 
        }
        else
        {
            $errormessage['errormessage']="Employee Not Found";
            $this->session->set_userdata($errormessage);
            redirect(admin_url().'/employeesetup/viewEmployeeList','refresh');
        }
    }

    public function view_profile($userId) 
    {
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $data['employeeById']       =   $this->employeemodel->getEmployeeById($userId);
            $user_id                    =   $data['employeeById']['userId'];
            $data['allLandcustomer']    =   $this->CREGModel->getRef($user_id);


            if(!empty($data['employeeById']))
            {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/employee/viewEmployeeprofile',$data);
                $this->load->view('admin/employee/number_of_customer',$data);
                $this->load->view('common/footer');
            }
            else
            {
                $errormessage['errormessage']="There are No Profile Found for this employee";
                $this->session->set_userdata($errormessage);
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('dashbord');
                $this->load->view('common/footer');
                //redirect(admin_url().'/employeesetup/viewEmployeeList','refresh');
            }
        }
        else
        {
             redirect(base_url('admin','refresh'));
        }
    }

    public function change_employee_profile()
    {
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $this->load->helper(array('form', 'url'));
                $this->load->library('form_validation');
                $config = array(
                        
                        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|callback__check_phone|max_length[13]|min_length[11]'
                        ),
                        array(
                            'field' => 'data[email]',
                            'label' => 'Email',
                            'rules' => 'trim|xss_clean|required'
                        ),
                       
                        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'
                        )
                    );
            $this->form_validation->set_rules($config);
            if ($this->form_validation->run() == FALSE) {

                 //echo '{"status": "3", "nationalId": "' . form_error('data[nationalId]') . '", "mobile": "' . form_error('data[mobile]') . '", "presentAddress" : "' . form_error('data[presentAddress]') . '", "parmanentAddress" : "' . form_error('data[parmanentAddress]') . '", "joiningdate" : "' . form_error('data[joiningdate]') . '", "email" : "' . form_error('data[email]') . '", "gender" : "' . form_error('data[gender]') . '", "religion" : "' . form_error('data[religion]') . '", "motherName" : "' . form_error('data[motherName]') . '", "fatherName" : "' . form_error('data[fatherName]') . '" }';
                  echo json_encode(array('status'=>3, 'message' => validation_errors()));
                  exit(3);
                // echo(json_encode(array('status'=>3,'config'=>$config,"validate"=>validation_errors())));
                // exit(3);
                
            }
            else
            {
                $data=$this->input->post('data',true);
               // print_r($data);

                $data['nationalId']=$_POST['nationalId'];
                $data['userId']=$_POST["userId"];
                $data['mobile']=$_POST["mobile"];
                $data['email']=$_POST["email"];
                $data['gender']=$_POST["gender"];
                $data['fatherName']=$_POST["fatherName"];
                $data['motherName']=$_POST["motherName"];
                $data['parmanentAddress']=$_POST["parmanentAddress"];
                $data['presentAddress']=$_POST["presentAddress"];
                if(!empty($data)){
                    $execution=$this->employeemodel->updateOwnProfile($data);
                    if(!empty($execution) || $execution!=null){
                        $message['message']="Your info updated";
                         $this->session->set_userdata($message);
                        //redirect(admin_url().'/employeesetup/viewEmployeeList','refresh');
                         echo "1";
                    }else{
                        echo "2";
                    }

                }
            }
        }

    }

    public function viewInactiveUserList(){
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {
            $data['allEmployee'] = $this->employeemodel->getAllInactiveEmployee();
            $data['inactive']    = 1;
            if(!empty($data['allEmployee']))
            {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/employee/viewEployeelist',$data);
                $this->load->view('common/footer');
            }
            else
            {
                $errormessage['errormessage']   =  " In This Time There Are No Employee Available";
                $this->session->set_userdata($errormessage);
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('dashbord');
                $this->load->view('common/footer');
            }
        }
        else{
            redirect(base_url('admin','refresh'));
        }
    }

    public function activeUser($id=''){
        if (!empty($id) && is_numeric($id)){
            $res = $this->employeemodel->makeUserActiveById($id);
            redirect(base_url('admin/employeesetup/viewEmployeeList','refresh'));
        }else{
            redirect(base_url('admin/home','refresh'));
        }
    }
    
    public function allleaves() 
    {
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $data['allLeaves']          =   $this->employeemodel->getEmployeeAllLeaves();
            $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
            
            // echo print_r($data['allLeaves']);
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/employee/viewEmployeeallleaves',$data);
            $this->load->view('common/footer');
            
        }
        else
        {
             redirect(base_url('admin','refresh'));
        }
    }
    
    public function leaves($userId) 
    {
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $data['employeeById']       =   $this->employeemodel->getEmployeeById($userId);
            $user_id                    =   $data['employeeById']['userId'];
            $data['allLeaves']          =   $this->employeemodel->getEmployeeLeaves($userId);


            if(!empty($data['employeeById']))
            {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/employee/viewEmployeeleaves',$data);
                $this->load->view('common/footer');
            }
            else
            {
                $errormessage['errormessage']="There are No Profile Found for this employee";
                $this->session->set_userdata($errormessage);
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('dashbord');
                $this->load->view('common/footer');
                //redirect(admin_url().'/employeesetup/viewEmployeeList','refresh');
            }
        }
        else
        {
             redirect(base_url('admin','refresh'));
        }
    }
    
    public function addLeave() {
        // $data['employeeById']       =   $this->employeemodel->getEmployeeById($userId);
        //     $user_id                    =   $data['employeeById']['userId'];
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $config = array(
            
            array(
                'field' => 'data[lv_employee]',
                'label' => 'lv_employee',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[lv_type]',
                'label' => 'lv_type',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[lv_start]',
                'label' => 'Date',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[lv_days]',
                'label' => 'lv_days',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[lv_number]',
                'label' => 'lv_number',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[lv_note]',
                'label' => 'lv_note',
                'rules' => 'required|xss_clean|trim'
            )
        );
        
        // $this->form_validation->set_rules($config);
        // if ($this->form_validation->run() == false) {
        //     $errormessage['errormessage'] = "Leave information missing......!";
        //     $this->session->set_userdata($errormessage);
        //     $this->load->view('common/header');
        //     $this->load->view('common/menu');
        //     $this->load->view('admin/employee/viewEmployeeleaves',$data);
        //     $this->load->view('common/footer');
        // } else {
            $data = $this->input->post('data', true);
            
        //     echo "<pre>";
        // echo print_r($data);
        // echo "</pre>";

            
            // if (!empty($data) && $data['userId'] != null) {
                $execution = $this->employeemodel->insert_leaves($data);
                if (!empty($execution)) {                   
                    
                    $message['message'] = "New Leave saved!";
                    $this->session->set_userdata($message);

                    //$this->session->set_userdata($user);
                    
                    //redirect(admin_url() . '/employeesetup/leaves/'.$data['lv_employee'], 'refresh');
                    redirect(admin_url() . '/employeesetup/allleaves', 'refresh');


                } else {
                    // $errormessage['errormessage'] = "Leave Not Saved......!";
                    // $this->session->set_userdata($errormessage);
                    // redirect(admin_url() . '/employeesetup/leaves/'.$data['userId'], 'refresh');
                }
                
            // } else {
            //     $errormessage['errormessage'] = "Leave Information Missing......!";
            //     $this->session->set_userdata($errormessage);
            //     redirect(admin_url() . '/employeesetup/leaves/'.$data['userId'], 'refresh');
            // }
        // }
    }
    
    
        public function edit_leave($leaveid)
    {
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $data['LeaveByid']=$this->employeemodel->getLeavesById($leaveid);
            if(!empty($data['LeaveByid']))
            {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/employee/edit_leave',$data);
                $this->load->view('common/footer');
            }
            else
            {
                redirect(admin_url().'/employeesetup/viewEmployeeList','refresh');
            }
        }
        else
        {
            redirect(base_url('admin','refresh'));
        }

    }

    public function update_leave(){
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            // if(isset($_POST['btnUpdate'])) {

                    $this->load->helper(array('form', 'url'));
                    $this->load->library('form_validation');
                    $config = array(
                        
                        array(
                            'field' => 'data[lv_type]',
                            'label' => ' Leave Type',
                            'rules' => 'required|xss_clean|trim'
                        ),
                        array(
                            'field' => 'data[lv_start]',
                            'label' => 'Leave Start',
                            'rules' => 'trim|xss_clean'
                        ),
                        array(
                            'field' => 'data[lv_days]',
                            'label' => 'Leave Days',
                            'rules' => 'xss_clean|trim'
                        ),
                        array(
                            'field' => 'data[lv_number]',
                            'label' => 'Leave Number',
                            'rules' => 'xss_clean|trim'
                        ),
                        array(
                            'field' => 'data[lv_note]',
                            'label' => 'Note',
                            'rules' => 'xss_clean|trim'
                        ),
                        array(
                            'field' => 'data[lv_year]',
                            'label' => 'Note',
                            'rules' => 'xss_clean|trim'
                        )
                    );
                $lv_id=$this->input->post('lv_id', TRUE);
                $lv_employee=$this->input->post('lv_employee', TRUE);  
                $data = $this->input->post('data', true);
                    //echo $lv_employee;
                if(!empty($data)){
                    $execution=$this->employeemodel->updateLeaveById($lv_id,$data);
                    if(!empty($execution) || $execution!=null){
                        $message['message']="Your info updated";
                        $this->session->set_userdata($message);
                        redirect(admin_url() . '/employeesetup/allleaves', 'refresh');
                        //redirect(admin_url().'/employeesetup/leaves/'.$lv_employee,'refresh');
                         
                    }else{
                        $errormessage['errormessage']="Leave Info Not Updated";
                        $this->session->set_userdata($errormessage);
                        redirect(admin_url() . '/employeesetup/allleaves', 'refresh');
                        //redirect(admin_url().'/employeesetup/leaves/'.$lv_employee,'refresh');
                    }

                }

        // }else{
        //     //redirect(base_url('admin','refresh'));
        }
    }

    public function delete_leave($leaveid) 
    {
        $leavedata=$this->employeemodel->getLeavesById($leaveid);
        
        if(!empty($leavedata))
        {
            
           
            $execution=$this->employeemodel->deleteleaveById($leaveid);
            if(!empty($execution))
            {
                $message['message']="Leave Info deleted";
                $this->session->set_userdata($message);
                redirect(admin_url() . '/employeesetup/allleaves', 'refresh');
            //    redirect(admin_url().'/employeesetup/leaves/'.$leavedata['lv_employee'],'refresh');
            }
            else
            {
                $errormessage['errormessage']="Leave Not Deleted";
                $this->session->set_userdata($errormessage);
                redirect(admin_url() . '/employeesetup/allleaves', 'refresh');
            //    redirect(admin_url().'/employeesetup/leaves/'.$leavedata['lv_employee'],'refresh');
            } 
        }
        else
        {
            $errormessage['errormessage']="Leave Not Found";
            $this->session->set_userdata($errormessage);
            redirect(admin_url() . '/employeesetup/allleaves', 'refresh');
            //  redirect(admin_url().'/employeesetup/leaves/'.$leavedata['lv_employee'],'refresh');
        }
        
        
    }


    
}