<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Price extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('price_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->price_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 Price';
        $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, 'price/list');
    }
    
    public function add() {
        $data = array();
        $actions = $this->actions();
        $data['form_action'] = $actions['insert'];
        $data['category'] = $this->price_model->category_list();
        //$data['item'] = $this->price_model->item_list();
        $data['form_action'] = $actions['insert'];
        $this->render($data, 'price/add');
    }
    
    private function get_price_url_key($item_name, $id = '') {
        $url_key = get_url_key($item_name);
        if ($this->price_model->item_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_price_url_key($item_name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function insert() {
        $item_id = implode(',',$this->input->post('item_id'));
        $insert = array(
            'category_id' => $this->input->post('category_id'),
            'item_id' => $item_id,
            'start_date' => date('Y-m-d',strtotime($this->input->post('start_date'))),
            'end_date' => date('Y-m-d',strtotime($this->input->post('end_date'))),
            'price' => $this->input->post('price'),
            'url_key' => $this->get_price_url_key($this->input->post('category_id')),
            'status' => $this->input->post('status'),
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $insert_id = $this->price_model->insert($insert);  
        if ($insert_id > 0)
            $this->session->set_flashdata('success', 'Price added successfully');    
        
        else
            $this->session->set_flashdata('error', 'Price not added!');  
        
        redirect('price', 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['category'] = $this->price_model->category_list();
        //$data['item'] = $this->price_model->item_list();
        $data['result'] = $this->price_model->get_by_id($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        $this->render($data, 'price/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        $item_id = implode(',',$this->input->post('item_id'));
        $update = array(
            'category_id' => $this->input->post('category_id'),
            'item_id' => $item_id,
            'start_date' => date('Y-m-d',strtotime($this->input->post('start_date'))),
            'end_date' => date('Y-m-d',strtotime($this->input->post('end_date'))),
            'price' => $this->input->post('price'),
            'url_key' => $this->get_price_url_key($this->input->post('category_id')),
            'status' => $this->input->post('status'),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        //echo "<pre>Data: "; print_r( $update ); die;
        
        if ($this->price_model->update($update, $id) > 0)
            $this->session->set_flashdata('success', 'Price updated successfully');  
        
        else
            $this->session->set_flashdata('error', 'Price not updated!');    
        
        redirect('price','refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['result'] = $this->price_model->get_by_id($id);
        $this->render($data, 'price/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->price_model->update($update,$id) > 0)
            $this->session->set_flashdata('success', 'Price deleted successfully');
            
        else 
            $this->session->set_flashdata('error', 'Price not deleted!');
        
        redirect( 'price', 'refresh');
    }
    
    public function price_name_exists() {
        $data =  $this->price_model->price_name_exists($_POST['category_name'],$_POST['item_name'],$_POST['start_date'],$_POST['end_date'],$_POST['id']);
        echo json_encode($data);
        exit(0);
    }
    
    function get_cat_items(){
        $results = $this->price_model->get_cat_items();
        echo json_encode( $results );
    }
}
?>