<?php

require_once(dirname(__FILE__) . '/dao.php');
$context = array();
$context['theme'] = get_template_directory_uri();
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
if ($user_id == 0) {
    $context['message'] = '请先登录';
} else if (isset($_POST['time'])) {
    $house_id = $_GET['house_id'];
    $consultant_id = $_GET['consultant_id'];
    $date = null;
    switch ($_POST['time']) {
        case 0:
        case 1:
        case 2:
            $date = current_time('Y-m-d');
            break;
        case 3:
        case 4:
        case 5:
            $cureent_time = current_time('Y-m-d');
            $datetime = new DateTime($cureent_time . '+1 day');
            $date = $datetime->format('Y-m-d');
            break;
        case 6:
        case 7:
        case 8:
            $cureent_time = current_time('Y-m-d');
            $datetime = new DateTime($cureent_time . '+2 day');
            $date = $datetime->format('Y-m-d');
            break;
        case 9:
        case 10:
        case 11:
            $date = $_POST['date'];
            break;
    }
    $time = $_POST['time'] % 3;
    $result = TospurDao::insert_view_house($house_id, $user_id, $date, $time, $consultant_id);
    if ($result) {
        $context['message'] = '预约成功';
    }
}

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

?>