<?php

namespace App\Controllers;

class Api extends BaseController {
	
	// getProductList for CLQMS

	// public function getProductList() { 
	// 	$db = \Config\Database::connect();
	// 	$sql = "SELECT p.`productid`, s.`sitename`, pc.`productname`, p.`productnumber` FROM products p
	// 		LEFT JOIN sites s ON p.`siteid`=s.`siteid`
	// 		LEFT JOIN productcatalog pc ON p.`catalogid`=pc.`catalogid`
	// 		WHERE pc.`producttypeid`=3 AND p.`locationenddate` IS NULL";
	// 	$query = $db->query($sql);
	// 	$data = $query->getResultArray();
	// 	if(count($data)==0) { $data = array('status' => 'error', 'message' => 'No data found'); }
    //     else {
	// 		header('Content-Type: application/json');
	// 		echo json_encode($data);
	// 	}
	// }

	public function getProductAlias() { 
		$db = \Config\Database::connect();
		$sql = "SELECT productaliasid, productaliastext 
				FROM productalias order by productaliasid";
		$query = $db->query($sql);
		$data = $query->getResultArray();
		if(count($data)==0) { $data = array('status' => 'error', 'message' => 'No data found'); }
        else {
			header('Content-Type: application/json');
			echo json_encode($data);
		}
	}

	public function getProductSites() { 
		$db = \Config\Database::connect();
		$sql = "SELECT pc.productaliasid, p.productnumber, s.sitename 
				FROM products p 
				LEFT JOIN productcatalog pc on pc.catalogid=p.catalogid 
				LEFT JOIN sites s on s.siteid=p.siteid 
				WHERE pc.productaliasid IN (18,19,20,21,60) AND (p.active=1 OR p.active IS NULL) ORDER BY pc.productaliasid";
		$query = $db->query($sql);
		$data = $query->getResultArray();
		if (count($data) === 0) {
			return $this->response->setJSON([
					'status' => 'error',
					'message' => 'No data found'
				]);
		}
			return $this->response->setJSON($data);
		}
	
}