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

class User extends CI_Controller {
     
	public function __construct()
	{
	    parent::__construct();
		 if(!isset($_SESSION['userid']))
		 {
			redirect(base_url());
	     }
	}
	
	//************* user info page start******************//	
	public function profile(){
		
		$offer_query=$this->Data_access_model->select_data('*','hs_offers',array( 'offer_status' =>1,));
		if($offer_query->num_rows() > 0){
			   
		    $data['all_offer'] =$offer_query->result();
			 
		}else{ 
			$data['all_offer'] = "";
			  
		}
		
		$order_query = $this->Data_access_model->select_data('*','hs_user_orders',array('order_uid' =>$_SESSION['userid']),'','order_id');
		if($order_query->num_rows() > 0){
			$data['all_uorders'] =$order_query->result();
		}else{ 
			$data['all_uorders'] = "";
		}
		
		$user_wallet = $this->Data_access_model->select_data('*','hs_user_wallet',array('wallet_uid' =>$_SESSION['userid']));
	    if($user_wallet->num_rows() > 0){
			$data['current_wallet'] = $user_wallet->result();
		}else{ 
			 $data['current_wallet'] = '';
		}
		
		$wallet_transaction = $this->Data_access_model->select_data('*','hs_uwallet_credit',array('wcredit_uid' =>$_SESSION['userid']),'','wcredit_id');
		 
		if($wallet_transaction->num_rows() > 0){
			   
		    $data['wallet_transactions'] =$wallet_transaction->result();
			 
		}else{ 
			$data['wallet_transactions'] = "";
			  
		}
		
		$opening_time_query =$this->Data_access_model->select_all('hs_opening_schedule');
		if($opening_time_query->num_rows() > 0){
			$setting_data['opening_time'] =$opening_time_query->result();
			 
		}else{ 
			$setting_data['opening_time'] = "";
		}
		
		$partner_query=$this->Data_access_model->select_data('*','hs_partners',array( 'partner_status' =>1,));
		if($partner_query->num_rows() > 0){
			   
		    $setting_data['all_partners'] =$partner_query->result();
			 
		}else{ 
			$setting_data['all_partners'] = "";
			  
		}
		$data['page_title']='User Profile';
		$data['page_descri'] ='User Profile';
		$this->load->view('theme/common/header',$data);
		$this->load->view('theme/user_info',$data);
		$this->load->view('theme/common/footer',$setting_data);
	}
	//************* user info page end******************//	

	//************* update user basic info start************//

    public function update_user_basicinfo(){
	
		if(isset($_FILES['user_pic']) && $_FILES['user_pic']['name'] !=="")
		{
			$config['upload_path'] = './uploads/user_pic';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $this->load->library('upload');
            $this->upload->initialize($config);
            if (!$this->upload->do_upload('user_pic'))
			{
				$error = array('error' => $this->upload->display_errors());
				echo 0;
			}
			else
			{  
				$imgdata = array('upload_data' => $this->upload->data());
				foreach($imgdata as  $path){
				$imagename = $path['file_name'];
				}
				 
				$web_image ='uploads/user_pic/'.$imagename;
				   //***image resize for***// 
				$config['image_library'] = 'gd2';
				$config['source_image']	='./uploads/user_pic/'.$imagename;
				$config['create_thumb'] = FALSE;
				$config['maintain_ratio'] = FALSE;
				$config['height']	= "61";
				$config['width'] = "70";
				$config['new_image'] ='./uploads/user_pic/'.$imagename;
				//you should have write permission here..
				 
				$this->image_lib->initialize($config);
				if(!$this->image_lib->resize()){
					$error['resize'] = $this->image_lib->display_errors();
				} 
			} 
				$data = array(
				'user_fname' =>ucfirst($this->input->post('user_fname')),
				'user_lname' =>ucfirst($this->input->post('user_lname')),
				'user_image' =>$web_image,
				);
		}else{
				$data = array(
				'user_fname' =>ucfirst($this->input->post('user_fname')),
				'user_lname' =>ucfirst($this->input->post('user_lname')),
				);
			}	  
				$this->Data_access_model->update_data('hs_user',array('user_id' =>$this->input->post('up_user_id')),$data);   
				echo 1;    
	}
	//************* update user basic info end************//	
	
	//************* update user billing info start********//
    public function update_user_billinginfo(){
		$old_uid = $this->input->post('billing_user_id');
		if(!empty($old_uid))
		{
			$data = array(
			'user_mobile' =>$this->input->post('update_umobile'),
			'user_address' =>$this->input->post('update_uaddress'),
			'user_country' =>$this->input->post('update_ucountry'),
			'user_state' =>$this->input->post('update_ustate'),
			'user_city' =>$this->input->post('update_ucity'),
			'user_zipcode' =>$this->input->post('update_uzip'),
			);
							
			$this->Data_access_model->update_data('hs_user',array('user_id' =>$old_uid),$data);   
			echo 1;	
		}else{
			echo 0;
		}
	}
	//************* update user billing info end**********//	


	//************* reset user password start ************//
	public function reset_user_password(){
	 
		$this->form_validation->set_rules('update_npwd','Password', 'trim|required');
		$this->form_validation->set_rules('update_confpwd','Confirm Password', 'trim|required');
		if ($this->form_validation->run() == FALSE)
		{
			echo 0; 
			
		}else{
			$old_user_id = $this->input->post('reset_userpwd_id');
			$u_newpass = md5($this->input->post('update_npwd'));
			$u_confpass = md5($this->input->post('update_confpwd'));
			 
			if($u_newpass == $u_confpass){
				$data = array('user_password' =>$u_newpass);
				$this->Data_access_model->update_data('hs_user',array('user_id' =>$old_user_id),$data); 
				echo 1;
			}else{
				echo 0;
			}
		}
    }
	//************* reset user password end **************//
	 
}


