<?php namespace App\Controllers;

use App\Models\UsersModel;

class Contact extends BaseController
{	
	
	public function index()
	{
		$session = session();	

		$islogin = $session->get('islogin');
		if($islogin){
			$data['user'] = $session->get('user');
			return view('pages/main-detail',$data);
		}else{
			$data['error'] = 'Session teah habis, mohon login kembali';
			return view('pages/login',$data);
		}
		
		
	}
	
	public function save(){
		
		$name = $this->request->getPost('name');
		$nohp = $this->request->getPost('nohp');
		$alamat = $this->request->getPost('alamat');	
		$angkatan = $this->request->getPost('angkatan');	
		$jurusan = $this->request->getPost('jurusan');		
		$email = $this->request->getPost('email');		
		$twitterprofile = $this->request->getPost('twitterprofile');		
		$instagramprofile = $this->request->getPost('instagramprofile');		
		$pengalaman = $this->request->getPost('pengalaman');		
		$quote = $this->request->getPost('quote');		
		
		$userModel = new UsersModel();
		$usr = $userModel->asObject()->where('email',$email)->findAll();				
		if($this->request->getFile('fileImg')->isValid()){
			$fileName = $this->request->getFile('fileImg')->getRandomName();		
			$this->request->getFile('fileImg')->move(ROOTPATH.'public/user_img/', $usr[0]->id.'.jpg');
			$path = ROOTPATH.'public/user_img/'.$usr[0]->id.'.jpg';
		}		
		
		$data = [
			'id'=>$usr[0]->id,
			'name'=>$name,			
			'jurusan'=>$jurusan,
			'nohp'=>$nohp,
			'pengalaman'=>$pengalaman,
			'angkatan'=>$angkatan,
			'alamat'=>$alamat,
			'twitterprofile'=>$twitterprofile,
			'instagramprofile'=>$instagramprofile,
			'quote'=>$quote
		];
		
		$res = $userModel->protect(false)->save($data);
		$usr = $userModel->asObject()->where('email',$email)->findAll();
		$msg['user'] = $usr[0];
		if($res!=false){
			$msg['message'] = 'Data anda berhasil diubah';			
			return view('pages/main-detail',$msg);
		}else{
			$msg['error'] = 'Data gagal untuk diubah';
			return view('pages/main-detail',$msg);
		}
		
	}

	//--------------------------------------------------------------------

}
