<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Employee extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('employee_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->employee_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 Employee';
        $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, 'employee/list');
    }
    
    private function get_employee_url_key($name, $id = '') {
        $url_key = get_url_key($name);
        if ($this->employee_model->employee_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_employee_url_key($name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function add() {
        $data = array();
        $actions = $this->actions();
        $data['form_action'] = $actions['insert'];
        $this->render($data, 'employee/add');
    }
    
    public function insert() {
        $insert = array(
            'name' => $this->input->post('name'),
            'designation' => $this->input->post('designation'),
            'dob'=> date('Y-m-d',strtotime($this->input->post('dob'))),
            'address' => $this->input->post('address'),
            'passport_no' => $this->input->post('passport_no'),
            'passport_valid_to' => date('Y-m-d',strtotime($this->input->post('passport_valid_upto'))),
            'work_permit_no' => $this->input->post('work_permit_no'),
            'security_bond_no' => $this->input->post('security_bond_no'),
            'date_of_app'=> date('Y-m-d',strtotime($this->input->post('date_of_app'))),
            'date_of_join' => date('Y-m-d',strtotime($this->input->post('date_of_join'))),
            'date_of_expired' => date('Y-m-d',strtotime($this->input->post('date_of_expire'))),
            'date_of_renewal' => date('Y-m-d',strtotime($this->input->post('date_of_renewal'))),
            'hps' => $this->input->post('hps'),
            'hpi' => $this->input->post('hpi'),
            'wci_insurance_no' => $this->input->post('wci_no'),
            'insurance_valid_to' => date('Y-m-d',strtotime($this->input->post('insurance_valid'))),
            'url_key' => $this->get_employee_url_key($this->input->post('name')),
            'status' => $this->input->post('status'),
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        // Upload Photo
        $upload = $this->upload(EMP_DIR);
        $insert['image'] = $upload['file_name'];
        
        $insert_id = $this->employee_model->insert($insert);  
        if ($insert_id > 0)
            $this->session->set_flashdata('success', 'Employee details added successfully');    
        
        else
            $this->session->set_flashdata('error', 'Employee details not added!');
        redirect('employee', 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['employee'] = $this->employee_model->get_by_id($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        $this->render($data, 'employee/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $update = array(
            'name' => $this->input->post('name'),
            'designation' => $this->input->post('designation'),
            'dob'=> date('Y-m-d',strtotime($this->input->post('dob'))),
            'address' => $this->input->post('address'),
            'passport_no' => $this->input->post('passport_no'),
            'passport_valid_to' => date('Y-m-d',strtotime($this->input->post('passport_valid_upto'))),
            'work_permit_no' => $this->input->post('work_permit_no'),
            'security_bond_no' => $this->input->post('security_bond_no'),
            'date_of_app'=> date('Y-m-d',strtotime($this->input->post('date_of_app'))),
            'date_of_join' => date('Y-m-d',strtotime($this->input->post('date_of_join'))),
            'date_of_expired' => date('Y-m-d',strtotime($this->input->post('date_of_expire'))),
            'date_of_renewal' => date('Y-m-d',strtotime($this->input->post('date_of_renewal'))),
            'hps' => $this->input->post('hps'),
            'hpi' => $this->input->post('hpi'),
            'wci_insurance_no' => $this->input->post('wci_no'),
            'insurance_valid_to' => date('Y-m-d',strtotime($this->input->post('insurance_valid'))),
            'url_key' => $this->get_employee_url_key($this->input->post('name')),
            'status' => $this->input->post('status'),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        
        // Upload Photo
        $upload = $this->upload(EMP_DIR);
        if( !empty( $upload['file_name'] ) ) :
            // New Photo
            $update['image'] = $upload['file_name'];
        
            $prev_img = $this->input->post('prev-image');    
            if( !empty( $prev_img ) ) :
                unlink( EMP_DIR . $prev_img );
            endif;
        endif;
        if( !empty( $this->input->post('rm-image') ) ) :
            // Remove Image
            $update['image'] = null; 
            unlink( EMP_DIR . $this->input->post('rm-image') );
        endif;
        
        if ( $this->employee_model->update($update, $id) > 0)
            $this->session->set_flashdata('success', 'Employee details updated successfully');  
       
        else
            $this->session->set_flashdata('error', 'Employee details not updated!');    
        
        redirect('employee','refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['employee'] = $this->employee_model->get_by_id($id);
        $this->render($data, 'employee/view');
    }
    
    public function delete($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $update = array ( 
            'status' => 2,
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        if ($this->employee_model->update($update,$id) > 0)
            $this->session->set_flashdata('success', 'Employee details deleted successfully');
            
        else 
            $this->session->set_flashdata('error', 'Employee details not deleted!');
        
        redirect( 'employee', 'refresh');
    }
    
    function upload( $path = '', $field = 'image' ) {
        $file = $_FILES[$field];
        $path = !empty( $path ) ? $path : UPLOADS;
        if( empty( $file['name'] ) ) :
            return '';
        endif;
        
        // Load upload library
        if( empty( $this->upload ) ) :
            $this->load->library( 'upload' );
        endif;
        
        $config = array(
            'file_name'     => time().'_'.$file['name'],
            'upload_path'   => $path,
            'allowed_types' => 'png|jpg|bmp|jpeg|ico',
            
        );
        
        if( !file_exists( $config['upload_path'] ) ) :
            mkdir( $config['upload_path'], 0777 );
        endif;
        
        $this->upload->initialize( $config );
        $this->upload->do_upload( $field );
        
        return $this->upload->data();
        
    }
    
    public function check_fields_exists() {
        $data = $this->employee_model->check_fields_exists($_POST['field_name'],$_POST['val'],$_POST['id']);
        echo json_encode($data);
    }
}
?>