<?php namespace App\Controllers;
/**
 * File			: App.php
 * Description  : Controller untuk utility
 * Created By	: Hendi
 * Created Date : 20 Jan 2021
 * Last Update  : 20 Feb 2021
**/

class App extends MyBaseController
{
	public function index()
	{
		//
	}
	
	public function show404() 
	{
		echo 'Maaf, halamanG tidak ditemukanG :)';
	}
	
	# ubah password
	public function changePassword()
	{
		# fn isLoggedIn di MyBaseController
		if (!$this->isLoggedIn()) return redirect()->to(base_url('admin/login'));
		# jika sudah menu dinamis diisi dari array hasil query
		$data_menu = []; 
		# jika dibutuhkan data untuk konten
		$data_content = ['mod' => 'change_password'];
		$data = [
			'title' => 'Ubah Password',
			'menu' => view('contents/admin/menu', $data_menu),
			'content' => view('contents/admin/change_password', $data_content)
		];
		return view('templates/admin', $data);
	}
	
	public function doChangePassword()
	{
		# fn isLoggedIn di MyBaseController
		if (!$this->isLoggedIn()) return redirect()->to(base_url('admin/login'));
		
		$post = [];
		foreach($_POST as $k => $v) $post[$k] = $this->request->getPost($k);
		
		# roles 'change_password' sudah didefinisikan di Config/Validation.php
		if ($this->form_validation->run($post, 'change_password') === FALSE) 
		{
			# mengembalikan nilai input yang sudah diinput sebelumnya
            $this->session->setFlashdata('inputs', $post);
            # memberikan pesan error 
			$this->session->setFlashdata('errors', $this->form_validation->getErrors());
           	return redirect()->to(base_url('admin/change-password'));
		}
		else
		{
			$user_id = $this->session->get('user_id');
			$password = $this->request->getPost('password');
			$UserModel = new \App\Models\UserModel();
			$data = $UserModel->where('user_id', $user_id)->first();
			if ($data) 
			{
				$password_hash = $data['password'];
				//echo password_hash("a", PASSWORD_DEFAULT)."\n"; 
				$verify_pass = password_verify($password, $password_hash);
				if(!$verify_pass)
				{
					$this->session->setFlashdata('errors', ['Password Lama tidak sama']);
					return redirect()->to(base_url('admin/change-password'));
				}
			}
			$new_password = $this->request->getPost('new_password');
			$post['password'] = password_hash($new_password, PASSWORD_DEFAULT);
			# tidak disertakan dalam validasi / penyimpanan
			unset($post['csrf_token_name']);
			# jika satu baris saja boleh tanpa tanda {}
			$res = $UserModel->update($user_id, $post);
			
			# memberikan pesan, pake ul jadi pake []
			if ($res) #true
				$this->session->setFlashdata('success', 'Password berhasil diubah. Silahkan login ulang');
			else
				$this->session->setFlashdata('errors', ['Password gagal diubah']);
			
			return redirect()->to(base_url('admin/change-password'));
		}
	}
	
	# siaran langsung
	public function liveStreaming()
	{
		# fn isLoggedIn di MyBaseController
		if (!$this->isLoggedIn()) return redirect()->to(base_url('admin/login'));
		# jika sudah menu dinamis diisi dari array hasil query
		$data_menu = []; 
		# jika dibutuhkan data untuk konten
		$data_content = [
		    'mod' => 'live_streaming',
		    'setting' => (new \App\Models\SettingModel())->first()
		];
		$data = [
			'title' => 'Setting Video Siaran Langsung',
			'menu' => view('contents/admin/menu', $data_menu),
			'content' => view('contents/admin/live_streaming', $data_content)
		];
		return view('templates/admin', $data);
	}
	
	public function setLiveStreaming()
	{
		# fn isLoggedIn di MyBaseController
		if (!$this->isLoggedIn()) return redirect()->to(base_url('admin/login'));
		
		$post = [];
		foreach($_POST as $k => $v) $post[$k] = $this->request->getPost($k);
		
		# roles 'live_streaming' sudah didefinisikan di Config/Validation.php
		if ($this->form_validation->run($post, 'live_streaming') === FALSE) 
		{
			# mengembalikan nilai input yang sudah diinput sebelumnya
            $this->session->setFlashdata('inputs', $post);
            # memberikan pesan error 
			$this->session->setFlashdata('errors', $this->form_validation->getErrors());
           	return redirect()->to(base_url('admin/live-streaming'));
		}
		else
		{
			$SettingModel = new \App\Models\SettingModel();
			# tidak disertakan dalam validasi / penyimpanan
			unset($post['csrf_token_name']);
			# jika satu baris saja boleh tanpa tanda {}
			$res = $SettingModel->update('ayub', $post);
			
			# memberikan pesan, pake ul jadi pake []
			if ($res) #true
				$this->session->setFlashdata('success', 'Video URL berhasil disimpan');
			else
				$this->session->setFlashdata('errors', ['Video URL gagal disimpan']);
			
			return redirect()->to(base_url('admin/live-streaming'));
		}
	}
}
