<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Dashboard extends CI_Controller {
	// Session Authoriesed
	public function __construct() {
		parent::__construct();
		
		$admin = $this->session->userdata('admin');
		$column['id'] = $admin['id'];
		$table = 'tbl_user';
		
	    // loading common model
	    $this->load->model('Common_model');
		$result = $this->Common_model->Result($column,$table);
		if($result['password'] == $admin['password']){
		    $login = true;
		} else {
		    $login = false;
		}
		
		if (empty($admin) || $login==false) {
			$this->session->set_flashdata('msg','Your Session Has Been Expired');
			redirect(base_url().'master/login/index');
		}
	}

	public function index()
	{
	
	
	    $admin = $this->session->userdata('admin');
		$column['id'] = $admin['id'];
		$table = 'tbl_user';
		
	    // loading common model
	    $this->load->model('Common_model');
		$data['admin'] = $this->Common_model->Result($column,$table);
		
		
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'dashboard';
		
		$data['faq'] = $this->Common_model->Count('tbl_faq');
		$data['customer'] = $this->Common_model->Count('tbl_customer');
		// 		getting product data
        $table0 = 'tbl_product';
        $table2 = 'tbl_top_category';
        $table3 = 'tbl_mid_category';
        $table4 = 'tbl_end_category';

        // we are getting product results from common modal
        $result0 = $this->Common_model->Results_product($table0, $table2, $table3, $table4);
            
		$data['product'] = count($result0);
		$data['stock_amt'] = $this->Common_model->Select_Sum('p_current_price','tbl_product_size');
// 		$data['sold_amt'] = $this->Common_model->Select_Sum('unit_price','tbl_order');

		$result2 = $this->Common_model->Select_Sum_into_qty('unit_price','quantity','tbl_order');
		$sold_amt = 0;
		foreach($result2 as $row){
		    $sold_amt += $row['unit_price']*$row['quantity'];
		}
		$data['sold_amt'] = $sold_amt;
		
		$data['product_sold'] = $this->Common_model->Select_Sum_By('quantity','tbl_order');
		$data['order'] = $this->Common_model->Count('tbl_payment');
		$data['top_category'] = $this->Common_model->Count('tbl_top_category');
		$data['mid_category'] = $this->Common_model->Count('tbl_mid_category');
		$data['end_category'] = $this->Common_model->Count('tbl_end_category');
		$data['state'] = $this->Common_model->Count('tbl_state');
		$data['district'] = $this->Common_model->Count('tbl_district');
		
        // we are loading admin dashboard
		$this->load->view('master/dashboard',$data);	
	}
	
	public function getPendingOrder(){
       $ipaddress = '';
       if (getenv('HTTP_CLIENT_IP')) {
           $ipaddress = getenv('HTTP_CLIENT_IP');
       } else if (getenv('HTTP_X_FORWARDED_FOR')) {
           $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
       } else if (getenv('HTTP_X_FORWARDED')) {
           $ipaddress = getenv('HTTP_X_FORWARDED');
       } else if (getenv('HTTP_FORWARDED_FOR')) {
           $ipaddress = getenv('HTTP_FORWARDED_FOR');
       } else if (getenv('HTTP_FORWARDED')) {
           $ipaddress = getenv('HTTP_FORWARDED');
       } else if (getenv('REMOTE_ADDR')) {
           $ipaddress = getenv('REMOTE_ADDR');
       } else {
           $ipaddress = 'UNKNOWN';
       }
       $ipaddress;
       
	    if($_POST['ipaddress'] && $ipaddress == '202.142.123.34') {
	        $where['shipping_status'] = 'Pending';
            $query = $this->Common_model->Result($where,'tbl_payment');
            echo $pending_order_count = count($query);
            // echo 0;
    	} else {
    	    echo "Wrong Request";
    	}
	}
	
}

/* End of file Dashboard.php */
/* Location: ./application/controllers/master/Dashboard.php */