<?php
/*
Plugin Name: table_test
Version: 1.0
Plugin URI: http://www.u-gen.net
Author: U-GEN TECH.
Description: 自定义列表
*/


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

function tospur_init()
{
    require_once('consultant_score.php');
    require_once('view_house.php');
    require_once(PLUGIN_DIR . 'Tools\TCSync.php');
    require_once(PLUGIN_DIR . 'Dao\SearchDao.php');
    require_once(PLUGIN_DIR . 'Admin\House.php');
    add_action('admin_menu', 'reset_menu');
    $type = $_POST["type"];
    if($type==1){
        wp_redirect("?page=newHouse&updated=true");
    }
    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_serachCity');
    add_action('wp_ajax_nopriv_serachCity', 'SearchDao::ajax_serachCity');
    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_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("talbe_test", "table_test", "manage_options", "1", "create_table");
    add_menu_page( 'newHouse_title', '新房信息', 'manage_options', 'newHouse', 'House::init_view', 'dashicons-admin-tools', 6 );
    //移除更新信息
    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 create_table()
{
    //TCSync::user_sync();
    TCSync::organization_sync();
    TCSync::other_sync();
}
