<?php

$house_id = $_GET['id'];

$page = $_GET['page'];
if ($page == 'tospur_sale_secondhand') {
    $house_type = 1;
} else if ($page == 'tospur_sale_rent') {
    $house_type = 2;
}

$role_flag = current_user_can('fdfw_allot');
global $wpdb;
if (isset($_POST['submit']) && $role_flag) {
    $consultant_id = $_POST['data']['recConsultant'][0];
    if (isset($consultant_id) && $consultant_id != null) {
        $wpdb->update('tospur_sale',
            array('consultant_id' => $consultant_id),
            array('id' => $house_id)
        );
    } else {
        echo '<script>alert("请选择置业顾问")</script>';
    }
} else if (isset($_POST['handle']) && !$role_flag) {
    $wpdb->update('tospur_sale',
        array(
            'handle' => 1,
            'handle_date' => current_time('Y-m-d H:i:s'),
        ),
        array('id' => $house_id)
    );
}
$detail_sql = "SELECT ts.*,dc.cityName,tc.id as consultant_id,tc.imageUrl,tc.name as consultant_name FROM tospur_sale ts" .
    " left join (SELECT cityId,cityName FROM dic_city group by cityId) dc on ts.cityId = dc.cityId" .
    " left join tospur_consultant tc on tc.id = ts.consultant_id" .
    " where ts.house_type = " . $house_type . " and ts.id = " . $house_id;

$result = $wpdb->get_row($detail_sql);

if ($result->consultant_id && $role_flag) {
    $context['consultant'][0] = array(
        'id' => $result->consultant_id,
        'imageUrl' => $result->imageUrl,
        'name' => $result->consultant_name
    );
}

$context['detail_result'] = $result;
$context['url'] = home_url();
$context['id'] = $house_id;
$context['cityId'] = $_GET['cityId'];
$context['house_type'] = $house_type;
$context['city'] = SearchDao::searchCity();
$context['role_flag'] = $role_flag;
if (!$role_flag) {
    $context['handle'] = (integer)$result->handle;
}

Timber::render('sale_detail.html', $context);

?>