<?php

require_once('const.php');
require_once(dirname(__FILE__) . '/../../plugins/tospur/Dao/TospurDao.php');
require_once(dirname(__FILE__) . '/../../plugins/tospur/Dao/SearchDao.php');
require_once(dirname(__FILE__) . '/../../plugins/tospur/Tools/Image.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 'comment':
                $page = $theme . '/comment.php';
                break;
            case 'myHouse':
                $page = $theme . '/myHouse.php';
                break;
            case 'detail':
                $page = $theme . '/detail.php';
                break;
            case 'map':
                $page = $theme . '/map.php';
                break;
            case 'loan_calculator':
                $page = $theme . '/loan_calculator.php';
                break;
            case 'tax_calculator':
                $page = $theme . '/tax_calculator.php';
                break;
            case 'ability_calculator':
                $page = $theme . '/ability_calculator.php';
                break;
            case 'manager':
                $page = $theme . '/manager.php';
                break;
            case 'sale':
                $page = $theme . '/sale.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 = TospurDao::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 = TospurDao::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 = TospurDao::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'])) {
        $type = TospurDao::search_score_type($_POST['user_id'], $_POST['consultant_id']);
        $result = null;
        if ($type) {
            $result = TospurDao::insert_consultant_score($_POST['consultant_id'], $_POST['user_id'], $_POST['score']);
        }
        $array = array();
        if ($type && $result) {
            $array['code'] = 2000;
        } else if(!$type){
            $array['code'] = 2001;
        } else if(!$result){
            $array['code'] = 2002;
        }
        wp_send_json($array);
    }
}

//置业顾问信息
function get_consultant_info($consultant_id)
{
    return TospurDao::search_consultant_info($consultant_id);
}

add_action('wp_ajax_get_consultant', 'get_consultant');
function get_consultant()
{
    if (isset($_POST['city_id']) && isset($_POST['index'])) {
        $result = TospurDao::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_search_house', 'SearchDao::ajax_searchHouse');
add_action('wp_ajax_nopriv_search_house', 'SearchDao::ajax_searchHouse');

add_action('wp_ajax_submit_comment', 'submit_comment');
function submit_comment()
{
    $user_id = $_POST['user_id'];
    $content = $_POST['content'];
    if (isset($user_id) && isset($content)) {
        $data = array(
            'comment_post_ID' => 1,
            'comment_content' => $content,
            'comment_parent' => 0,
            'user_id' => $user_id,
            'comment_date' => current_time('mysql'),
            'comment_approved' => 1,
        );
        wp_insert_comment($data);
    }
}

add_action('wp_ajax_add_collect', 'add_collect');
function add_collect($house_id)
{
    $insert_a_house_user_array = array(
        'user_id' => get_current_user_id(),
        'house_id' => $_POST["hid"],
        'user_type' => 0
    );
    global $wpdb;
    $result = $wpdb->insert('a_house_user', $insert_a_house_user_array);

    $array = array();
    if ($result) {
        $array['code'] = 2000;
    } else {
        $array['code'] = 2001;
    }
    wp_send_json($array);
}

add_filter('login_redirect', 'new_login_redirect', 10, 3);
function new_login_redirect($redirect_to, $request, $user)
{
    if ($user->ID > 0) {
        $role = $user->roles[0];
        if ($role == 'administrator' || $role == 'editor') {
            return admin_url('admin.php?page=newHouseList');
        } else {
            return site_url();
        }
    } else {
        return $redirect_to;
    }
}

add_action('wp_logout', 'auto_redirect_after_logout');
function auto_redirect_after_logout()
{
    header('Location: ' . home_url('/?page=manager'));
    exit;
}

add_filter('login_url', 'my_login_page', 10, 2);
function my_login_page($login_url, $redirect)
{
    return home_url('/?page=manager&redirect_to=' . $redirect);
}

function user_redirect($user)
{
    $url = null;
    if ($user->ID > 0) {
        $role = $user->roles[0];
        if ($role == 'administrator' || $role == 'editor' || $role == 'author') {
            $url = admin_url('admin.php?page=newHouseList');
        } else {
            $url = site_url();
        }
        header('Location:' . $url);
    }
}

add_action('wp_ajax_submit_sale', 'submit_sale');
function submit_sale()
{
    $array = array('code' => 2001);
    if ($_POST) {
        $result = TospurDao::insert_tospur_sale($_POST);
        if ($result) {
            $array['code'] = 2000;
        }
    }
    wp_send_json($array);
}

?>