function weixin_robot_messages_page()
{
    ?>
	<div class="wrap">
		
		<h2>最新消息</h2>
		<p>下面是你公众号上最新的消息,你可以直接删除(WordPress 本地删除,公众号后台不受影响)!</p>

		<?php 
    global $wpdb, $plugin_page;
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (!wp_verify_nonce($_POST['weixin_robot_send_user_nonce'], 'weixin_robot')) {
            ob_clean();
            wp_die('非法操作');
        }
        $weixin_openid = stripslashes(trim($_POST['weixin_openid']));
        $reply_id = stripslashes(trim($_POST['reply_id']));
        $reply_type = stripslashes(trim($_POST['reply_type']));
        $content = stripslashes(trim($_POST['content']));
        if ($weixin_openid && $message_id && $content) {
            $data = array('MsgType' => 'manual', 'FromUserName' => $weixin_openid, 'CreateTime' => current_time('timestamp') - get_option('gmt_offset') * 3600, 'Content' => $content);
            $insert_id = $wpdb->insert($wpdb->weixin_messages, $data);
            $wpdb->update($wpdb->weixin_messages, array('Response' => $wpdb->insert_id), array('id' => $reply_id));
            $succeed_msg = weixin_rebot_sent_user($weixin_openid, $content, $reply_type);
        }
    }
    $response_types = weixin_robot_get_response_types();
    $types = weixin_robot_stats_get_types();
    unset($types['subscribe']);
    unset($types['unsubscribe']);
    $types['manual'] = '需要人工回复';
    $type = weixin_robot_stats_get_type();
    if (!$type) {
        $type = 'total';
    }
    $Response = isset($_REQUEST['Response']) ? $_REQUEST['Response'] : '';
    if (isset($_GET['delete']) && isset($_GET['id']) && $_GET['id']) {
        $wpdb->query("DELETE FROM {$wpdb->weixin_messages} WHERE id = {$_GET['id']}");
    }
    $current_page = isset($_GET['paged']) ? $_GET['paged'] : 1;
    $number_per_page = 100;
    $start_count = ($current_page - 1) * $number_per_page;
    $limit = 'LIMIT ' . $start_count . ',' . $number_per_page;
    if ($type == 'total') {
        $where = '';
    } elseif ($type == 'manual') {
        $where = "AND Response in('not-found','too-long')";
    } else {
        $where = "AND MsgType = '{$type}'";
    }
    if (isset($_GET['openid'])) {
        $where = "AND FromUserName = '******'openid']}'";
    }
    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$wpdb->weixin_messages} WHERE 1=1 {$where} AND MsgType != 'manual' AND Event!= 'subscribe' AND Event != 'unsubscribe'  ORDER BY CreateTime DESC " . $limit;
    $weixin_messages = $wpdb->get_results($sql);
    $total_count = $wpdb->get_var("SELECT FOUND_ROWS();");
    ?>

		<?php 
    if (!empty($succeed_msg)) {
        ?>
		<div class="updated">
			<p><?php 
        echo $succeed_msg;
        ?>
</p>
		</div>
		<?php 
    }
    ?>

		<ul class="subsubsub">
		<?php 
    foreach ($types as $key => $value) {
        ?>
			<li class="<?php 
        echo $key;
        ?>
"><a href="<?php 
        echo admin_url('admin.php?page=' . $plugin_page . '&type=' . $key);
        ?>
" <?php 
        if ($type == $key) {
            ?>
 class="current"<?php 
        }
        ?>
><?php 
        echo $value;
        ?>
</a> |</li>
		<?php 
    }
    ?>
		<?php 
    /*	<li class="not-found"><a href="<?php echo admin_url('admin.php?page='.$plugin_page.'&Response=not-found')?>" <?php if($Response == 'need-manual') {?> class="current"<?php } ?>>需要回复</a></li>*/
    ?>
		</ul>
		<table class="widefat" cellspacing="0">
		<thead>
			<tr>
				<?php 
    if (weixin_robot_get_setting('weixin_advanced_api') && strpos($wpdb->weixin_messages, 'weixin')) {
        ?>
				<th colspan="2">用户</th>
				<?php 
    } else {
        ?>
				<th>用户</th>
				<?php 
    }
    ?>
				<th style="min-width:200px;width:40%;">内容</th>
				<th>类型</th>
				<th>回复类型</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		<?php 
    $alternate = '';
    foreach ($weixin_messages as $weixin_message) {
        $MsgType = $weixin_message->MsgType;
        $alternate = $alternate ? '' : 'alternate';
        $weixin_openid = $weixin_message->FromUserName;
        ?>
			<tr id="<?php 
        echo $weixin_message->id;
        ?>
" class="<?php 
        echo $alternate;
        ?>
">
			<?php 
        if (weixin_robot_get_setting('weixin_advanced_api') && strpos($wpdb->weixin_messages, 'weixin')) {
            ?>
				<?php 
            $weixin_user = weixin_robot_get_user($weixin_openid);
            ?>
				<?php 
            if ($weixin_user['subscribe']) {
                ?>
				<td>
				<?php 
                $weixin_user_avatar = '';
                if (!empty($weixin_user['headimgurl'])) {
                    $weixin_user_avatar = $weixin_user['headimgurl'];
                    ?>
					<a href="<?php 
                    echo admin_url('admin.php?page=' . $plugin_page . '&openid=' . $weixin_openid);
                    ?>
"><img src="<?php 
                    echo $weixin_user_avatar;
                    ?>
" width="32" /></a>
				<?php 
                }
                ?>
				</td>
				<td>
					<?php 
                echo $weixin_user['nickname'];
                ?>
(<?php 
                if ($weixin_user['sex'] == 1) {
                    echo '男';
                } elseif ($weixin_user['sex'] == 2) {
                    echo '女';
                } else {
                    echo "未知";
                }
                ?>
)<br />
					<?php 
                echo $weixin_user['country'] . ' ' . $weixin_user['province'] . ' ' . $weixin_user['city'];
                ?>
<br />
				</td>
				<?php 
            } else {
                ?>
				<td colspan="2">
					<span style="color:red;">*已经取消关注</span>
				</td>
				<?php 
            }
            ?>
			<?php 
        } else {
            ?>
				<td><?php 
            echo $weixin_openid;
            ?>
</td>
			<?php 
        }
        ?>
				<td class="content">
				<?php 
        if ($MsgType == 'text') {
            echo $weixin_message->Content;
        } elseif ($MsgType == 'link') {
            echo '<a href="' . $weixin_message->Url . '" target="_blank">' . $weixin_message->Title . '</a>';
        } elseif ($MsgType == 'image') {
            echo '<a href="' . $weixin_message->PicUrl . '" target="_blank" title="' . $weixin_message->MediaId . '"><img src="' . $weixin_message->PicUrl . '" alt="' . $weixin_message->MediaId . '" width="100px;"></a>';
            if (isset($_GET['debug'])) {
                echo '<br />MediaId:' . $weixin_message->MediaId;
            }
        } elseif ($MsgType == 'location') {
            echo '<a href="http://ditu.google.cn/maps?q=' . urlencode($weixin_message->label) . '&amp;ll=' . $weixin_message->Location_X . ',' . $weixin_message->Location_Y . '&amp;source=embed" target="_blank">' . $weixin_message->label . '</a>';
        } elseif ($MsgType == 'event') {
            echo '[' . $weixin_message->Event . '] ' . $weixin_message->EventKey;
        } elseif ($MsgType == 'voice') {
            if ($weixin_message->Recognition) {
                echo '语音识别成:';
                echo $weixin_message->Recognition;
            } else {
                echo '未识别';
            }
            if (isset($_GET['debug'])) {
                echo '<br />MediaId:' . $weixin_message->MediaId;
            }
        } else {
            echo $MsgType;
            echo '该类型的内容无法显示,请直接访问微信公众号后台进行操作!';
        }
        if (is_numeric($weixin_message->Response)) {
            $weixin_reply_message = weixin_robot_get_message($weixin_message->Response);
            echo '<br /><span style="background-color:yellow; padding:2px; ">人工回复:' . $weixin_reply_message->Content . '</span>';
        }
        ?>
				</td>
				<td><?php 
        echo $types[$MsgType];
        ?>
<br /><?php 
        echo date('Y-m-d H:i:s', $weixin_message->CreateTime + get_option('gmt_offset') * 3600);
        ?>
</td>
				<td>
					<?php 
        if (is_numeric($weixin_message->Response)) {
            echo '人工回复';
        } elseif (isset($response_types[$weixin_message->Response])) {
            echo $response_types[$weixin_message->Response];
        }
        ?>
				</td>
				<td class="action">
				<?php 
        if (weixin_robot_get_setting('weixin_advanced_api') && strpos($wpdb->weixin_messages, 'weixin') && current_time('timestamp') - $weixin_message->CreateTime < (48 + get_option('gmt_offset')) * 3600) {
            ?>
					<?php 
            if (is_numeric($weixin_message->Response)) {
                ?>
					<span>已经回复</span>
					<?php 
            } elseif ($weixin_user['subscribe']) {
                ?>
					<span class="reply"><a href="javascript:;" onclick="reply_to_weixin('<?php 
                echo $weixin_openid;
                ?>
', '<?php 
                echo $weixin_message->id;
                ?>
')">回复</a></span>
					<?php 
            }
            ?>
					
				<?php 
        } else {
            ?>
					<span class="delete"><a href="<?php 
            echo admin_url('admin.php?page=weixin-robot-messages&delete&id=' . $weixin_message->id);
            ?>
">删除</a></span>
				<?php 
        }
        ?>
				</td>
			</tr>
			<?php 
    }
    ?>

			<?php 
    if (weixin_robot_get_setting('weixin_advanced_api') && strpos($wpdb->weixin_messages, 'weixin')) {
        ?>
			<tr id="reply_form" style="display:none;" >
				<td colspan="2">&nbsp;</td>
				<td colspan="4">
				<form action="<?php 
        echo admin_url('admin.php?page=' . $plugin_page);
        ?>
" method="POST">
					<p>
						<textarea name="content" id="content" rows="5" class="large-text code"></textarea>
					</p>
					<p>
						<select name="reply_type" id="reply_type" >
							<option value="text">文本回复</option>
							<option value="img">图文回复</option>
						</select>
					</p>
					<input type="hidden" name="weixin_openid" id="weixin_openid" value="" />
					<input type="hidden" name="reply_id" id="reply_id" value="" />
					<?php 
        wp_nonce_field('weixin_robot', 'weixin_robot_send_user_nonce');
        ?>
					<p><input type="submit" name="submit" id="submit" class="button button-primary" value="回复用户" style="float:right; margin-right:20px;"></p>
				</form>
				</td>
			</tr>
			<?php 
    }
    ?>
		</tbody>
		</table>
		<?php 
    wpjam_admin_pagenavi($total_count, $number_per_page);
    ?>
		<?php 
    if (weixin_robot_get_setting('weixin_advanced_api') && strpos($wpdb->weixin_messages, 'weixin')) {
        ?>
		<script type="text/javascript">
			function reply_to_weixin(weixin_openid, id){
				jQuery('input#weixin_openid')[0].value = weixin_openid;
				jQuery('input#reply_id')[0].value = id;
				jQuery('tr#'+id).after(jQuery('#reply_form'));
				jQuery('tr#reply_form').show();
			}

			jQuery(function(){

				jQuery('form').submit(function( event ) {
					var reply_id		= jQuery('input#reply_id')[0].value;
					var reply_type		= jQuery('select#reply_type')[0].value;
					var weixin_openid	= jQuery('input#weixin_openid')[0].value;
					var reply_content	= jQuery('textarea#content')[0].value;

					if(jQuery('textarea#content')[0].value != ''){
						jQuery.ajax({
							type: 'post',
							url: '<?php 
        echo admin_url('admin-ajax.php');
        ?>
',
							data: { 
								action: 'weixin_reply', 
								weixin_openid: weixin_openid,
								reply_id: reply_id, 
								reply_type: reply_type, 
								content: reply_content,
								_ajax_nonce: '<?php 
        echo wp_create_nonce('weixin_robot_ajax_nonce');
        ?>
'
							},
							success: function(html){
								reply_content = jQuery('tr#'+reply_id+' td.content').html()+'<br /><span style="background-color:yellow; padding:2px; ">人工回复:'+reply_content+'</span>';
								jQuery('tr#'+reply_id+' td.content').html(reply_content);
								jQuery('tr#'+reply_id+' td.action').html('已经回复');
								jQuery('textarea#content')[0].value = '';
								jQuery('tr#reply_form').hide();
							}
						});
					}else{
						alert('回复的内容不能为空');
						jQuery('textarea#content').focus();
					}
					
					event.preventDefault();
				});
			});
		</script>		
		<?php 
        wpjam_confim_delete_script();
        ?>
		<?php 
    }
}
function weixin_robot_custom_menu_page()
{
    global $weixin_robot_custom_menus, $id, $succeed_msg;
    $weixin_robot_custom_menus = get_option('weixin-robot-custom-menus');
    if (!$weixin_robot_custom_menus) {
        $weixin_robot_custom_menus = array();
    }
    if (isset($_GET['delete']) && isset($_GET['id']) && $_GET['id']) {
        unset($weixin_robot_custom_menus[$_GET['id']]);
        update_option('weixin-robot-custom-menus', $weixin_robot_custom_menus);
        $succeed_msg = '删除成功';
    }
    if (isset($_GET['sync'])) {
        $succeed_msg = apply_filters('weixin_robot_post_custom_menus', '', $weixin_robot_custom_menus);
    } elseif (isset($_GET['edit']) && isset($_GET['id'])) {
        $id = (int) $_GET['id'];
    }
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (!wp_verify_nonce($_POST['weixin_robot_custom_menu_nonce'], 'weixin_robot')) {
            ob_clean();
            wp_die('非法操作');
        }
        $is_sub = isset($_POST['is_sub']) ? 1 : 0;
        $data = array('name' => stripslashes(trim($_POST['name'])), 'type' => stripslashes(trim($_POST['type'])), 'key' => stripslashes(trim($_POST['key'])), 'position' => $is_sub ? '0' : stripslashes(trim($_POST['position'])), 'parent' => $is_sub ? stripslashes(trim($_POST['parent'])) : '0', 'sub_position' => $is_sub ? stripslashes(trim($_POST['sub_position'])) : '0');
        if (empty($id)) {
            if ($weixin_robot_custom_menus) {
                end($weixin_robot_custom_menus);
                $id = key($weixin_robot_custom_menus) + 1;
            } else {
                $id = 1;
            }
            $weixin_robot_custom_menus[$id] = $data;
            update_option('weixin-robot-custom-menus', $weixin_robot_custom_menus);
            $succeed_msg = '添加成功';
            $id = 0;
        } else {
            $weixin_robot_custom_menus[$id] = $data;
            update_option('weixin-robot-custom-menus', $weixin_robot_custom_menus);
            $succeed_msg = '修改成功';
        }
    }
    ?>
	<div class="wrap">
		<h2>自定义菜单 <a href="<?php 
    echo admin_url('admin.php?page=weixin-robot-stats2&tab=custom-menu-stats');
    ?>
