<?php

namespace App\Controllers;

use App\Models\ProductServiceModel;
use CodeIgniter\Controller;

class ProductService extends Controller {

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

	public function create() {
		$db = \Config\Database::connect();
		$data = array();
		if ($this->request->getMethod() === 'POST') {
			$rules = [
				'productservicetext' => 'required'
			];
			$data['new_value'] = [
				'productservicetext' => $this->request->getVar('productservicetext')
			];
			if($this->validate($rules)){
				$productServiceModel = new ProductServiceModel();
				$productServiceModel->set('createdate', 'NOW()', FALSE);
				$productServiceModel->insert($data['new_value']);
				return view('form_success');
			} else {
				$data['validation'] = $this->validator;
				return view('productservice_create',$data);
			}
		}
		return view('productservice_create', $data);
	}
	
	public function edit($productserviceid = null) {
		$db = \Config\Database::connect();
		$sql = "SELECT * FROM productservice WHERE productserviceid='$productserviceid'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['productservice'] = $results;
		if ($this->request->getMethod() === 'POST') {
			$rules = [
				'productservicetext' => 'required'
			];
			$data['new_value'] = [
				'productservicetext' => $this->request->getVar('productservicetext')
			];
			if($this->validate($rules)){
				$productServiceModel = new ProductServiceModel();
				$productServiceModel->update($productserviceid, $data['new_value']);
				return view('form_success');
			} else {
				$data['validation'] = $this->validator;
				return view('productservice_edit',$data);
			}
		}
		return view('productservice_edit', $data);
	}
}