<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\GuidebookModel;

class Guidebook extends Controller {
	
	protected array $data;
	
	function __construct() {
		// $this->data['bugpriorities'] = array('0'=>'Low', '1'=> 'Medium', '2' => 'High');
	}

	// Melihat dan membuat Thread Bugs
	public function index() {
		$db = \Config\Database::connect();
		$sql = "SELECT * FROM guidebooks";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['guidebooks'] = $results;
		
		return view('guidebook_index', $data);
	}

	// Fungsi Membuat Thread Bugs Baru pada Jendela Baru
	public function create() {
		
		// $data['bugpriorities'] = $this->data['bugpriorities'];

		if ($this->request->getMethod() === 'POST') {
			$rules = [
				'guidetitle' => 'required',
				'guidedetail' => 'required',
			];
			$data['new_value'] = [
				'guidetitle' => $this->request->getVar('guidetitle'),
				'guidedetail'	=> $this->request->getVar('guidedetail'),
				// 'guidecategory'	=> $this->request->getVar('bugstatus'),
				'userid_creator'  => $this->request->getVar('userid_creator')
			];
			if($this->validate($rules)){
				$guideModel = new GuidebookModel();
				$guideModel->set('createdate', 'NOW()', FALSE);
				$guideModel->insert($data['new_value']);
				return view('form_success');
			} else {
				$data['validation'] = $this->validator;
				return view('guidebook_create', $data);
			}
		}
		return view('guidebook_create');
	}

	public function edit($guideid = null) {
		$db = \Config\Database::connect();
		$sql = "SELECT * FROM guidebooks WHERE guideid='$guideid'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['guidebooks'] = $results;

		if ($this->request->getMethod() === 'POST') {
			if ($this->request->getMethod() === 'POST') {
				$rules = [
					'guidetitle' => 'required',
					'guidedetail' => 'required',
				];
				$data['new_value'] = [
					'guidetitle' => $this->request->getVar('guidetitle'),
					'guidedetail'	=> $this->request->getVar('guidedetail'),
					// 'guidecategory'	=> $this->request->getVar('bugstatus'),
				];

				if ( $this->validate($rules) ) {
					$guideModel = new GuidebookModel();
					$guideModel->update($guideid, $data['new_value']);
					return view('form_success');
				} else {
					$data['validation'] = $this->validator;
					return view('guidebook_edit', $data);
				}
			}
		}
		return view('guidebook_edit', $data);
	}

	public function view($guideid = null) {
		
		//Connect Database
		$db = \Config\Database::connect();
		$sql = "SELECT g.*, CONCAT(u.firstname, ' ', u.lastname) AS fullname FROM guidebooks g 
				LEFT JOIN users u ON u.userid=g.userid_creator
				WHERE g.guideid=$guideid";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['guidebook'] = $results;

		// var_dump($data);die();

		return view('guidebook_view', $data);
	}

	public function delete($guideid = 0) {

		$db = \Config\Database::connect();
		$sql = "DELETE FROM guidebooks 
				WHERE guideid = '$guideid'";
		if($db->query($sql)) { 
			return view('form_success'); 
			// return redirect()->to('/bugs');
		} else { 
			return view('form_fail'); 
		}		
	}

}