<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Service extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('service_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->service_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 Service';
        $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, 'service/list');
    }
    
    private function get_service_url_key($service_name, $id = '') {
        $url_key = get_url_key($service_name);
        if ($this->service_model->service_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_service_url_key($service_name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function add() {
        $data = array();
        $actions = $this->actions();
        $data['form_action'] = $actions['insert'];
        $data['service_menus'] = $this->service_model->list_service_menus();
        $this->render($data, 'service/add');
    }
    
    public function insert() {
        $insert = array(
            'menu_name' => $this->input->post('menu_id'),
            'name' => $this->input->post('service_name'),
            'tamil_name'=> htmlspecialchars( $this->input->post('tamil_service_name') ),
            'amount' => $this->input->post('amount'),
            'prefix' => $this->input->post('prefix'),
            'gst' => $this->input->post('gst'),
            'url_key' => $this->get_service_url_key($this->input->post('service_name')),
            'status' => $this->input->post('status'),
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $insert_id = $this->service_model->insert($insert);  
        if ($insert_id > 0)
            $this->session->set_flashdata('success', 'Service added successfully');    
        
        else
            $this->session->set_flashdata('error', 'Service not added!');
        redirect('service', 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['service_menus'] = $this->service_model->list_service_menus();
        $data['service'] = $this->service_model->get_service_by_id($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        $this->render($data, 'service/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $update = array(
            'menu_name' => $this->input->post('menu_id'),
            'name' => $this->input->post('service_name'),
            'tamil_name'=> htmlspecialchars( $this->input->post('tamil_service_name') ),
            'amount' => $this->input->post('amount'),
            'prefix' => $this->input->post('prefix'),
            'gst' => $this->input->post('gst'),
            'url_key' => $this->get_service_url_key($this->input->post('service_name')),
            'status' => $this->input->post('status'),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        
        if ( $this->service_model->update($update, $id) > 0)
            $this->session->set_flashdata('success', 'Service updated successfully');  
       
        else
            $this->session->set_flashdata('error', 'Service not updated!');    
        
        redirect('service','refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['service'] = $this->service_model->get_service_by_id($id);
        $this->render($data, 'service/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->service_model->update($update,$id) > 0)
            $this->session->set_flashdata('success', 'Service deleted successfully');
            
        else 
            $this->session->set_flashdata('error', 'Service not deleted!');
        
        redirect( 'service', 'refresh');
    }
    
    public function service_name_exists() {
        $data = $this->service_model->service_name_exists($_POST['menu_id'],$_POST['service'],$_POST['id']);
        echo json_encode($data);
        exit(0);
    }
}
?>