<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Archanai_report extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('reports/archanai_report_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
       
        if (!empty($this->session->flashdata('success'))) 
            $data['success'] = $this->session->flashdata('success');
        else 
            $data['error'] = $this->session->flashdata('error');
        
        if((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Get Data' || $this->input->post('submit') == 'Print Data')) {
            $start_date = $this->input->post('start_date');
            $end_date 	= $this->input->post('end_date');
            $type_id 	= $this->input->post('type_id');
			$item_id	= $this->input->post('item_id');
        } else {
            $start_date = '';
            $end_date 	= '';
            $type_id 	= '';
			$item_id 	= '';
        }
        
		$data['results']	 	= $this->archanai_report_model->get_data($start_date,$end_date,$type_id,$item_id);
        $data['start_date'] 	= $start_date;
        $data['end_date']		= $end_date;
        $data['type_id'] 		= $type_id;
		$data['item_id'] 		= $item_id;
        
        $data['categories'] 	= $this->archanai_report_model->list_categories();
		
		if(!empty($type_id)){
			$data['items'] 		= $this->archanai_report_model->get_category_items($type_id);  //$post['cid']
		}else{
			$data['items'] 		= '';
		}
        
		$actions 				= $this->actions();
        $data['form_action'] 	= 'archanai_report';
        $data['title'] 			= 'Archanai Report';
        
        $filters = array();
        if(!empty($this->input->post('filter_columns'))) {
            foreach($this->input->post('filter_columns') as $key=>$value) {
                array_push($filters,$this->input->post($value));
            } 
        }
        $data['filters'] = array_filter($filters);
        #error_log(print_r($data['filters'],1));
        if($this->input->post('submit') == 'Print Data'){
            if( IS_KSK ) :
                $html = $this->load->view('reports/archanai/pdf', $data, TRUE);
                print_rec($html);
                redirect('archanai_report','refresh');
            else :
                $this->render($data, 'reports/archanai/pdf');
            endif;
        } else {
            $this->render($data, 'reports/archanai/list');
        }
    }
	
	public function get_category_items() {
       $post = $this->input->post();    
	   //echo "<pre>";print_r($post); die;    
	   $data = $this->archanai_report_model->get_category_items($post['cid']);  //$post['cid']
       echo json_encode($data);
    }
   
} 
?>