<?php

namespace App\Controllers;

use App\Models\ActTypeModel;
use CodeIgniter\Controller;

class ActType extends Controller {

	public function index() {
		$db = \Config\Database::connect();
		$sql = "SELECT * FROM acttype";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['acttype'] = $results;
		return view('acttype_index', $data);
	}

	public function edit($acttypeid = null) {
		$db = \Config\Database::connect();
		$sql = "select * from acttype where acttypeid='$acttypeid'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['acttype'] = $results;
		if ($this->request->getMethod() === 'POST') {
			$rules = [
				'acttypeid' => 'required',
				'acttypecode' => 'required',
				'fulltext' => 'required'
			];
			$data['new_value'] = [
				'acttypeid' => $this->request->getVar('acttypeid'),
				'acttypecode' => $this->request->getVar('acttypecode'),
				'fulltext'  => $this->request->getVar('fulltext')
			];
			if($this->validate($rules)){
				$actTypeModel = new ActTypeModel();
				$actTypeModel->update($acttypeid, $data['new_value']);
				return view('form_success');
			} else {
				$data['validation'] = $this->validator;
				return view('acttype_edit',$data);
			}
		}
		return view('acttype_edit', $data);
	}
	
	public function create() {
		$db = \Config\Database::connect();
		$data = array();
		if ($this->request->getMethod() === 'POST') {
			$rules = [
				'acttypecode' => 'required',
				'fulltext' => 'required'
			];
			$data['new_value'] = [
				'acttypecode' => $this->request->getVar('acttypecode'),
				'fulltext'  => $this->request->getVar('fulltext')
			];
			if($this->validate($rules)){
				$actTypeModel = new ActTypeModel();
				$actTypeModel->set('createdate', 'NOW()', FALSE);
				$actTypeModel->insert($data['new_value']);
				return view('form_success');
			} else {
				$data['validation'] = $this->validator;
				return view('acttype_create',$data);
			}
		}
		return view('acttype_create', $data);
	}
}