<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/
//
//$Id: OfferController, v 1.0 2018/12/27 12:56:15
//
class TestdriveController extends Controller {
        public function indexAction()    {
                $ObjTestdrive    	= 	new TestdriveModel();
                $ObjBrand               =       new BrandModel();         
                $languageIDs		= 	$this->ObjView->languageIDs;		
                $link	=	'testdrive/index/';
                if (isset($_POST['search'])){			
                        if (isset($_POST['searchName']) && $_POST['searchName'] != ""){
                                $link .= "searchName/" . $this->encodeUrlString($_POST['searchName']) . "/";
                        }			
                        if (isset($_POST['searchEmail']) && $_POST['searchEmail'] != ""){
                                $link .= "searchEmail/" . $this->encodeUrlString($_POST['searchEmail']) . "/";
                        }			
                        if (isset($_POST['searchShowroom']) && $_POST['searchShowroom'] != ""){
                                $link .= "searchShowroom/" . $this->encodeUrlString($_POST['searchShowroom']) . "/";
                        }
                         if (isset($_POST['searchModel']) && $_POST['searchModel'] != ""){
                                $link .= "searchModel/" . $this->encodeUrlString($_POST['searchModel']) . "/";
                        }
                        if ($_POST['brandID']>0) {
                            $link .= "frID/" . $_POST['brandID'] . "/";
                        }
                        $this->setRedirect($link);
                }
                else{
//                        $sortby		=	$this->getRequestValue("sortby");
//                        $orderby	=	($this->getRequestValue("orderby")=="")?"asc":$this->getRequestValue("orderby");
                $searchName='';			
                        if($this->getRequestValue('searchName')){
                                $searchName		= 	$this->decodeUrlString($this->getRequestValue('searchName'));
                                $link.="searchName/".$this->encodeUrlString($searchName). "/";
                        } $searchEmail='';			
                        if($this->getRequestValue('searchEmail')){
                                $searchEmail		= 	$this->decodeUrlString($this->getRequestValue('searchEmail'));
                                $link.="searchEmail/".$this->encodeUrlString($searchEmail). "/";
                        }
                         $searchShowroom='';			
                        if($this->getRequestValue('searchShowroom')){
                                $searchShowroom		= 	$this->decodeUrlString($this->getRequestValue('searchShowroom'));
                                $link.="searchShowroom/".$this->encodeUrlString($searchShowroom). "/";
                        }
                         $searchModel='';			
                        if($this->getRequestValue('searchModel')){
                                $searchModel		= 	$this->decodeUrlString($this->getRequestValue('searchModel'));
                                $link.="searchModel/".$this->encodeUrlString($searchModel). "/";
                        }
                        $brandID = '';
                        if ($this->getRequestValue('frID')) {
                            $brandID = $this->getRequestValue('frID');
                        }
                }		
				if (isset($_POST['export'])) {
					$resOrdersCsv         = $ObjTestdrive->listTestdrive(0,'',$languageIDs,$searchName,$searchEmail,$searchShowroom,$searchModel,$brandID,'Export');	
					$ObjCsv               = new Csv();
					$csvOutputHeader      = array(
						$this->getLanguageValue("lblName"),
						$this->getLanguageValue("lblPhone"),
						$this->getLanguageValue("lblEmailID"),
						$this->getLanguageValue("lblShowroom"),
						$this->getLanguageValue("lblModel"),
						$this->getLanguageValue("lblModel"),
						$this->getLanguageValue("lblMenuBrands"),
						$this->getLanguageValue("lblWhenBuy"),
						$this->getLanguageValue("lblCreatedDate"));
					$csvOutputFileName    = "test_drive_list_" . date('d_m_Y_h_i_s_A');
					$count                = $resOrdersCsv->num_rows;
					if ($count > 0) {
						for ($i = 0; $i < $count; $i++) {
							$orders                                  = $resOrdersCsv->fetch_array(MYSQLI_ASSOC);
							$resOrdersCsvOut[$i]['name']      		 = $this->encryptDecrypt("decrypt",$orders['customer_name']);
							$resOrdersCsvOut[$i]['phone']      		 = $this->encryptDecrypt("decrypt",$orders['customer_phone']);
							$resOrdersCsvOut[$i]['email']  	 		 = $this->encryptDecrypt("decrypt",$orders['emailID']); 
							$resOrdersCsvOut[$i]['showroom']  	     = $orders['name']; 
							$resOrdersCsvOut[$i]['model']  	 		 = $orders['model']; 
							$resOrdersCsvOut[$i]['brand']  	 		 = $orders['brandName']; 
							$resOrdersCsvOut[$i]['currentcar']  	 = $orders['currentcar']; 
							$resOrdersCsvOut[$i]['whenBuy']  	 	 = $orders['whenBuy']; 
							$resOrdersCsvOut[$i]['createdDate']  	 = ($orders['CreatedDate']!='0000-00-00 00:00:00')?$orders['CreatedDate']:'';
							unset($orders);
						}
					}
					$csvOutputData = $ObjCsv->output($csvOutputFileName . '.csv', $resOrdersCsvOut, $csvOutputHeader);
					exit;
				}				
                list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page', $rows = 10); 

                
                $listTestdrive    = $ObjTestdrive->listTestdrive($limitFrom,$limitTo,$languageIDs,$searchName,$searchEmail,$searchShowroom,$searchModel,$brandID);	
                $resPagesCount = $this->setPagesCount($key = 'page');
                $this->ObjView->assign("listTestdrive", $listTestdrive);
                $this->ObjView->assign("searchName", $searchName);
                $this->ObjView->assign("searchEmail", $searchEmail);
                $this->ObjView->assign("searchShowroom", $searchShowroom);
                $this->ObjView->assign("searchModel", $searchModel);
                $this->ObjView->assign("brandID", isset($brandID) ? $brandID : '');
                $brandParams         = array(
                    'languageIDs' => $languageIDs
                );       
                $listBrand      = $ObjBrand->listBrand($brandParams);  
                $this->ObjView->assign('listBrand', $listBrand);
                $this->ObjView->display('Testdrive.tpl');
        }
       
}