<?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: RequestaserviceController.php, v 1.0 2019/05/17 
//
class RequestaserviceController extends Controller {
      public function indexAction() {
        $ObjServiceModel= new ServiceCenterModel(); 
        $ObjProductList = new ProductListModel();
        $ObjBrand       = new BrandModel();
        $ObjOtpCountry  = new OtpCountryModel();
        $ObjEmailtemplate= new EmailtemplateModel();
        $languageIDs    = $this->ObjView->languageIDs; 
        $menuItemID                       = (int) $this->getRequestValue("itemID");   
        $customGroupBy='bodyStyle';
        $default_catalog_listing     = 'price_asc';
		$settingsDateFormat = $this->getSettingValue("site_settings", "date_format");
        $bodyStyle=array();
        $custCode=array();
		$sucssMessage = 'no';
        //$brandKey = 'bmw';
        $brandKey = FRANCHISEE;
        /* if brand key found in url */
        if ($brandKey != '') {
           
            $paramsBrand            = array(
                'languageIDs' => $languageIDs,
                'brandKey'    => $brandKey
            );
            $brandInfo         = $ObjBrand->getBrand($paramsBrand);
            
            $brandID          = $brandInfo['brandID'];
           
            if($brandID>0) {
                //get Subbrands if any
                $getSub = $ObjBrand->getSubBrandIDs($brandID);
                //$brandIDs = 1;
				$brandIDs = $this->ObjCommon->brandID;
                if(!empty($getSub) && !empty($getSub['brandIDs'])) {
                    $brandIDs .= ",".$getSub['brandIDs'];
                }
            }
        }        
        $productType                 = array(
                'productTypeID' => 1,
                'productTypeKey' => 'product_product',
                'productTypeUrlKey' => '',
                'isFeatured' => 1,
                'limitMainListing' => '',
                'limitDefListing' => '',
                'productTypeName' => $this->getLanguageValue('lblProducts')
            );
        $args                        = array(
                'requestedBy' => 'customer',
                'requestPage' => 'category_prod_listing',
                'limitFrom' => 0,
                'limitTo' => '',
                'languageIDs' => $languageIDs,
                'productType' => $productType,
                'brandID' => $brandID,
                'businessID' => '',
                'brandIDs' => $brandIDs,
                'categoryID' => '',
                'categoryIDs' =>'',
                'customOrderBy' =>$default_catalog_listing,
                'searchKeyword' =>  array(),
                'attrDataObj' => array(),
                'priceRangeMin' => '',
                'priceRangeMax' => '',
                'emiRangeMin' => '',
                'emiRangeMax' => '',
                'discountRange' => '',
                'attrQueryStringArray' => '',
                'userGroupID' => (int) $this->ObjCommon->REGISTERED_USER_GROUP_ID,
                'isProductDeal' => 'No',
                'showpage' => '',
                'menuItemID' => $menuItemID,
                'customGroupBy'=>$customGroupBy,
                'bodyStyle'=>$bodyStyle,
                'bmwCode'=>'',
                'custom'=>'Yes',
                'custCode'=>$custCode,
                'frontEnd'=>'Yes'
            );
       
        $resAllProducts              = $ObjProductList->testDriveProductModels($args); 
        $productarray=array();
        foreach($resAllProducts as $key=>$products){
            $productarray[$key][0]=$products['productCategoryName']." ".$products['bodyStyle'];
            $productarray[$key][1]=$products['productCategoryName']." ".$products['bodyStyle'];
        }        
        $selCat = '';
        if(!empty($this->ObjSession->getSession("MODELTEST"))) {            
            $selCat = $this->ObjSession->getSession("MODELTEST");
        }
        $params        = array(
            'limitFrom' => 0,
            'limitTo' => '',
            'languageIDs' => $languageIDs,
            'searchName' => '',
            'status' => 'A'
        );
        if($this->ObjForm->isValid('formService','submitFormService')){
            $siteUrl                  = rtrim($this->getSettingValue('site_settings', 'global_http_url'), '/');
            $redUri                   = str_replace($siteUrl."/".$this->ObjView->_currLanguageLCID."/",'',$_SERVER['SCRIPT_URI']);
            $redUri                   = str_replace($siteUrl."/",'',$redUri);
            //echo $redUri;exit;
            if(empty($redUri)) {
                $redUri = 'requestaservice/';
            } 
			$_POST['brandID'] = $this->ObjCommon->brandID;		
			$_POST['prefDate']    = $this->convertDateForSQL($_POST['prefDate'], $settingsDateFormat);
            $insert =   $ObjServiceModel->saveServiceInfo($languageIDs,$_POST);
            if($insert){
				$sucssMessage = 'yes';
                    $languageIDs['Content']=$languageIDs['Current'];
                    $paramsGet       = array(
                    'serviceID' => $_POST['serviceID'],
                    'languageIDs' => $languageIDs
                    );
                    #mail to user
                    $emailbody              = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'servicereq-notification-email-user');
                    $from                   = array(
                        $emailbody['emailFromEmail'],
                        $emailbody['emailFromName']
                    );
                    $subject                = $emailbody['emailSubject'];
                    $userName               = $_POST['subtitle'].$_POST['name'];
                    $getService             = $ObjServiceModel->getService($paramsGet);
                    //email body
                    $bodytag                = str_replace('{$username}', $userName, $emailbody['emailHtml']);
                    $bodytag                = str_replace('{$modelname}', $_POST['model'], $bodytag);
                     $bodytag                = str_replace('{$serviceCenterName}', $getService['name'], $bodytag);
                     $bodytag                = str_replace('{$serviceCenterAddress}', $getService['address'], $bodytag);
                    //$bodytag                = str_replace('{$serviceCenterPhone}', $getService['phone'], $bodytag);
					$phone 					 = stripslashes(preg_replace('/\s+/', '', $getService['phone']));
					$bodytag                = str_replace('{$serviceCenterPhone}', '<a href="tel:'.$phone.'">'.$phone.'</a>', $bodytag);
                    $bodytag                = str_replace('{$serviceCenterEmail}', $getService['email'], $bodytag);
                    //$bodytag                = str_replace('{$latitude}', $getService['latitude'], $bodytag);
                    //$bodytag                = str_replace('{$longitude}', $getService['longitude'], $bodytag);
                    $bodytag                = str_replace('{$mapLink}', $getService['mapLink'], $bodytag);
					
					// if($_POST['serviceID'] == '1'){
						// $mapLink = "https://goo.gl/maps/7noFaQykVP72";
					// else if($_POST['serviceID']== '2'){
						// $mapLink = "https://goo.gl/maps/Ze5QxPvcVvz";
					// }
					// else if($_POST['serviceID'] == '3'){
						// $mapLink = "https://goo.gl/maps/D6wAy9XuBxu";
					// }
					// else if($_POST['serviceID'] == '4'){
						// $mapLink = "https://goo.gl/maps/gXqaDR9kvP52";
					// }
					// else if($_POST['serviceID'] == '5'){
						// $mapLink = "https://goo.gl/maps/gXqaDR9kvP52";
					// }
					// else{
						// $mapLink = "https://goo.gl/maps/7noFaQykVP72";
					// }
                    $this->ObjView->assign('emailbody', $bodytag);
                    $body                          = $this->ObjCommon->fetch("email_templates/Layout.tpl", $languageIDs);
                    $mailSettings                  = $this->getSettingValue('mail_settings');
                    $mailSettings['smtp_password'] = $this->encryptDecrypt("decrypt", $mailSettings['smtp_password']);
                    $ObjMail                       = new Mail($mailSettings);
                    $to        =  trim(addslashes($_POST['emailID']));
                    $ObjMail->clearAddresses();
                    $ObjMail->IsHTML();
                    $ObjMail->sendMail($from, $to, $subject, $body);
                    if ($ObjMail->Send()) {
                    }
					if($this->getSettingValue('product_cart_settings', 'enable_book_service_mail')=='Yes') {
					
						$emailbody              = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'servicereq-notification-email-admin');
						$from                   = array(
							$emailbody['emailFromEmail'],
							$emailbody['emailFromName']
						);
						$subject                = $emailbody['emailSubject'];
						$userName               = $_POST['subtitle'].$_POST['name'];
                    
						//$emailID                = $this->getSettingValue('site_settings', 'global_notification_email');
						$emailID                = $this->getSettingValue('product_cart_settings', 'book_service_appointment_email');
						//email body
						$phone                  = "+" . $_POST['countrycode']." ".$_POST['phone'];
						$bodytag                = str_replace('{$userName}', $userName, $emailbody['emailHtml']);
						$bodytag                = str_replace('{$modelname}', $_POST['model'], $bodytag);
						$bodytag                = str_replace('{$userSurName}', $_POST['surname'], $bodytag);
						$bodytag                = str_replace('{$userPhone}', $phone, $bodytag);
						$bodytag                = str_replace('{$userEmail}', $_POST['emailID'], $bodytag);
						$bodytag                = str_replace('{$serviceCenter}', $getService['name'], $bodytag);                    
						$bodytag                = str_replace('{$vehicleReg}', $_POST['vehiclereg'], $bodytag);
						$bodytag                = str_replace('{$modelYear}', $_POST['modelyear'], $bodytag);
						$bodytag                = str_replace('{$prefDate}', $_POST['prefDate'], $bodytag);
						$bodytag                = str_replace('{$dropoffTime}', $_POST['dropoffTime'], $bodytag);
						$serviceItems = '';
						if(!empty($_POST['serviceitems'])) {
							foreach($_POST['serviceitems'] as $item) {
								$serviceItems = ($serviceItems=='')?$item:$serviceItems.", ".$item;
							}
						}
						$bodytag                = str_replace('{$serviceItems}', $serviceItems, $bodytag);                    
						$bodytag                = str_replace('{$serviceDesc}', $_POST['serviceDesc'], $bodytag);
						$this->ObjView->assign('emailbody', $bodytag);
						$body                          = $this->ObjCommon->fetch("email_templates/Layout.tpl", $languageIDs);
						$mailSettings                  = $this->getSettingValue('mail_settings');
						$mailSettings['smtp_password'] = $this->encryptDecrypt("decrypt", $mailSettings['smtp_password']);
						$ObjMail                       = new Mail($mailSettings);
						$emailID                       = trim(addslashes($emailID));
						$ObjMail->clearAddresses();
						$ObjMail->IsHTML();
						$ObjMail->sendMail($from, $emailID, $subject, $body);
						if ($ObjMail->Send()) {
						}
					}
					echo "<script> fbq('track', 'Lead');</script>";
                    $this->setMessage("language.lblThankYouForTestdrive", "success");
                    $this->setRedirect($redUri);
            }
                
        }
        $languageIDs['Content'] = $languageIDs['Current'];
        $resOTPCountry  =   $ObjOtpCountry->listOtpCountryOptions(array('status'=>'A','languageIDs'=>$languageIDs));
        $this->ObjView->assign('resCountry', $resOTPCountry);
        $arr = array();
        $arr[0][0] = '8.00 - 8.30';
        $arr[0][1] = '8.00 - 8.30';
        $arr[1][0] = '8.30 - 9.00';
        $arr[1][1] = '8.30 - 9.00';
        $arr[2][0] = '9.00 - 9.30';
        $arr[2][1] = '9.00 - 9.30';
        $arr[3][0] = '9.30 - 10.00';
        $arr[3][1] = '9.30 - 10.00';
        $arr[4][0] = '10.00 - 10.30';
        $arr[4][1] = '10.00 - 10.30';
        $arr[5][0] = '10.30 - 11.00';
        $arr[5][1] = '10.30 - 11.00';
        $arr[6][0] = '11.00 - 11.30';
        $arr[6][1] = '11.00 - 11.30';
        $arr[7][0] = '11.30 - 12.00';
        $arr[7][1] = '11.30 - 12.00';
        $arr[8][0] = '12.00 - 12.30';
        $arr[8][1] = '12.00 - 12.30';
        $arr[9][0] = '12.30 - 13.00';
        $arr[9][1] = '12.30 - 13.00';
        $arr[10][0] = '14.00 - 14.30';
        $arr[10][1] = '14.00 - 14.30';
        $arr[11][0] = '14.30 - 15.00';
        $arr[11][1] = '14.30 - 15.00';
        $arr[12][0] = '15.00 - 15.30';
        $arr[12][1] = '15.00 - 15.30';
        $arr[13][0] = '15.30 - 16.00';
        $arr[13][1] = '15.30 - 16.00';
        $arr[14][0] = '16.00 - 16.30';
        $arr[14][1] = '16.00 - 16.30';
		$params['franchisee'] = FRANCHISEE;
        $listServices = $ObjServiceModel->testdriveCenters($params);
        $this->ObjView->assign('listServices', $listServices);
        $this->ObjView->assign('arr', $arr);
        $this->ObjView->assign('productarray', $productarray);
        $this->ObjView->assign('selCat', $selCat);
		$this->ObjView->assign('sucssMessage', $sucssMessage);
        $this->ObjView->assign('currDate', date("Y/m/d",strtotime(date("Y-m-d"))));
        $this->ObjView->display('RequestAservice.tpl');
    }
	
    public function testindexAction() {
        $showroomsModel = new ShowroomsModel(); 
        $ObjProductList = new ProductListModel();
        $ObjBrand       = new BrandModel();
        $languageIDs    = $this->ObjView->languageIDs; 
        $menuItemID                       = (int) $this->getRequestValue("itemID");   
        $customGroupBy='bodyStyle';
        $default_catalog_listing     = 'price_asc';
        $bodyStyle=array();
        $custCode=array();
        //$brandKey = 'bmw';
		$brandKey = FRANCHISEE;
        /* if brand key found in url */
        if ($brandKey != '') {
           
            $paramsBrand            = array(
                'languageIDs' => $languageIDs,
                'brandKey'    => $brandKey
            );
            $brandInfo         = $ObjBrand->getBrand($paramsBrand);
            
            $brandID          = $brandInfo['brandID'];
           
            if($brandID>0) {
                //get Subbrands if any
                $getSub = $ObjBrand->getSubBrandIDs($brandID);                
                //$brandIDs = 1;
				$brandIDs = $this->ObjCommon->brandID;
                if(!empty($getSub) && !empty($getSub['brandIDs'])) {
                    $brandIDs .= ",".$getSub['brandIDs'];
                }
            }
        }
		
        $productType                 = array(
                'productTypeID' => 1,
                'productTypeKey' => 'product_product',
                'productTypeUrlKey' => '',
                'isFeatured' => 1,
                'limitMainListing' => '',
                'limitDefListing' => '',
                'productTypeName' => $this->getLanguageValue('lblProducts')
            );
        $args                        = array(
                'requestedBy' => 'customer',
                'requestPage' => 'category_prod_listing',
                'limitFrom' => 0,
                'limitTo' => '',
                'languageIDs' => $languageIDs,
                'productType' => $productType,
                'brandID' => $brandID,
                'businessID' => '',
                'brandIDs' => $brandIDs,
                'categoryID' => '',
                'categoryIDs' =>'',
                'customOrderBy' =>$default_catalog_listing,
                'searchKeyword' =>  array(),
                'attrDataObj' => array(),
                'priceRangeMin' => '',
                'priceRangeMax' => '',
                'emiRangeMin' => '',
                'emiRangeMax' => '',
                'discountRange' => '',
                'attrQueryStringArray' => '',
                'userGroupID' => (int) $this->ObjCommon->REGISTERED_USER_GROUP_ID,
                'isProductDeal' => 'No',
                'showpage' => '',
                'menuItemID' => $menuItemID,
                'customGroupBy'=>$customGroupBy,
                'bodyStyle'=>$bodyStyle,
                'bmwCode'=>'',
                'custom'=>'Yes',
                'custCode'=>$custCode,
                'frontEnd'=>'Yes'
            );
       
        $resAllProducts              = $ObjProductList->testDriveProductModels($args); 
        $productarray=array();
        foreach($resAllProducts as $key=>$products){
            $productarray[$key][0]=$products['productCategoryName']." ".$products['bodyStyle'];
            $productarray[$key][1]=$products['productCategoryName']." ".$products['bodyStyle'];
        }        
        $selCat = '';
        
        $params        = array(
            'limitFrom' => 0,
            'limitTo' => '',
            'languageIDs' => $languageIDs,
            'searchName' => '',
            'status' => 'A'
        );
        if($this->ObjForm->isValid('formService','submitFormService')){
            echo "<pre>";print_r($_POST);exit;
        }
        $listShowrooms = $showroomsModel->testdriveShowrooms($params);
        $this->ObjView->assign('listShowrooms', $listShowrooms);
        $this->ObjView->assign('productarray', $productarray);
        $this->ObjView->assign('selCat', $selCat);
        $this->ObjView->display('RequestAservicenew.tpl');
    }
}