<?php 

namespace App\Controllers\User;
use App\Views;
use App\Models\CommonModel;
use App\Models\finance_model;
use App\Libraries\MyEncrypt;
use App\Libraries\mathcaptcha; 



class Know_customer extends BaseController {

	public function __construct() {

       $this->db = db_connect();
	  $this->common_model = model('CommonModel'); 
	  $this->session = \Config\Services::session();
       helper('site_helper');	

		$status = site_info('site_status');
		if($status!='on'){
			return redirect()->to('offline');
		} 

		// if(!$this->session->get('user_login')) 
		// {
		// 	 throw new \CodeIgniter\Router\Exceptions\RedirectException('login');
		// }
       

	     	$validation =  \Config\Services::validation();;

			$this->finance_model = model('finance_model');

	}
	
	public function index()
	{


        $this->session = \Config\Services::session();
		if(!$this->session->get('user_login')){
			return redirect()->to(site_url('user/login'));
		}

		$condition = "type in ('deposit','reinvest')";
		$this->data['lastinvest'] = $this->common_model->GetResultslast('history',$condition);
		$this->data['action'] = base_url().'/know_customer';
			
		$condition = "uusersid = '".$this->session->get('uusersid')."'";	
		$this->data['getuser'] = $this->common_model->GetRow('uusers',$condition);
	

		if(isset($_FILES['proof_id']['name']))
		{	
			if(isset($_FILES['proof_id']['tmp_name']) && $_FILES['proof_id']['size'] > 0)
			{
					$uploads_dir = './uploads';
					$tmp_name = $_FILES["proof_id"]["tmp_name"];
					$datez=strtotime(date("Y-m-d H:i:s"));         
			        $name = $datez.''.basename($_FILES["proof_id"]["name"]);
			        if(move_uploaded_file($tmp_name, "$uploads_dir/$name"))
			        {
			        	// delete existing file
			        	if(isset($usesdata->proof_id)){
							unlink('./uploads/'.$usesdata->proof_id);
						}

						$sitedata = array(
							"proof_id"	=>	$name,
							"proof_verify"=>'0'
						);

                        $builder = $this->db->table('uusers');
						$builder->where('uusersid',$this->session->get('uusersid'));
						$update_status_logo = $builder->update($sitedata);
			        }
			}

				if(isset($_FILES['proof_doc']['tmp_name']) && $_FILES['proof_doc']['size'] > 0) {

					$uploads_dir = './uploads';
					$tmp_name = $_FILES["proof_doc"]["tmp_name"];
					
					$datez=strtotime(date("Y-m-d H:i:s")); 
			        $name = $datez.''.basename($_FILES["proof_doc"]["name"]);
			        if(move_uploaded_file($tmp_name, "$uploads_dir/$name")){
			        	

			        	// delete existing file
			        	if(isset($usesdata->proof_id)){
							unlink('./uploads/'.$usesdata->proof_id);
						}

						$sitedata = array(
							"proof_doc"	=>	$name,
							"proof_verify"=>'0'
						);

						$builder = $this->db->table('uusers');
						$builder->where('uusersid',$this->session->get('uusersid'));
						$update_status_logo = $builder->update($sitedata);
			        }
			        
				}	

			if($update_status_logo)
			{
				$successlogin = array(
                    'uuserid' => $this->session->get('uusersid'),
                    'ip_address' => $_SERVER['REMOTE_ADDR'],
                    'pages'	=> basename($_SERVER['REQUEST_URI']),
                    'datatime' => date('Y-m-d H:i:s'),
                    'status' => 'success'
                );
                $builder = $this->db->table('login_ip');
                $builder->insert($successlogin);                 	                  	
				// $this->session->set_flashdata('success_message', $this->lang->line('menu_kfc_updated_success'));
				return redirect()->to('know_customer');
			} else {
				$this->session->setFlashData('error_message', lang('language_lang.language_lang.menu_kfc_updated_fail'));
				return redirect()->to('know_customer');
			}	
		}
		// $this->load->view('user/know',$this->data);
		echo view('user/know',$this->data);
		
	}

}

