<?php

require_once(dirname(__FILE__) . '/dao.php');

add_filter('template_include', 'page_template');
function page_template($template)
{
    $theme = get_stylesheet_directory();
    $page = null;
    switch ($_GET['page']) {
        case 'login':
            $page = $theme . '/login.php';
            break;
        case 'register':
            $page = $theme . '/register.php';
            break;
        case 'forget':
            $page = $theme . '/forget.php';
            break;
        case 'my':
            $page = $theme . '/my.php';
            break;
        case 'view':
            $page = $theme . '/view.php';
            break;
        case 'list':
            $page = $theme . '/list.php';
            break;
        case 'consultant_info':
            $page = $theme . '/consultant_info.php';
            break;
        case 'score':
            $page = $theme . '/score.php';
            break;
        default:
            $page = $template;
            break;
    }
    return $page;
}

function generate_code()
{
    $characters = '0123456789';
    $charactersLength = strlen($characters);
    $random = '';
    for ($i = 0; $i < 4; $i++) {
        $random .= $characters[rand(0, $charactersLength - 1)];
    }
    return $random;
}

//发送验证码
add_action('wp_ajax_send_code', 'send_code');
add_action('wp_ajax_nopriv_send_code', 'send_code');
function send_code()
{
    $phone = $_POST['phone'];
    $validate = (bool)$_POST['validate'];
    $array = array();
    if (isset($phone)) {
        $user = get_user_by('login', $phone);
        if (($user && $validate == 0) || (!$user && $validate == 1)) {
            $array['code'] = 2002;
        } else {
            $code = generate_code();
            $insert_result = dao::insert_tospur_verify($phone, strtolower($code));
            $response = wp_remote_post('http://218.1.67.130:8988/api/NanTongWechat/MobileSendMessage' .
                '?mobilePhone=' . $phone . '&VerificationCode=' . $code
            );
            if ($response['response']['code'] == 200) {
                $array['code'] = 2000;
            } else {
                $array['code'] = 2001;
            }
        }
        wp_send_json($array);
    }
}

//预约看房
add_action('wp_ajax_view_house', 'view_house');
function view_house()
{
    $index = $_POST['index'];
    if (isset($index)) {
        $date = null;
        if ($_POST['day']) {
            $cureent_time = current_time('Y-m-d');
            $datetime = new DateTime($cureent_time . '+' . $_POST['day'] . ' day');
            $date = $datetime->format('Y-m-d');
        } else {
            $date = $_POST['date'];
        }
        $time = (9 + $index) . ':00-' . (10 + $index) . ':00';
        $result = dao::insert_view_house($_POST['house_id'], $_POST['user_id'], $date, $index, $time, $_POST['consultant_id']);
        $array = array();
        if ($result) {
            $array['code'] = 2000;
        } else {
            $array['code'] = 2001;
        }
        wp_send_json($array);
    }
}

//置业顾问接受 预约看房
add_action('wp_ajax_handle_view_house', 'handle_view_house');
function handle_view_house()
{
    $id = $_POST['id'];
    if (isset($id)) {
        $result = dao::update_view_house($id);
        $array = array();
        if ($result) {
            $array['code'] = 2000;
        } else {
            $array['code'] = 2001;
        }
        wp_send_json($array);
    }
}

//添加 置业顾问 评分
add_action('wp_ajax_add_score', 'add_consultant_score');
function add_consultant_score()
{
    if (isset($_POST['score'])) {
        $result = dao::insert_consultant_score($_POST['consultant_id'], $_POST['user_id'], $_POST['score']);
        $array = array();
        if ($result) {
            $array['code'] = 2000;
        } else {
            $array['code'] = 2001;
        }
        wp_send_json($array);
    }
}

//置业顾问信息 评分
function get_consultant_score($consultant_id)
{
    return dao::search_consultant_score($consultant_id);
}

?>