<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

Class Payment extends CI_Controller
{
    public function __construct()
    {
      parent::__construct();
      //load models
      $this->load->library('ion_auth');
      $this->load->model('payment_model','',TRUE);
    }
    
    public function index()
    {
        if (!$this->ion_auth->logged_in() && !$this->ion_auth->is_admin())
		{
			//redirect them to the login page
			redirect('auth/login', 'refresh');
		}
		else
		{
		       $this->display();
		}
    }
    
    public function display()
    {
        //load sub views
         $this->data['subview'] = 'home';
         $this->data['subview'] = 'membership/change_fee';
         
         //get individual price
         $indi_price = $this->payment_model->get_individual_price();
         $indi_price2 = $indi_price['rows'];//json_encode($indi_price);
         $indi_price3 = $indi_price2['0'];
         $indi_price4 = $indi_price3->price;
         
         //get company price
         $comp_price = $this->payment_model->get_company_price();
         $comp_price2 = $comp_price['rows'];//json_encode($indi_price);
         $comp_price3 = $comp_price2['0'];
         $comp_price4 = $comp_price3->price;
         
         //get listing price
         $list_price = $this->payment_model->get_listing_price();
         $list_price2 = $list_price['rows'];//json_encode($indi_price);
         $list_price3 = $list_price2['0'];
         $list_price4 = $list_price3->price;
         
       //load input and display boxes
         $this->data['current_individual'] = $indi_price4;//'100';
         $this->data['current_company'] =  $comp_price4;//'500';
         $this->data['current_listing'] = $list_price4;//'500';
         
       //load view
         $this->load->view("layouts/main_admin", $this->data);
         //print_r($indi_price3);
    }
    
    //set the prices after run
    public function set_prices()
    {
        
        $this->form_validation->set_rules('individual_price', 'Enter New Price for Individual', 'trim|required|xss_clean');
        $this->form_validation->set_rules('company_price', 'Enter New Price for Company', 'trim|required|xss_clean');
        $this->form_validation->set_rules('listing_price', 'Enter New Price for Listing', 'trim|required|xss_clean');
        
        if ($this->form_validation->run() == FALSE) {
          if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
		{
	             //load the view passing $data values
                     $this->display();
		} 
                else
                    {
                        $this->load->view("layouts/main_logged_in", $this->data);
                    }
            
        }
        
        else
        {
        $newindi =$this->input->post('individual_price');
        $newcomp =$this->input->post('company_price');
        $newlist =$this->input->post('listing_price');
        
        $this->payment_model->set_individual_price($newindi);
        $this->payment_model->set_company_price($newcomp);
        $this->payment_model->set_listing_price($newlist);
        
        $this->session->set_flashdata('prices_updated', "New prices updated");
        
        redirect('payment/set_prices', 'refresh');
        }
    }
    
    public function get_individual_price()
    {
        $indi_price = $this->payment_model->get_individual_price();
         $indi_price2 = $indi_price['rows'];//json_encode($indi_price);
         $indi_price3 = $indi_price2['0'];
         $indi_price4 = $indi_price3->price;
         
         return $indi_price4;
    }
    
    public function get_company_price()
    {
        $comp_price = $this->payment_model->get_company_price();
         $comp_price2 = $comp_price['rows'];//json_encode($indi_price);
         $comp_price3 = $comp_price2['0'];
         $comp_price4 = $comp_price3->price;
         
         return $comp_price4;
    }
    
    public function get_listing_price()
    {
       $list_price = $this->payment_model->get_listing_price();
         $list_price2 = $list_price['rows'];//json_encode($indi_price);
         $list_price3 = $list_price2['0'];
         $list_price4 = $list_price3->price;
         
         return $list_price4;
    }
   
}
?>