<?php
/*
Plugin Name: 同策插件
Version: 1.0
Author: 同策.
Description: 同策
*/


define('PLUGIN_DIR', dirname(__FILE__) . '/');
add_action('init', 'tospur_init');

function tospur_init()
{
    require_once(PLUGIN_DIR . 'Config.php');
   require_once(PLUGIN_DIR . 'Tools/TCSync.php');
    require_once(PLUGIN_DIR . 'Tools/Image.php');
    require_once(PLUGIN_DIR . 'Dao/SearchDao.php');
    require_once(PLUGIN_DIR . 'Admin/House.php');
    require_once(PLUGIN_DIR . 'Admin/newHouseList.php');
    require_once(PLUGIN_DIR . 'Admin/secHandHouse.php');
    require_once(PLUGIN_DIR . 'Admin/secHandHouseList.php');
    require_once(PLUGIN_DIR . 'Admin/rentHouse.php');
    require_once(PLUGIN_DIR . 'Admin/rentHouseList.php');
    require_once('consultant_score.php');
    require_once('view_house.php');
    add_action('admin_menu', 'reset_menu');
    tospur_register_script_style();
    tospur_ajax_set();
    tospur_theme_format();
}

function tospur_theme_format(){
    //移除
    add_filter('admin_footer_text', tospur_remove_admin_footer_text, 1000);
    add_filter('update_footer', tospur_remove_admin_footer_upgrade, 1000);
    add_action('login_enqueue_scripts', 'tospur_login_logo');
    //admin bar
    add_action('wp_before_admin_bar_render', 'tospur_remove_admin_bar');
    function tospur_remove_admin_footer_text($footer_text =''){
        return '';
    }
    function tospur_remove_admin_footer_upgrade($footer_text =''){
        return '';
    }

    function tospur_remove_admin_bar(){
        global $wp_admin_bar;
        $wp_admin_bar->remove_menu('wp-logo');
        $wp_admin_bar->remove_menu('updates');
        $wp_admin_bar->remove_menu('new-content');
        $wp_admin_bar->remove_menu('edit-profile');
        $wp_admin_bar->remove_menu('view-site');
        $wp_admin_bar->remove_menu('user-info');
    }

    function tospur_login_logo()
    { ?>
        <style type="text/css">
            .login h1 a {
                background-image: url(<?php echo get_stylesheet_directory_uri(); ?>/img/fnl.jpg);
                padding-bottom: 30px;
            }
        </style>
    <?php }
}

function tospur_register_script_style(){
    wp_register_script('jquery-ui',plugins_url('Admin/views', __FILE__)."/js/jquery-ui.js");
    wp_register_script('bootstrapjs',plugins_url('Admin/views', __FILE__)."/js/bootstrap.min.js");
    wp_register_style('jquery-ui_css', plugins_url('Admin/views', __FILE__)."/css/jquery-ui.css");
    wp_register_style('bootstrapcss', plugins_url('Admin/views', __FILE__)."/css/bootstrap.css");
}

