<?php

global $wpdb;

$id = $_GET['id'];
$role_flag = current_user_can('zygw');

if (isset($_POST['submit']) && !$role_flag) {
    $consultant_id = $_POST['data']['recConsultant'][0];
    if (isset($consultant_id) && $consultant_id != null && isset($id)) {
        $result = TospurDao::update_view_house_consultant($id, $consultant_id);
    } else {
        echo '<script>alert("请选择置业顾问")</script>';
    }
} else if (isset($_POST['handle']) && $role_flag) {
    $wpdb->update('tospur_view_house',
        array(
            'handle' => 1,
            'handle_date' => current_time('Y-m-d H:i:s'),
        ),
        array('id' => $id)
    );
}

$house_sql = 'SELECT v.id,h.name,h.city_id as cityId,u.user_login as phone,v.date,v.time,v.consultant_id as consultant_id,v.handle,v.handle_date,c.imageUrl,c.name as consultant_name FROM tospur_view_house v '
    . 'left join tospur_house h on v.house_id = h.id '
    . 'left join wp_users u on v.user_id = u.id '
    . 'left join tospur_consultant c on v.consultant_id = c.id '
    . 'where v.id = ' . $id;

$context['role_flag'] = $role_flag;
$context['city'] = SearchDao::searchCity();
$result = $wpdb->get_row($house_sql);

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

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

?>