<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Service_settings extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('service_settings_model', '', TRUE);
    }
    
    public function index() {
        $data = array();
        if (!empty($this->session->flashdata('success'))) 
            $data['success'] = $this->session->flashdata('success');
        else 
            $data['error'] = $this->session->flashdata('error');
        
        $data['setting'] = $this->service_settings_model->list_all();
        $actions = $this->actions();
        $data['form_action'] = $actions['update'];
        $data['view_link'] = $actions['view'];
        $data['edit_link'] = $actions['index'];
        
        $this->render($data, 'service_settings/edit');
    }
    
    public function update() {
        $service_id = $this->input->post('service_name');
        $name       = $this->input->post('name');
        $tamil_name = $this->input->post('tamil_name');
        $seq_no = $this->input->post('seq_no');
        $service_id = (!empty($service_id)) ? $service_id : array();
        $files = $_FILES;
        foreach($service_id as $key=>$id){
            $update = array (
                'status'     => 1,
                'tamil_name' => htmlspecialchars( $tamil_name[$id] ),
                'seq_number' => $seq_no[$id],
                'updated_on' => date('Y-m-d H:i:s'),
                'updated_by' => $this->user_data['user_id']
            );
            
            if( !empty( $name[$id] ) ) :
                $update['service_name'] = $name[$id];
            endif;
            
            
            $service = $this->service_settings_model->get_by_id($id);
            
            if(!empty($files['image']['name'][$id])){
                $_FILES['image']['name']= $files['image']['name'][$id];
                $_FILES['image']['type']= $files['image']['type'][$id];
                $_FILES['image']['tmp_name']= $files['image']['tmp_name'][$id];
                $_FILES['image']['error']= $files['image']['error'][$id];
                $_FILES['image']['size']= $files['image']['size'][$id]; 
                
                $config['upload_path'] = FCPATH . DIR_SERVICE_IMAGE;
                $config['allowed_types'] ='jpg|jpeg|png|jif|bmp';
                
                $config['file_name'] = $_FILES['image']['name'];
                $this->upload->initialize($config);
                
                $service = $this->service_settings_model->get_by_id($id);
                if(!empty($service->image)) {
                    unlink( FCPATH . DIR_SERVICE_IMAGE . $service->image);
                }
                
                $this->upload->do_upload('image');
                $update['image'] = $_FILES['image']['name'];		
			}
            
            $this->service_settings_model->update($update,$id); 
        }

        
        $setting = $this->service_settings_model->get_data($service_id);
        foreach($setting as $key=>$value){
            $update = array (
                'status' => 0,
                'updated_on' => date('Y-m-d H:i:s'),
                'updated_by' => $this->user_data['user_id']
            );
            
            $del_service = $this->service_settings_model->get_by_id($value->id);
            
            $update['image'] = '';
            if (!empty($del_service->image)) {
                if (file_exists(FCPATH . DIR_SERVICE_IMAGE . $del_service->image)) {
                    unlink(FCPATH . DIR_SERVICE_IMAGE . $del_service->image);
                }
            }
            $this->service_settings_model->update($update,$value->id);
        }
        
        /* $actions = $this->actions();
        $service_menu_id = 4; //Service Settings      
        add_login_details($args = array( 'page_type_id' => '2', 'service_menu_id' => $service_menu_id, 'menu_type' => '2', 'auto_id' => '')); */
        
        redirect('service_settings', 'refresh');
        
    }
}
?>