<?php

/* 
 * Author       : Jai K
 * Purpose      : The controller for Archanai
 * Created On   : 2016-08-22 12:19
 */

class Billing_Archanai extends REF_Controller {
    public $controller;
    
    function __construct() {
        parent::__construct();
        
        $segments = $this->uri->segments;
        $this->controller = $segments[1];
        
        $this->load->model('billing/archanai_model');
    }
    
    function scan(){
        // Archanai Page for Kiosk Scan
        $user_data = get_user_data();
        
        if( empty( $user_data['user_id'] ) ) :
            // Redirect to login
            redirect('kiosk/scan');
        endif;
        
        $user = _get( TBL_BILL_MEMBER_CARD, array('id' => $user_data['user_id'] ), array('select' => '*, star AS star_id', 'is_single' => TRUE) );
                            
        $data = array(
            'user'      => $user,
            'category_on' => $user_data['category_on'],
            'settings_design'=> get_design_setting(),
            
            'gods'      => $this->archanai_model->get( TBL_GOD_NAME ),
            'members'   => $this->archanai_model->get_members( $user['id'] ),
            'pay_id'    => $this->archanai_model->scan('payment'), // Enable "Membership Card Payment Type" 
        );
        
        if( $data['category_on'] ) :
            $data['categories'] = $this->archanai_model->get( TBL_CATEGORY, array('is_scan' => 1), array('order_by' => 'category_order') );
        else :
            $data['items'] = $this->archanai_model->get_items_by_not_cat(1);
        endif;
        
        #echo "<pre>"; print_r( $data ); die;
        $this->load->view( 'billing/archanai/scan', $data);
		
    }
    
    function add( $id='' ) {
        $this->manipulate( $id );
		
        
    }
    
    function manipulate( $id, $args = array() ) {
        $actions = $this->actions();
        $post = $this->input->post();
        $session_data = $this->session->userdata(TBL_PREFIX . 'user_logged_in');
        $token = $this->session->userdata( 'rtoken-'.$session_data['user_id'] );
        
        $page = $page_file_name = !empty( $args['page'] ) ? $args['page'] : 'add';
        $page_file_name         = $page == 'pdf' ? 'view' : $page_file_name;
        
        $data = array(
            'id'                => $id,
            'title'             => 'Archanai (Billing)',
            'is_'.$page         => TRUE,
            'controller'        => $this->controller,
            'actions'           => $actions,
            'form_action'       => base_url() . $this->controller .'/'.$page.'/'.$id,
            'success'           => $this->session->flashdata('success'),
            'error'             => $this->session->flashdata('error'),
            'stars'             => $this->archanai_model->get( TBL_STAR ), 
            'categories'        => $this->archanai_model->get( TBL_CATEGORY, '', array('order_by' => 'category_order') ),
            'gods'              => $this->archanai_model->get( TBL_GOD_NAME ),
        );
        
        if( $page == 'add' || $page == 'edit' ) :
            // ID => Menu id
            $data['token'] = $this->auto_generation_code( rand( 999, 9999 ) );
            $this->session->set_userdata( 'rtoken-'.$session_data['user_id'], $data['token'] );
            
            $data['ptypes']     = $this->archanai_model->get( TBL_PAY_TYPE );
            $data['items']      = $this->archanai_model->get_items_by_not_cat();
        endif;
        
        // Load Views
        $this->render( $data, 'billing/archanai/add' );
        /*$this->load->view( 'billing/header', $data );
        $this->load->view( 'billing/archanai/add', $data );
        $this->load->view( 'billing/footer', $data );*/
        
    }
    
    function ajax() {
        $results =  $this->archanai_model->ajax();
        
        echo json_encode( $results );
    }
    
    

    
    
    
} // class    

