<?php namespace App\Controllers;

use App\Models\UsersModel;

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

		$islogin = $session->get('islogin');
		if($islogin){
			$data['user'] = $session->get('user');
			return view('pages/setting',$data);
		}else{
			$data['error'] = 'Session teah habis, mohon login kembali';
			return view('pages/login',$data);
		}
		
		
	}
	
	public function save(){
		
		$pwd = $this->request->getPost('password');
		$session = session();	
		
		$userModel = new UsersModel();
		$usr = $userModel->asObject()->find($session->get('user')->id);
		
		$data = [
			'id'=>$usr->id,
			'password'=>md5($pwd)
		];
		
		$res = $userModel->protect(false)->save($data);		
		$usr = $userModel->asObject()->find($session->get('user')->id);
		$msg['user'] = $usr;
		if($res!=false){
			$msg['message'] = 'Data anda berhasil diubah';
		}else{
			$msg['error'] = 'Data gagal untuk diubah';			
		}
		return view('pages/setting',$msg);
		
	}

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

}