" class="add-new-h2">点击统计</a></h2>
		<?php 
    if (!empty($succeed_msg)) {
        ?>
		<div class="updated">
			<p><?php 
        echo $succeed_msg;
        ?>
</p>
		</div>
		<?php 
    }
    ?>
		<?php 
    if (!empty($err_msg)) {
        ?>
		<div class="error" style="color:red;">
			<p>错误:<?php 
        echo $err_msg;
        ?>
</p>
		</div>
		<?php 
    }
    ?>
		<?php 
    weixin_robot_custom_menu_list();
    ?>
		<?php 
    weixin_robot_custom_menu_add();
    ?>
		<?php 
    wpjam_confim_delete_script();
    ?>
	</div>
<?php 
}
function weixin_robot_qrcode_page()
{
    global $wpdb, $id, $succeed_msg;
    $wpdb->show_errors();
    if (isset($_GET['delete']) && isset($_GET['id']) && $_GET['id']) {
        $wpdb->query("DELETE FROM {$wpdb->weixin_qrcodes} WHERE id = {$_GET['id']}");
    }
    if (isset($_GET['edit']) && isset($_GET['id'])) {
        $id = (int) $_GET['id'];
    }
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (!wp_verify_nonce($_POST['weixin_robot_qrcode_nonce'], 'weixin_robot')) {
            ob_clean();
            wp_die('非法操作');
        }
        $scene = stripslashes(trim($_POST['scene']));
        $name = stripslashes(trim($_POST['name']));
        $type = stripslashes(trim($_POST['type']));
        $expire = stripslashes(trim($_POST['expire']));
        if (weixin_robot_get_setting('weixin_app_id') && weixin_robot_get_setting('weixin_app_secret')) {
            if (weixin_robot_create_qrcode($scene, $name, $type, $expire)) {
                if (empty($id)) {
                    $succeed_msg = '添加成功';
                } else {
                    $succeed_msg = '修改成功';
                }
            }
        }
    }
    ?>
	<div class="wrap">

		<h2>带参数的二维码 <a href="<?php 
    echo admin_url('admin.php?page=weixin-robot-stats2&tab=qrcode-stats');
    ?>
" class="add-new-h2">使用统计</a></h2>

		<?php 
    if (!empty($succeed_msg)) {
        ?>
		<div class="updated">
			<p><?php 
        echo $succeed_msg;
        ?>
</p>
		</div>
		<?php 
    }
    ?>
		<?php 
    if (!empty($err_msg)) {
        ?>
		<div class="error" style="color:red;">
			<p>错误:<?php 
        echo $err_msg;
        ?>
</p>
		</div>
		<?php 
    }
    ?>
		
	    <?php 
    weixin_robot_qrcode_list();
    ?>
		<?php 
    weixin_robot_qrcode_add();
    ?>

		<?php 
    wpjam_confim_delete_script();
    ?>

	</div>
<?php 
}