<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Payment_type extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('payment_type_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->payment_type_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 Payment Type';
        $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, 'payment_type/list');
    }
    
    public function add() {
        $data = array();
        $actions = $this->actions();
        $data['form_action'] = $actions['insert'];
        $data['form_action'] = $actions['insert'];
        $this->render($data, 'payment_type/add');
    }
    
    private function get_payment_url_key($name, $id = '') {
        $url_key = get_url_key($name);
        if ($this->payment_type_model->payment_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_payment_url_key($name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function insert() {
        $insert = array(
            'name' => $this->input->post('pay_type'),
            'url_key' => $this->get_payment_url_key($this->input->post('pay_type')),
            'slug' => get_var_name($this->input->post('pay_type')),
            'menu_id' => ((!empty($this->input->post('is_archanai'))) ? $this->input->post('is_archanai') : 0),
            /* 'status' => $this->input->post('status'), */
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $img = $_FILES['image_url'];
        if( !empty( $img['name'] ) ) :
            // Upload
            
            if( !file_exists( DIR_PAY_TYPE ) ) :
                mkdir(DIR_PAY_TYPE, 0777, TRUE);
                chmod(DIR_PAY_TYPE, 0777);
            endif;
            
            $file_name =  time() .'-'. get_var_name($img['name'], '_', array('.'));
            $config = array(
                'upload_path'   => DIR_PAY_TYPE,
                'file_name'     => $file_name,
                'allowed_types' => '*',
            );
            
            $this->upload->initialize($config);
            $this->upload->do_upload( 'image_url' );
            $uploaded_data = $this->upload->data();
            $insert['img_url'] = $file_name;
        endif;
        
        $insert_id = $this->payment_type_model->insert($insert);  
        if ($insert_id > 0)
            $this->session->set_flashdata('success', 'Payment Type added successfully');    
        
        else
            $this->session->set_flashdata('error', 'Payment Type not added!');  
        
        redirect('payment_type', 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['result'] = $this->payment_type_model->get_by_id($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        $this->render($data, 'payment_type/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $update = array(
            'name' => $this->input->post('pay_type'),
            //'img_url' => $this->input->post('image_url'),
            'url_key' => $this->get_payment_url_key($this->input->post('pay_type')),
            'menu_id' => ((!empty($this->input->post('is_archanai'))) ? $this->input->post('is_archanai') : 0),
            /* 'status' => $this->input->post('status'), */
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        $file_name = '';
        $rm_files = $this->input->post('file_remove'); 
        $hid_file = $this->input->post('hidden_image_url'); 
        if( !empty( $rm_files[0] ) || !empty( $hid_file ) ) :
            // Deleted
            $rm_file_name = !empty( $rm_files[0] ) ? $rm_files[0] : DIR_PAY_TYPE . $hid_file;
            unlink( $rm_file_name );
            $update['img_url'] = NULL;
        endif;
        
        $img = $_FILES['image_url'];
        if( !empty( $img['name'] ) ) :
            // Upload
            
            if( !file_exists( DIR_PAY_TYPE ) ) :
                mkdir(DIR_PAY_TYPE, 0777, TRUE);
                chmod(DIR_PAY_TYPE, 0777);
            endif;
            
            $file_name =  time() .'-'. get_var_name($img['name'], '_', array('.'));
            $config = array(
                'upload_path'   => DIR_PAY_TYPE,
                'file_name'     => $file_name,
                'allowed_types' => '*',
            );
            
            $this->upload->initialize($config);
            $this->upload->do_upload( 'image_url' );
            $uploaded_data = $this->upload->data();
            $update['img_url'] = $file_name;
        endif;
        
        
        if ($this->payment_type_model->update($update, $id) > 0)
            $this->session->set_flashdata('success', 'Payment Type updated successfully');  
        
        else
            $this->session->set_flashdata('error', 'Payment Type not updated!');    
        
        redirect('payment_type','refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['result'] = $this->payment_type_model->get_by_id($id);
        $this->render($data, 'payment_type/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->payment_type_model->update($update,$id) > 0)
            $this->session->set_flashdata('success', 'Payment Type deleted successfully');
            
        else 
            $this->session->set_flashdata('error', 'Payment Type not deleted!');
        
        redirect( 'payment_type', 'refresh');
    }
    
    public function payment_name_exists() {
        $data =  $this->payment_type_model->payment_name_exists($_POST['name'],$_POST['id']);
        echo json_encode($data);
        exit(0);
    }
}
?>