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

/*
//20160126 - NEW - st
*/

class Service 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()
   {
      if($this->Filters[frSearch]->html->value != "")
      {
         $like = "LIKE(". $this->db->qs("%".$this->Filters[frSearch]->html->value."%") .")";
         $Where .= " AND (strService $like OR strType $like OR txtDescription $like OR txtReportTerms $like)";
      }

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

      $this->ListSQL("SELECT ServiceID, strService AS Service, strType AS 'Service Type', txtDescription AS 'Description'
         , FORMAT(dblCostYear1,0) AS 'Cost Year 1', FORMAT(dblCostMaintenance,0) AS 'Cost Maintenance', blnActive AS Active, strLastUser AS 'Last User', dtLastEdit AS 'Last Edit'
                     FROM slaService
                     WHERE 1=1 $Where
                     ORDER BY strType, intDisplayOrder, strService", 0);


      $this->Columns["Cost Year 1"]->html->align = "right";
      $this->Columns["Cost Maintenance"]->html->align = "right";

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

   // EXport all columns
   public function getListExportAllColumns()
   {
      if($this->Filters[frSearch]->html->value != "")
      {
         $like = "LIKE(". $this->db->qs("%".$this->Filters[frSearch]->html->value."%") .")";
         $Where .= " AND (slaService.strService $like OR slaService.strType $like OR slaService.txtReportTerms $like)";
      }

      $this->ListSQL("SELECT *
                     FROM slaService
                     WHERE 1=1 $Where
                     ORDER BY strType, intDisplayOrder, strService", 0);

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

   public static function Save(&$ServiceID)
   {
      global $xdb, $arrSys, $TR, $SP, $HR, $SystemSettings;

      $db = new NemoDatabase("slaService", $ServiceID, null, 0);

      $db->SetValues($_POST);

      $db->Fields[strLastUser] = $_SESSION['USER']->USERNAME;

      $result = $db->Save();
      if($ServiceID == 0) $ServiceID = $db->ID[ServiceID];

      //print_rr($result);
      if($result->Error == 1){
         return $result->Message;
      }else{
         return "Details Saved. ";
      }
   }

   public static function Delete($chkSelect)
   {
      global $xdb;
      //print_rr($chkSelect);
      if(count($chkSelect) > 0){
      foreach($chkSelect as $key => $value)
      {
         $xdb->doQuery("DELETE FROM slaService WHERE ServiceID = ". $xdb->qs($key));
         //$xdb->doQuery("UPDATE slaService SET blnActive = 0 WHERE ServiceID = ". $xdb->qs($key));
      }
         return "Records Deleted. ";
      }
   }

}

?>