function tospur_ajax_set()
{
    add_action('wp_ajax_serachCity', 'SearchDao::ajax_searchCity');
    add_action('wp_ajax_nopriv_serachCity', 'SearchDao::ajax_searchCity');
    add_action('wp_ajax_searchArea', 'SearchDao::ajax_searchArea');
    add_action('wp_ajax_nopriv_searchArea', 'SearchDao::ajax_searchArea');
    add_action('wp_ajax_searchBuildProperty', 'SearchDao::ajax_searchBuildProperty');
    add_action('wp_ajax_nopriv_searchBuildProperty', 'SearchDao::ajax_searchBuildProperty');
    add_action('wp_ajax_searchRoom', 'SearchDao::ajax_searchRoom');
    add_action('wp_ajax_nopriv_searchRoom', 'SearchDao::ajax_searchRoom');
    add_action('wp_ajax_searchUnitPriceRange', 'SearchDao::ajax_searchUnitPriceRange');
    add_action('wp_ajax_nopriv_searchUnitPriceRange', 'SearchDao::ajax_searchUnitPriceRange');
    add_action('wp_ajax_searchTotalPrice', 'SearchDao::ajax_searchTotalPrice');
    add_action('wp_ajax_nopriv_searchTotalPrice', 'SearchDao::ajax_searchTotalPrice');
    add_action('wp_ajax_searchOrganization', 'SearchDao::ajax_searchOrganization');
    add_action('wp_ajax_nopriv_searchOrganization', 'SearchDao::ajax_searchOrganization');
    add_action( 'wp_ajax_searchHouse', 'SearchDao::ajax_searchHouse' );
    add_action( 'wp_ajax_nopriv_searchHouse', 'SearchDao::ajax_searchHouse');
    add_action( 'wp_ajax_searchConsultant', 'SearchDao::ajax_searchConsultant' );
    add_action( 'wp_ajax_nopriv_searchConsultant', 'SearchDao::ajax_searchConsultant');
    //后台处理 置业顾问评分
    add_action('wp_ajax_valid_consultant_score', 'valid_consultant_score');
    //后台处理 置业顾问
    add_action('wp_ajax_update_consultant', 'update_consultant');
}

function valid_consultant_score()
{
    $score_id = $_POST['id'];
    $valid = $_POST['valid'];
    if (isset($score_id) && isset($valid)) {
        $result = dao::update_consultant_score($score_id, $valid);
        $array = array();
        if ($result) {
            $array['code'] = 2000;
        } else {
            $array['code'] = 2001;
        }
        wp_send_json($array);
    }
}
function update_consultant()
{
    $house_id = $_POST['id'];
    $consultant_id = $_POST['consultant_id'];
    if (isset($consultant_id) && isset($house_id)) {
        $result = dao::update_view_house_consultant($house_id, $consultant_id);
        $array = array();
        if ($result) {
            $array['code'] = 2000;
        } else {
            $array['code'] = 2001;
        }
        wp_send_json($array);
    }
}

function reset_menu()
{
    add_menu_page("sync", "同步数据", "manage_options", "1", "do_sync");
    add_menu_page('nesHouseList','新房列表', 'moderate_comments', 'newHouseList', 'newHouseList', 'dashicons-menu', 6);
    add_submenu_page('newHouseList', '添加新房', '添加新房', 'moderate_comments', 'newHouse', 'House::init_view');
    add_menu_page('secHandHouseList','二手房列表', 'moderate_comments', 'secHandHouseList', 'secHandHouseList', 'dashicons-menu', 7);
    add_submenu_page('secHandHouseList', '添加二手房', '添加二手房', 'moderate_comments', 'secHandHouse', 'SecHandHouse::secHandHouse_html');
    add_menu_page('rentHouseList','租房列表', 'moderate_comments', 'rentHouseList', 'rentHouseList', 'dashicons-menu', 8);
    add_submenu_page('rentHouseList', '添加租房', '添加租房', 'moderate_comments', 'rentHouse', 'RentHouse::rentHouse_html');
    //移除更新信息
    remove_action( 'admin_notices', 'update_nag', 3 );
    global $menu;
    $restricted = array(
        __('Dashboard'),
        __('Posts'),
        __('latestnews'),
        __('Media'),
        __('Links'),
        __('Pages'),
        __('Appearance'),
        __('Tools'),
        __('Users'),
        __('Settings'),
        __('Comments'),
        __('Plugins'),
    );
    end($menu);
    while (prev($menu)) {
        $value = explode(' ', $menu[key($menu)][0]);
        if (strpos($value[0], '<') === FALSE) {
            if (in_array($value[0] != NULL ? $value[0] : "", $restricted)) {
                unset($menu[key($menu)]);
            }
        } else {
            $value2 = explode('<', $value[0]);
            if (in_array($value2[0] != NULL ? $value2[0] : "", $restricted)) {
                unset($menu[key($menu)]);
            }
        }
    }
}

function do_sync()
{
    TCSync::user_sync();
    TCSync::organization_sync();
    TCSync::other_sync();
}
