<?php

require_once(dirname(__FILE__) . '/dao.php');
require_once(dirname(__FILE__) . '/../../plugins/tospur/Dao/SearchDao.php');

add_filter('template_include', 'page_template');
function page_template($template)
{
    $theme = get_stylesheet_directory();
    $page = null;
    if (isset($_GET['page'])) {
        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 'consultant_list':
                $page = $theme . '/consultant_list.php';
                break;
            case 'score':
                $page = $theme . '/score.php';
                break;
            case 'comment':
                $page = $theme . '/comment.php';
                break;
            case 'myHouse':
                $page = $theme . '/myHouse.php';
                break;
        }
        return $page;
    } else {
        return $template;
    }
}

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);
}

add_action('wp_ajax_get_consultant', 'get_consultant');
function get_consultant()
{
    if (isset($_POST['city_id']) && isset($_POST['index'])) {
        $result = dao::search_consultant_by_city($_POST['city_id'], $_POST['index']);
        $array = array();
        if ($result) {
            $array['code'] = 2000;
            $array['result'] = $result;
        } else {
            $array['code'] = 2001;
        }
        wp_send_json($array);
    }
}

add_action('wp_ajax_get_my_house', 'get_my_house');
function get_my_house()
{
    $houseType = $_POST['houseType'];
    $userType = $_POST['userType'];
    $userId = $_POST['userId'];
    $index = $_POST['index'];
    if (isset($houseType) && isset($userType) && isset($userId) && isset($index)) {
        $house_result = SearchDao::searchHouse(
            array(
                'houseType' => $houseType,
                'userType' => $userType,
                'userId' => $userId,
                'index' => $index
            )
        );
        $array = get_house_image_and_tags($house_result);
        wp_send_json($array);
    }
}

add_action('wp_ajax_search_house', 'search_house');
function search_house()
{
    $house_result = SearchDao::searchHouse(
        array(
            'cityId' => $_POST['cityId'],
            'districtId' => $_POST['districtId'],
            'plateId' => $_POST['plateId'],
            'buildPropertyId' => $_POST['buildPropertyId'],
            'roomId' => $_POST['roomId'],
            'acreage' => $_POST['acreage'],
            'totalPrice' => $_POST['totalPrice'],
            'searchText' => $_POST['searchText'],
            'index' => $_POST['index']
        )
    );
    $array = get_house_image_and_tags($house_result);
    wp_send_json($array);
}

function get_house_image_and_tags($house_result)
{
    $array = array();
    if ($house_result) {
        $array['code'] = 2000;
        $house_ids = array();
        foreach ($house_result as $key => $value) {
            $house_id = $value->id;
            $house_ids[] = $house_id;
        }
        $string = '(' . implode(',', array_map('intval', $house_ids)) . ')';
        $tag_result = dao::search_house_tag($string);
        //标签
        if ($tag_result) {
            foreach ($tag_result as $key => $value) {
                $array['tags'][$value->house_id][] = $value->name;
            }
        }
        //图片
        $image_result = dao::search_house_image($string);
        if ($image_result) {
            foreach ($image_result as $key => $value) {
                $array['images'][$value->house_id] = home_url() . $value->path;
            }
        }
        $array['result'] = $house_result;
    } else {
        $array['code'] = 2001;
    }
    return $array;
}

?>