<?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: LoandetailsController, v 1.0 2018/12/27 12:56:15
//
class LoandetailsController extends Controller {
        public function indexAction()    {
        $ObjProductList         = new ProductListModel();
		$ObjBrand               =       new BrandModel();  
        $languageIDs            = $this->ObjView->languageIDs;
		$settingsDateFormat = $this->getSettingValue("site_settings", "date_format");
		$settingsTimeFormat = $this->getSettingValue("site_settings", "time_format"); 
        $languageIDs['Current'] = $languageIDs['Content'];
        $link	=	'loandetails/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 ($_POST['brandID']>0) {
                            $link .= "frID/" . $_POST['brandID'] . "/";
                        }
                        $this->setRedirect($link);
                }
                else{

						$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). "/";
                        }
						 $brandID = '';
                        if ($this->getRequestValue('frID')) {
                            $brandID = $this->getRequestValue('frID');
                        }
                        
                }
				if (isset($_POST['export'])) {
					$resOrdersCsv         = $ObjProductList->listLoanDetails(0,'',$languageIDs,$searchName,$searchEmail,$brandID,'Export');
					//echo "<pre>";print_r($resOrdersCsv);exit;	
					$count                = $resOrdersCsv->num_rows;					
					$ObjCsv               = new Csv();
					$csvOutputHeader      = array(
						$this->getLanguageValue("lblName"),
						$this->getLanguageValue("lblPhone"),
						$this->getLanguageValue("lblEmailID"),
						$this->getLanguageValue("lblMenuBrands"),
						$this->getLanguageValue("lblLocation"),
						$this->getLanguageValue("lblEmploymentType"),
						$this->getLanguageValue("lblMonthlyIncome"),
						$this->getLanguageValue("lblCreditcardlimit"),
						$this->getLanguageValue("lblMonthlypayment"),
						$this->getLanguageValue("lblVehicleTitle"),
						$this->getLanguageValue("lblAppliedDate")
						);
					$csvOutputFileName    = "loan_list_" . date('d_m_Y_h_i_s_A');
					
					if ($count > 0) {
						for ($i = 0; $i < $count; $i++) {
							$orders                                  = $resOrdersCsv->fetch_array(MYSQLI_ASSOC);
							$resOrdersCsvOut[$i]['name']      	 	 = $orders['firstname']." ".$orders['lastname'];
							$resOrdersCsvOut[$i]['phone']			 = $orders['phone'];
							$resOrdersCsvOut[$i]['email']  	     	 = $orders['emailID']; 
							$resOrdersCsvOut[$i]['brand']  	 		 = $orders['brandName'];
							$resOrdersCsvOut[$i]['location']  	 		 = $orders['location']; 
							$resOrdersCsvOut[$i]['employmenttype']  	 = $orders['employmenttype']; 
							$resOrdersCsvOut[$i]['monthlyincome']  	 	 = $orders['monthlyincome']; 
							$resOrdersCsvOut[$i]['totalcreditcardlimit'] = $orders['totalcreditcardlimit']; 
							$resOrdersCsvOut[$i]['loanmonthlyPayment']   = $orders['loanmonthlyPayment'];
							$resOrdersCsvOut[$i]['vehicleTitle']   = $orders['vehicleTitle']; 							
							$resOrdersCsvOut[$i]['createdDate']   = $orders['createdDate']; 							
							unset($orders);
						}
					}
					//echo "<pre>";print_r($resOrdersCsvOut);exit;
					$csvOutputData = $ObjCsv->output($csvOutputFileName . '.csv', $resOrdersCsvOut, $csvOutputHeader);
					//print_r($csvOutputData);exit;
					exit;
				}	
        
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page', $rows = 10); 

		$resloanlist    = $ObjProductList->listLoanDetails($limitFrom,$limitTo,$languageIDs,$searchName,$searchEmail,$brandID);
			//echo "<pre>";print_r($resloanlist);exit;
			$brandParams         = array(
                    'languageIDs' => $languageIDs
                );       
        $listBrand      = $ObjBrand->listBrand($brandParams);  
        $this->ObjView->assign('listBrand', $listBrand);
		$resPagesCount = $this->setPagesCount($key = 'page');
		$this->ObjView->assign("resloanlist", $resloanlist);
		$this->ObjView->assign("searchName", $searchName);
        $this->ObjView->assign("searchEmail", $searchEmail);
		$this->ObjView->assign('settingsDateFormat', $settingsDateFormat);
		$this->ObjView->assign('settingsTimeFormat', $settingsTimeFormat);
		 $this->ObjView->assign("brandID", isset($brandID) ? $brandID : '');
		$this->ObjView->display('Loanlist.tpl');
	}
}