<?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 . 'Dao/TospurDao.php');
    require_once(PLUGIN_DIR . 'Dao/CustomerTrackingDao.php');
    require_once(PLUGIN_DIR . 'Dao/CustomerDao.php');
    require_once(PLUGIN_DIR . 'Admin/House.php');
    require_once(PLUGIN_DIR . 'Admin/customer.php');
    require_once(PLUGIN_DIR . 'Admin/customerList.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(PLUGIN_DIR . 'Admin/customer.php');
    require_once(PLUGIN_DIR . 'Admin/customerList.php');
    require_once(PLUGIN_DIR . 'Admin/customerTrackingList.php');	require_once(PLUGIN_DIR . 'Admin/feature.php');
	require_once(PLUGIN_DIR . 'Admin/introduction.php');
    require_once(PLUGIN_DIR . 'Admin/Contract.php');
    require_once(PLUGIN_DIR . 'Admin/consultant_score.php');
    require_once(PLUGIN_DIR . 'Admin/view_house.php');
    require_once(PLUGIN_DIR . 'Admin/tospur_sale.php');
    require_once(PLUGIN_DIR . 'Admin/TCSyncView.php');
    require_once(PLUGIN_DIR . 'Admin/Contract_List.php');
    require_once(PLUGIN_DIR . 'Admin/commissionManage.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');
    add_action( 'admin_enqueue_scripts', 'tospur_load_resources');
    //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_load_resources(){
    wp_enqueue_script('jqueryjs');
    wp_enqueue_script('jquery-ui');
    wp_enqueue_script('bootstrapjs');
    wp_enqueue_script('publicjs');

    wp_enqueue_script('validate');
    wp_enqueue_style('jquery-ui_css');
	wp_enqueue_style('publicCss');
    wp_enqueue_style('bootstrapMinCss');

}

function tospur_register_script_style(){
    wp_register_script('jqueryjs',plugins_url('Admin/views', __FILE__)."/js/jquery.min.js");
    wp_register_script('jquery-ui',plugins_url('Admin/views', __FILE__)."/js/jquery-ui.js");
    wp_register_script('validate',plugins_url('Admin/views', __FILE__)."/js/jquery.validate.js");
    wp_register_script('bootstrapjs',plugins_url('Admin/views', __FILE__)."/js/bootstrap.min.js");
    wp_register_script('publicjs',plugins_url('Admin/views', __FILE__)."/js/public.js");

    wp_register_style('jquery-ui_css', plugins_url('Admin/views', __FILE__)."/css/jquery-ui.css");
	wp_register_style('publicCss', plugins_url('Admin/views', __FILE__)."/css/public.css");
    wp_register_style('bootstrapcss', plugins_url('Admin/views', __FILE__)."/css/bootstrap.css");
    wp_register_style('bootstrapMinCss', plugins_url('Admin/views', __FILE__)."/css/bootstrap.min.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_searchRentalPrice', 'SearchDao::ajax_searchRentalPrice');
    add_action('wp_ajax_nopriv_searchRentalPrice', 'SearchDao::ajax_searchRentalPrice');
    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_add_feature', 'feature::ajax_add_feature');
	add_action('wp_ajax_nopriv_add_feature', 'feature::ajax_add_feature');
	add_action('wp_ajax_submit_introduction', 'introduction::ajax_submit_introduction');
	add_action('wp_ajax_nopriv_submit_introduction', 'introduction::ajax_submit_introduction');
    add_action('wp_ajax_searchCustomerByNameOrPhone', 'CustomerDao::searchCustomerByNameOrPhone');
	add_action('wp_ajax_nopriv_searchCustomerByNameOrPhone', 'CustomerDao::searchCustomerByNameOrPhone');
	//标签和特色
	add_action('wp_ajax_searchTagOrFeature', 'SearchDao::ajax_searchTagOrFeature');
	add_action('wp_ajax_nopriv_searchTagOrFeature', 'SearchDao::ajax_searchTagOrFeature');
    //后台处理 置业顾问评分
    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 = TospurDao::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 = TospurDao::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('新房列表','新房列表', 'edit_published_posts', 'newHouseList', 'function_newHouseList', 'dashicons-menu', 6);
	add_submenu_page('newHouseList', '添加新房', '添加新房', 'edit_published_posts', 'newHouse', 'House::init_view');
	add_menu_page('二手房列表','二手房列表', 'edit_published_posts', 'secHandHouseList', 'function_secHandHouseList', 'dashicons-menu', 7);
    add_submenu_page('secHandHouseList', '添加二手房', '添加二手房', 'edit_published_posts', 'secHandHouse', 'SecHandHouse::secHandHouse_html');
    add_menu_page('租房列表','租房列表', 'edit_published_posts', 'rentHouseList', 'function_rentHouseList', 'dashicons-menu', 8);
    add_submenu_page('rentHouseList', '添加租房', '添加租房', 'edit_published_posts', 'rentHouse', 'RentHouse::rentHouse_html');
	add_menu_page("诚信宣言", "诚信宣言", "author", "introduction", "introduction::introduction_html", 'dashicons-menu', 9);
    add_menu_page('合同列表','合同列表', 'edit_published_posts', 'contractList', 'Contract_List::showTableView', 'dashicons-menu', 10);
    add_submenu_page('contractList', '添加合同', '添加合同', 'edit_published_posts', 'contract', 'Contract::init_view');
    add_menu_page('客户列表','客户列表', 'edit_published_posts', 'customerList', 'function_customerList', 'dashicons-menu', 11);
    add_submenu_page('customerList', '新增客户', '新增客户', 'edit_published_posts', 'customer', 'customer::customer_html');
	add_menu_page('签约—房客跟进', '签约—房客跟进', 'edit_published_posts', 'customerTrackingList', 'function_customerTrackingList', 'dashicons-menu', 12);
	add_menu_page('收佣管理','收佣管理', 'edit_published_posts', 'commissionManage', 'commissionManage::commissionManage_html', 'dashicons-menu', 13);
    add_menu_page('添加标签', '添加标签', 'edit_published_posts', 'add_tag', 'feature::add_feature_html', 'dashicons-menu');
    add_menu_page('添加特色', '添加特色', 'edit_published_posts', 'add_feature', 'feature::add_feature_html', 'dashicons-menu');
    add_menu_page("同步数据", "同步数据", "manage_options", "sync", "TCSyncView::display", 'dashicons-menu');
    //移除更新信息
    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)]);
            }
        }
    }
}