<?php

require_once 'header.php';
$controller = new ControllerPhoto();
$controllerRealEstate = new ControllerRealEstate();
$extras = new Extras();
$realestate_id = $extras->decryptQuery1(KEY_SALT, $_SERVER['QUERY_STRING']);
$photo_realestate_delete = $extras->decryptQuery2(KEY_SALT, $_SERVER['QUERY_STRING']);
if ($realestate_id != null) {
    $photos_realestate = $controller->getPhotosByRealEstateId($realestate_id);
    $selected_realestate = $controllerRealEstate->getRealEstateByRealEstateId($realestate_id);
}
if ($photo_realestate_delete != null) {
    $realestate_id = $photo_realestate_delete[0];
    $photo_id = $photo_realestate_delete[1];
    $controller->deletePhoto($photo_id, 1);
    $viewUrl = $extras->encryptQuery1(KEY_SALT, 'realestate_id', $realestate_id, 'photo_realestate_view.php');
    echo "<script type='text/javascript'>location.href='{$viewUrl}';</script>";
}
if ($realestate_id == null && $photo_realestate_delete == null) {
    echo "<script type='text/javascript'>location.href='403.php';</script>";
}
?>


<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<?php

require_once 'header.php';
$controller = new ControllerRealEstate();
$controllerAgent = new ControllerAgent();
$controllerPropertyType = new ControllerPropertyType();
$agents = $controllerAgent->getAgents();
$propertytypes = $controllerPropertyType->getPropertyTypes();
$extras = new Extras();
$realestate_id = $extras->decryptQuery1(KEY_SALT, $_SERVER['QUERY_STRING']);
if ($realestate_id != null) {
    $realestate = $controller->getRealEstateByRealEstateId($realestate_id);
    if (isset($_POST['submit'])) {
        $itm = $realestate;
        $itm->address = htmlspecialchars(trim(strip_tags($_POST['address'])), ENT_QUOTES);
        $itm->baths = htmlspecialchars(trim(strip_tags($_POST['baths'])), ENT_QUOTES);
        $itm->beds = trim(strip_tags($_POST['beds']));
        $itm->built_in = trim(strip_tags($_POST['built_in']));
        $itm->country = htmlspecialchars(trim(strip_tags($_POST['country'])), ENT_QUOTES);
        $itm->desc1 = $extras->removeHttp(htmlspecialchars(trim(strip_tags($_POST['desc1'])), ENT_QUOTES));
        $itm->featured = htmlspecialchars(trim(strip_tags($_POST['featured'])), ENT_QUOTES);
        $itm->lat = trim(strip_tags($_POST['lat']));
        $itm->lon = trim(strip_tags($_POST['lon']));
        $itm->lot_size = trim(strip_tags($_POST['lot_size']));
        $itm->price = htmlspecialchars(trim(strip_tags($_POST['price'])), ENT_QUOTES);
        $itm->price_per_sqft = htmlspecialchars(trim(strip_tags($_POST['price_per_sqft'])), ENT_QUOTES);
        $itm->property_type = trim(strip_tags($_POST['property_type']));
        $itm->rooms = trim(strip_tags($_POST['rooms']));
        $itm->sqft = trim(strip_tags($_POST['sqft']));
        $itm->status = trim(strip_tags($_POST['status']));
        $itm->updated_at = time();
    $login_hash = str_replace(" ", "+", $login_hash);
    if ($user != null) {
        if ($user->login_hash == $login_hash) {
            $controllerRealEstate->deleteRealEstate($realestate_id, 1);
            $json = "{\n                        \"realestate_info\" : {\n                                        \"realestate_id\" : \"{$realestate_id}\",\n                                        \"is_deleted\" : \"1\"\n                                      },\n                        \"status\" : {\n                                        \"status_code\" : \"-1\",\n                                        \"status_text\" : \"Success.\"\n                                    }\n                        }";
        } else {
            $json = "{\n                        \"status\" : {\n                                      \"status_code\" : \"5\",\n                                      \"status_text\" : \"It seems you are out of sync. Please relogin again.\"\n                                    }\n                        }";
        }
    }
} else {
    if (!empty($realestate_id) >= 0 && !empty($login_hash) && !empty($user_id) && !empty($agent_id) > 0) {
        $user = $controllerUser->getUserByUserId($user_id);
        $login_hash = str_replace(" ", "+", $login_hash);
        if ($user != null) {
            if ($user->login_hash == $login_hash) {
                $realestate = $controllerRealEstate->getRealEstateByRealEstateId($realestate_id);
                $itm = new RealEstate();
                $itm->address = $address;
                $itm->agent_id = $agent_id;
                $itm->baths = $baths;
                $itm->beds = $beds;
                $itm->built_in = $built_in;
                $itm->country = $country;
                $itm->created_at = time();
                $itm->desc1 = $desc1;
                $itm->featured = $featured;
                $itm->lat = $lat;
                $itm->lon = $lon;
                $itm->lot_size = $lot_size;
                $itm->price = $price;
                $itm->price_per_sqft = $price_per_sqft;