<?php
error_reporting(0); //6dec25
include('config/minho.php');
include('config/autoloadClass.php');
$autoLoad		=	new autoloadClass();
$formClass		=	new formClass();
$project		=	isset($_POST['project'])	?  $_POST['project']	: "";
$model			=	isset($_POST['model'])		? str_replace("_"," ",$_POST['model']) : "";
$pricelistDate	=	isset($_POST['pricelist_date'])	? trim($_POST['pricelist_date'])	: "";
$action			=	isset($_POST['action']) ? trim(strip_tags(htmlentities(addslashes($_POST['action'])))) : "";
$mid			=	isset($_POST['mid']) ? trim(strip_tags(htmlentities(addslashes($_POST['mid'])))) : "";
$mtype			=	isset($_POST['mtype']) ? trim(strip_tags(htmlentities(addslashes($_POST['mtype'])))) : "";
$curphase		=	isset($_POST['curphase']) ? trim(strip_tags(htmlentities(addslashes($_POST['curphase'])))) : "";
$curblock		=	isset($_POST['curblock']) ? trim(strip_tags(htmlentities(addslashes($_POST['curblock'])))) : "";
$curlot			=	isset($_POST['curlot']) ? trim(strip_tags(htmlentities(addslashes($_POST['curlot'])))) : "";
$blockArr		=	array();
$phaseArr		=	array();
$lotArr			=	array();
$phase 	 		= "";
$selec			= "";
 
$lot = "";
$block = "";
$rPhase = ""; $rBlock = ""; $rLot = ""; $rBroker = ""; $rAgent = ""; $rProject = ""; $rModel = "";


/*
if($action=="edit" || $action=="view" || $action=="transfer"){
		$reserveArr = $formClass->getReservationDetails($mid); 
		$rPhase		= $reserveArr[0]['Phase'];
		$rBlock		= $reserveArr[0]['Block'];
		$rLot		= $reserveArr[0]['Lot'];
		$rBroker	= str_replace("_"," ",$reserveArr[0]['Broker']);
		$rAgent		= str_replace("_"," ",$reserveArr[0]['Agent']); 
		$rProject	= str_replace("_"," ",$reserveArr[0]['Project']); 
		$rModel		= str_replace("_"," ",$reserveArr[0]['Model']);  
}else{

}*/
$rPhase = $curphase;
$rBlock = $curblock;
$rLot = $curlot;


$disabled = ($action=="view") ? "disabled" : "";  
$project = str_replace('_',' ',$project);   
$pricelistDate	 = date('Y-m-d', strtotime($pricelistDate));
$modelSelect = array("field"=>"PricelistDetails","field_value"=>"PricelistDetails","table"=>"pricelist","project"=>$project,"model"=>$model,"pricelist_date"=>$pricelistDate,"action"=>$action);
 $s = $formClass->selectPricelistBox($modelSelect); 
 rsort($s);
if(!empty($s)){
		for($c=0; $c<count($s); $c++){
			$p = json_decode($s[$c]['PricelistDetails'],true);  
			if(count($p['details'])!=0){
				for($a=0; $a<count($p['details']); $a++){
							$phaseArr[]	= $p['details'][$a]['phase'];
							$blockArr[]	= $p['details'][$a]['block'];
					if(!empty($rBlock)){
						if($p['details'][$a]['phase']==$rPhase && $p['details'][$a]['block']==$rBlock){
							$lotArr[]	= $p['details'][$a]['lot'];
						}
					}else{
							$phaseArr[]	= $p['details'][$a]['phase'];
							$blockArr[]	= $p['details'][$a]['block']; 
							$lotArr[]	= $p['details'][$a]['lot'];
					}	
				} 
			}
		}
		
			$phase = '<select '.$disabled.' name="current_phase" id="current_phase" class="select2 form-control text-right">';
			$phase .= '<option value="">- Select Phase -</option>'; 
			sort($phaseArr); 
			$phaseUnique = array_unique($phaseArr);
			foreach($phaseUnique as $phaseVal){
				$selec = ($rPhase==$phaseVal) ? "selected" : "";
				$phase .= '<option value="'.$phaseVal.'" '.$selec.'>'.$phaseVal.'</option>'; 
			} 
			$phase .= '</select>'; 
			
			$block = '<select '.$disabled.' name="current_block" id="current_block" class="select2 form-control text-right">';
			$block .= '<option value="">- Select Block -</option>';
			sort($blockArr);
			$blockUnique = array_unique($blockArr);
			foreach($blockUnique as $blockVal){
				$selec = ($rBlock==$blockVal) ? "selected" : "";
				$block .= '<option value="'.$blockVal.'" '.$selec.'>'.$blockVal.'</option>'; 
			} 			
			$block .= '</select>';


			$lot = '<select '.$disabled.' name="current_lot" id="current_lot" class="select2 form-control text-right">';
				$lot .= '<option value="">- Select Lot -</option>';
			sort($lotArr);
			$lotUnique = array_unique($lotArr);
			foreach($lotUnique as $lotVal){
				$selec = (html_entity_decode($rLot)==html_entity_decode($lotVal)) ? "selected" : "";
				$lot .= '<option value="'.$lotVal.'" '.$selec.'>'.$lotVal.'</option>'; 
			} 
			$lot .= '</select>';
			
	
}else{
	$phase	 = '<input type="text"  disabled class="form-control" name="current_phase" id="current_phase">';
	$lot	 = '<input type="text" disabled class="form-control" name="current_lot" id="current_lot">';
	$block	 = '<input type="text" disabled class="form-control" name="current_block" id="current_block">';
} 
$main			= array();
$main['phase']	= $phase;
$main['block']	= $block;
$main['lot']	= $lot; 

$resultArr 						= array();
$resultArr['output']	  		= $main;  
echo json_encode($resultArr);