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

class Venue extends NemoList
{
   private $ID = 0;

   public function __construct($DataKey)
   {
      parent::__construct($DataKey);
   }

   public function getCourseVenue($CourseID)
   {
      global $xdb, $SystemSettings, $DT;   

      $rowCourseVenueID = $xdb->getRowSQL("SELECT tblCourseVenue.*, tblVenue.intMaxCapacity
                                             FROM tblCourseVenue INNER JOIN tblVenue ON tblCourseVenue.refVenueID = tblVenue.VenueID
                                             WHERE tblCourseVenue.refCourseID=$CourseID",0);
     
      //$refVenueID = $rowCourseVenueID->refVenueID;
      $intMaxCapacity = $rowCourseVenueID->intMaxCapacity;

      $rowCourseVenue = $xdb->getRowSQL("SELECT * FROM tblCourseVenue INNER JOIN tblCourse on refCourseID = CourseID WHERE refCourseID = $CourseID",0);

      $sql = "SELECT tblVenue.* FROM tblVenue Where blnActive = 1 ORDER BY strVenue";            
      $rst = $xdb->doQuery($sql);
      
      while($row = $xdb->fetch_object($rst)) 
      {   
         //ini
         $disabled = "";
         $rowCV = $xdb->getRowSQL("SELECT * FROM tblCourseVenue INNER JOIN tblCourse on tblCourseVenue.refCourseID = tblCourse.CourseID  
                                    WHERE tblCourseVenue.refVenueID = $row->VenueID AND (dtStartDate >= LEFT('$DT',10)) AND (dtEndDate <= LEFT('$DT',10))",0);

         //$courseVenue ='('.$rowCourseVenue->strCourse.': '.$rowCourseVenue->dtStartDate.' --> '.$rowCourseVenue->dtEndDate.')';
         print_rr($row);
         
         //process   
         if($rowCV && $rowCV->refCourseID != $CourseID)
         {//disabled!         
            $disabled = "disabled";             
         }
         $ddlVenue .= "<option value='$row->VenueID' ".($row->VenueID == $rowCourseVenue->refVenueID ? "Selected" : "")." $disabled>$row->strVenue</option>";      
      }

      $strOutput = "                    
                    <table width='100%' cellspacing='1' cellpadding='2' border='0'>
                        <caption>Course Venue</caption>  
                           <tr>
                              <td class='tdDetailsLeft' align='right'>Venue :</td>
                              <td class='tdDetailsRight' align='left'>
                                 <select style='width: 155px;' name='refVenueID' id='refVenueID' onChange='jsGetMaxCapacity()';>   
                                    <option value='0'>- Select -</option>                  
                                       $ddlVenue
                                 </select>
                              </td>
                           </tr>

                           <tr>
                              <td class='tdDetailsLeft' align='right'>Max capacity :</td>
                              <td class='tdDetailsRight' align='left'>
                                 <input type = 'text' name='intMaxCapacity'  id='intMaxCapacity' value=\"$intMaxCapacity\" style='width: 150px;'>
                              </td>                  
                           </tr>
                     </table>

                     <script>
                        function jsGetMaxCapacity()
                        {
                           var intMaxCapacity = d('intMaxCapacity');
                           var VID = d('refVenueID').value;

                           $('#intMaxCapacity').hide('fast');

                           $.ajax(
                           {
                              type: 'POST',
                              url: 'ajaxfunctions.php',
                              data: 'header=text&type=getMaxCapacity&VenueID=' + VID,
                              success: function(data)
                              {
                                 //$('#intMaxCapacity').value = data;
                                 intMaxCapacity.value = data;                              
                              }
                           });
                           $('#intMaxCapacity').show('fast');
                        }
                  </script>";         

         return $strOutput;
         //return $this->renderTable("Venue");      
   }

   public function getList()
   {
      global $SystemSettings;

      $this->ListSQL("
         SELECT tblVenue.VenueID, tblVenue.strVenue AS Venue, txtAddress as Address, tblVenue.intMaxCapacity AS 'Max Capacity', tblVenue.txtAddress AS Address, tblVenue.txtNotes AS Notes, tblVenue.blnActive AS Active, 
                tblVenue.strLastUser AS 'Last User', tblVenue.dtLastEdit AS 'Last Edit'
         FROM tblVenue
         WHERE 1=1 $Where
         GROUP BY tblVenue.VenueID, tblVenue.strVenue, tblVenue.intMaxCapacity, tblVenue.txtAddress, tblVenue.txtNotes, tblVenue.blnActive, tblVenue.strLastUser, tblVenue.dtLastEdit
         ORDER BY tblVenue.strVenue");
      
      //print_rr($this->Columns["Select"]);
      return $this->renderTable("Venue List");
   }

   public static function Save(&$VenueID)
   {
      $db = new NemoDatabase("tblVenue", $VenueID, null, 0);

      $db->SetValues($_POST);

      $db->Fields[strLastUser] = $_SESSION['USER']->USERNAME;
      //$db->Fields[dtLastEdit] = date("YmdHi");
      $result = $db->Save();
      //print_rr($db->Fields);
      if($VenueID == 0) $VenueID = $db->ID[VenueID];

      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 tblVenue WHERE VenueID = ". $xdb->qs($key));
         }
         return "Records Deleted.";
      }
   }
}
?>
