<?php 
$model = "";
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){
 	ob_start("ob_gzhandler");	
}else{
	ob_start();
}


include('config/minho.php');
include('config/autoloadClass.php');

$autoLoad 			= new autoloadClass();
$formClass 			= new formClass();
$selType 			= isset($_POST['select_type']) ? trim(strip_tags($_POST['select_type'])) : "";
$selCondition 		= isset($_POST['select_condition_value']) ? trim(strip_tags(htmlentities(addslashes($_POST['select_condition_value'])))) : "";
$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'])))) : "";
$mmodel 			= isset($_POST['mmodel']) ? str_replace("_", " ", trim(strip_tags(htmlentities(addslashes($_POST['mmodel']))))) : "";
$mproj 				= isset($_POST['mproj']) ? str_replace("_", " ", trim(strip_tags(htmlentities(addslashes($_POST['mproj']))))) : "";
$mrdate 			= isset($_POST['mrdate']) ? trim($_POST['mrdate']) : "";
$default_pricelist 	= isset($_POST['default_pricelist']) ? trim($_POST['default_pricelist']) : "";
$custom 			= isset($_POST['custom']) ? trim($_POST['custom']) : "";
$reserveArr 		= array();
$promoArr 			= array();
$priceArr 			= array();
$brokerArr 			= array();
$rPhase 			= "";
$rBlock 			= "";
$rLot 				= "";
$rBroker 			= "";
$rAgent 			= "";
$rProject 			= "";
$rModel 			= "";
$rReserveDate 		= "";
$rEmployeeType 		= "";
$rEmployeePosition 	= "";
$rBuyerProfile 		= "";
$rEmployeeTypeCo1 	= "";
$rEmployeeTypeCo2 	= "";
$rEmployeeTypeCo3 	= "";
$coBorrowerDetails 	= "";
$rPositionCo1 		= "";
$rPositionCo2		= "";
$rPositionCo3 		= "";
$rTerms 			= "";
$rPricelistDate 	= '';

if ($action == "edit" || $action == "transfer" || $action == "view") {

    if ($mtype == 'reservation') {

        $reserveArr		= $formClass->getReservationDetails($mid);
        $rPhase 		= $reserveArr[0]['Phase'];
        $rBlock 		= $reserveArr[0]['Block'];
        $rLot 			= $reserveArr[0]['Lot'];
        $rBroker 		= $reserveArr[0]['Broker'];
        $rAgent 		= $reserveArr[0]['Agent'];
        $rProject 		= str_replace("_", " ", $reserveArr[0]['Project']);
        $rModel 		= str_replace("_", " ", $reserveArr[0]['Model']);
        $rReserveDate 	= $reserveArr[0]['ReservationDate'];
        $rPricelistDate = $reserveArr[0]['PricelistDate'];
        $rEmployeeType 	= $reserveArr[0]['EmployeeType'];
        $rEmployeePosition 	= $reserveArr[0]['Position'];
        $rBuyerProfile 		= $reserveArr[0]['BuyerProfile'];
        $coBorrowerDetails 	= $reserveArr[0]['CoBorrowerDetails'];
        $coDetails 			= json_decode($coBorrowerDetails, true);

        if (count($coDetails['details'] != 0)) {
            $rEmployeeTypeCo1 = $coDetails['details'][0]['employee_type'];
            $rEmployeeTypeCo2 = $coDetails['details'][1]['employee_type'];
            $rEmployeeTypeCo3 = $coDetails['details'][2]['employee_type'];
            $rPositionCo1 = $coDetails['details'][0]['position'];
            $rPositionCo2 = $coDetails['details'][1]['position'];
            $rPositionCo3 = $coDetails['details'][2]['position'];
        }
    }
}

if ($selType == "pricelist") {

    $pproject 			= $mproj;
    $pmodel 			= $mmodel;
    $dDate 				= date('Y-m-d', strtotime($default_pricelist));
    $sDate 				= date('Y-m-d', strtotime($mrdate));
    $curDate	 		= date('Y-m-d');
    $preservation_date 	= (empty($mrdate) || ($mrdate == "")) ? $curDate : $sDate;
    $placeholder 		= 'Select Pricelist Date';
    $default 			= $dDate;
    $project 			= str_replace("_", " ", $pproject);
    $model 				= str_replace("_", " ", strtoupper($pmodel));
    $reserveDate 		= $preservation_date;
    $price 				= $formClass->generatePricelist($action, $placeholder, $default, $project, $model, $reserveDate, $custom);
    $actionTask 		= ($action == "view") ? "disabled" : "";
    $html 				= '<select name="pricelist_date" class="form-control select2 text-right" id="pricelist_date" '.$actionTask.'>';

    if (!empty($placeholder)) {
        $html .= '<option value="-">'.$placeholder.'</option>';
    }

    if (!empty($price)) {
        for ($c = 0; $c < count($price); $c++) {
            $value 		= $price[$c]['PricelistDate'];
            $option 	= date('m/d/Y', strtotime($price[$c]['PricelistDate']));
            $default 	= date('m/d/Y', strtotime($default));
            $value 		= date('m/d/Y', strtotime($value));

            if ($value == $default) {
                
                $html .= '<option value='.$value.' selected="selected">'.$option.'</option>';

            } else {
                
                $html .= '<option value='.$value.'>'.$option.'</option>';

            }
        }
    }

    $html .= '</select>';
    echo $html;
}

echo "<script>";

echo "
var project		= $('#current_project').val();
var model 		= $('#current_model').val();
var priceDate 	= $('#pricelist_date').val();
var rpriceDate 	= $('#reservepricedate').val();
var rdate 		= $('#reservation_date').val();

loadPhaseBlockLot(project, model, rpriceDate, '$rPhase', '$rBlock', '$rLot');
";

echo "
$('#pricelist_date').on('change', function () {
    var project = $('#current_project').val();
    var model = $('#current_model').val();
    var priceDate = $('#pricelist_date').val();
    loadPhaseBlockLot(project, model, priceDate, '$rPhase', '$rBlock', '$rLot');
});
";

echo " </script>";