<?php

namespace App\Controllers;

// Ambil 3 Tabel BUGS, BUGS_COMMENT, USERS
use App\Models\BugsModel;
use App\Models\UsersModel;
use App\Models\BugCommentModel;

use CodeIgniter\Controller;

class BugComment extends Controller {

    public function delete($bugcommentid = null, $bugid =  null) {
        $db = \Config\Database::connect();
		$sql = "DELETE FROM bugcomment where bugcommentid='$bugcommentid'";
		if($db->query($sql)) {
            return redirect()->to('/bugs/view/'.$bugid); 
        } else { 
            return view('form_fail'); 
        }
    }

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

        if ($this->request->getMethod() === 'POST') {
            $rules = [
                'bugcommenttext' => 'required',
            ];
            $data['new_value'] = [
                'bugid' => $this->request->getVar('bugid'),
                'bugcommenttext' => $this->request->getVar('bugcommenttext'),
                'userid' => $this->request->getVar('userid'),
            ];

            if($this->validate($rules)){
                $bugCommentModel = new BugCommentModel();
                $bugCommentModel->set('logdate', 'NOW()', FALSE);
                $bugCommentModel->update($bugcommentid, $data['new_value']);                
                return redirect()->to('/bugs/view/'.$this->request->getVar('bugid'));
            } else {
                $data['validation'] = $this->validator;
                return view('bugcomment_edit', $data);
            }
        }
        return view('bugcomment_edit', $data);
    }
}