<?php

include_once("_nemo.list.cls.php");

class Product extends NemoList
{
	private $ID = 0;

	public function __construct($DataKey)
	{
		$this->Filters[frSearch]->tag = "input";
	   	$this->Filters[frSearch]->html->value = "";
	   	$this->Filters[frSearch]->html->type = "text";
	   	$this->Filters[frSearch]->html->class = "controlText";

		$this->Filters[frStatus]->tag = "select";
		$this->Filters[frStatus]->html->value = "-1";
		$this->Filters[frStatus]->html->class = "controlText";
		$this->Filters[frStatus]->sql = "SELECT -1 AS ControlValue, '- All -' AS ControlText
			UNION ALL
			SELECT 1 AS ControlValue, 'Active' AS ControlText
			UNION ALL
			SELECT 0 AS ControlValue, 'Inactive' AS ControlText
			ORDER BY ControlText ASC";

      parent::__construct($DataKey);
	}

	public function getList()
	{
		global $SystemSettings;

		$Where = "";
		if ($this->Filters[frSearch]->html->value != ""){
			$like = "LIKE(". $this->db->qs("%".$this->Filters[frSearch]->html->value."%") .")";
			$Where .= "AND (tblProduct.strProduct $like)";
		}

		if ($this->Filters[frStatus]->html->value != -1){
			$Where .= " AND tblProduct.blnActive = ". $this->Filters[frStatus]->html->value;
		}

		$this->ListSQL("
         SELECT tblProduct.ProductID, tblProduct.strProduct AS Product, tblProduct.strShortCode AS Code, FORMAT(tblProduct.dblPrice, 2) AS 'Product Price', IF(tblProduct.blnActive,'Yes','No') AS Status , tblProduct.strLastUser AS 'Last User', tblProduct.strLastEdit AS 'Last Edit'
         FROM (tblProduct)
         WHERE 1=1 $Where
         ORDER BY tblProduct.strProduct ASC", 0,"product.php");

		$this->Columns["Status"]->html->align = "center";
		$this->Columns["Product Price"]->html->align = "right";
		//print_rr($this->Columns["Status"]);

		return $this->renderTable("Products List");
	}

	public static function Save(&$ProductID)
	{
		global $SystemSettings;

		// //print_rr($_POST);

		$cdb = new NemoDatabase("tblProduct", $ProductID, null, 0);
	   	$cdb->SetValues($_POST);
	   	if($cdb->Fields[refPracticeConsultantID] == 0) $cdb->Fields[refPracticeConsultantID] = "NULL";
	   	$cdb->Fields[strLastUser] = $_SESSION["USER"]->USERNAME;
	   	$cdb->doQuery("SET FOREIGN_KEY_CHECKS=0");	//this is for the foreign keys - neils
	   	$rst = $cdb->Save(0,0);
	   	$cdb->doQuery("SET FOREIGN_KEY_CHECKS=1");

		if($ProductID == 0) $ProductID = $cdb->ID[ProductID];

		if($rst->Error == 1)
		   return $rst->Message;
		else
		   return "Details Saved. ";
	}

	public static function Delete($chkSelect)
	{
		global $SystemSettings, $xdb;

		if(count($chkSelect) > 0){
			foreach($chkSelect as $key => $value){
				$xdb->doQuery("DELETE FROM tblProduct WHERE ProductID = ". $xdb->qs($key));
			}
			return "Records Deleted. ";
		}
	}
}

?>