<?php

namespace App\Controllers;

use App\Models\ProductCatalogModel;
use CodeIgniter\Controller;

class ProductCatalog extends Controller {

	public function index() {
		$db = \Config\Database::connect();
		$data = array();
		$sql = "SELECT producttypeid, texts FROM producttype";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['producttype'] = $results;
		
		if ($this->request->getMethod() === 'POST') {
			$productname = $_POST['productname'];
			$catalognumber =  $_POST['catalognumber'];
			$producttypeid = $_POST['producttypeid'];
			if( $productname != '' || $catalognumber != '' || $producttypeid != '' ) {
				$db = \Config\Database::connect();
				$sql = "SELECT p.*, v.`vendorname`, pt.`texts` FROM productcatalog p
					LEFT JOIN vendors v ON v.`vendorid`=p.`vendorid`
					LEFT JOIN producttype pt ON pt.`producttypeid`=p.producttypeid
					where p.enddate is null ";
				if($productname != '') { $sql .= "AND p.productname like '%$productname%' "; }
				if($catalognumber != '') { $sql .= "AND p.catalognumber like '%$catalognumber%' "; }
				if($producttypeid != '') { $sql .= "AND p.producttypeid='$producttypeid' "; }
				$query = $db->query($sql);
				$results = $query->getResultArray();
				$data['productname'] = $productname;
				$data['catalognumber'] = $catalognumber;
				$data['productcatalog'] = $results;
				return view('productcatalog_index',$data);
			} else {
				return view('productcatalog_index', $data);
			}
			
		}
		return view('productcatalog_index', $data);
	}

	public function edit($catalogid = null) {
		$db = \Config\Database::connect();
		$sql = "SELECT * FROM productcatalog WHERE catalogid='$catalogid'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['productcatalog'] = $results;
		$sql = "SELECT * FROM producttype";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['producttype'] = $results;
		$sql = "SELECT * FROM vendors";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['vendors'] = $results;
		$sql = "SELECT * FROM productalias";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['productalias'] = $results;
		if ($this->request->getMethod() === 'POST') {
			$rules = [
				'catalogid' => 'required',
				'catalognumber' => 'required',
				'productname' => 'required',
				'producttypeid' => 'required',
				'vendorid' => 'required'
			];
			$data['new_value'] = [
				'catalogid' => $this->request->getVar('catalogid'),
				'catalognumber' => $this->request->getVar('catalognumber'),
				'productname'  => $this->request->getVar('productname'),
				'nie'  => $this->request->getVar('nie'),
				'producttypeid'  => $this->request->getVar('producttypeid'),
				'vendorid'  => $this->request->getVar('vendorid'),
				'manufacturer'  => $this->request->getVar('manufacturer'),
				'productaliasid' => $this->request->getVar('productaliasid')
			];
			if($this->validate($rules)){
				$productCatalogModel = new ProductCatalogModel();
				$productCatalogModel->update($catalogid, $data['new_value']);
				return view('form_success');
			} else {
				$data['validation'] = $this->validator;
				return view('productcatalog_edit',$data);
			}
		}
		return view('productcatalog_edit', $data);
	}
	
	public function create() {
		$db = \Config\Database::connect();
		$sql = "SELECT * FROM producttype";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['producttype'] = $results;
		$sql = "SELECT * FROM vendors";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['vendors'] = $results;
		$sql = "SELECT * FROM productalias";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['productalias'] = $results;
		if ($this->request->getMethod() === 'POST') {
			$rules = [
				'catalognumber' => 'required|is_unique[productcatalog.catalognumber]',
				'productname' => 'required',
				'producttypeid' => 'required',
				'vendorid' => 'required'
			];
			$data['new_value'] = [
				'catalognumber' => $this->request->getVar('catalognumber'),
				'productname'  => $this->request->getVar('productname'),
				'nie'  => $this->request->getVar('nie'),
				'producttypeid'  => $this->request->getVar('producttypeid'),
				'vendorid'  => $this->request->getVar('vendorid'),
				'manufacturer'  => $this->request->getVar('manufacturer'),
				'productaliasid' => $this->request->getVar('productaliasid')
			];
			if($this->validate($rules)){
				$productCatalogModel = new ProductCatalogModel();
				$productCatalogModel->set('createdate', 'NOW()', FALSE);
				$productCatalogModel->insert($data['new_value']);
				return view('form_success');
			} else {
				$data['validation'] = $this->validator;
				return view('productcatalog_create',$data);
			}
		}
		return view('productcatalog_create', $data);
	}
}