function weixin_robot_credit_list()
{
    global $plugin_page, $succeed_msg, $plugin_page;
    global $wpdb;
    $current_page = isset($_GET['paged']) ? $_GET['paged'] : 1;
    $number_per_page = 50;
    $start_count = ($current_page - 1) * $number_per_page;
    $limit = 'LIMIT ' . $start_count . ',' . $number_per_page;
    $where = '';
    if (isset($_GET['openid'])) {
        $where = "AND wct.weixin_openid = '{$_GET['openid']}'";
    }
    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$wpdb->weixin_credits} as wct LEFT JOIN {$wpdb->weixin_users} wut ON wct.weixin_openid = wut.openid WHERE wut.subscribe = '1'  {$where} ORDER BY wct.id DESC {$limit}";
    $weixin_robot_credits = $wpdb->get_results($sql);
    $total_count = $wpdb->get_var("SELECT FOUND_ROWS();");
    ?>
	<?php 
    if ($weixin_robot_credits) {
        ?>
	<form action="<?php 
        echo admin_url('admin.php?page=' . $plugin_page);
        ?>
" method="POST">
		<table class="widefat" cellspacing="0">
		<thead>
			<tr>
				<th>微信 OpenID</th>
				<th<?php 
        if (weixin_robot_get_setting('weixin_advanced_api')) {
            echo ' colspan="2"';
        }
        ?>
>用户</th>
				<th>积分</th>
				<th>变动</th>
				<th>积分类型</th>
				<th>时间</th>
				<th>备注</th>
			</tr>
		</thead>

		<tbody>
		<?php 
        $alternate = '';
        ?>
		<?php 
        foreach ($weixin_robot_credits as $weixin_robot_credit) {
            $alternate = $alternate ? '' : 'alternate';
            ?>
			<tr class="<?php 
            echo $alternate;
            ?>
">
				<td><a href="<?php 
            echo admin_url('admin.php?page=' . $plugin_page . '&openid=' . $weixin_robot_credit->weixin_openid);
            ?>
"><?php 
            echo $weixin_robot_credit->weixin_openid;
            ?>
</a></td>
			<?php 
            if (weixin_robot_get_setting('weixin_advanced_api')) {
                ?>
				<?php 
                if ($weixin_robot_credit->subscribe) {
                    ?>
				<td>
				<?php 
                    $weixin_user_avatar = '';
                    if (!empty($weixin_robot_credit->headimgurl)) {
                        $weixin_user_avatar = $weixin_robot_credit->headimgurl;
                        ?>
					<img src="<?php 
                        echo $weixin_user_avatar;
                        ?>
" width="32" />
				<?php 
                    }
                    ?>
				</td>
				<td><?php 
                    echo $weixin_robot_credit->nickname;
                    ?>
</td>
				<?php 
                } else {
                    ?>
				<td colspan="2"><span style="color:red">*取消关注*</td>
				<?php 
                }
                ?>
			<?php 
            } elseif ($weixin_robot_credit->name) {
                ?>
				<td><?php 
                echo $weixin_robot_credit->name;
                ?>
</td>
			<?php 
            } else {
                ?>
				<td></td>
			<?php 
            }
            ?>
	
				<td><?php 
            echo $weixin_robot_credit->credit;
            ?>
</td>
				<td><?php 
            echo $weixin_robot_credit->credit_change;
            ?>
</td>
				<td><?php 
            echo $weixin_robot_credit->type;
            ?>
				<?php 
            if ($weixin_robot_credit->operator_id) {
                $operator_user = get_userdata($weixin_robot_credit->operator_id);
                echo '<br />操作人:' . $operator_user->display_name;
            }
            ?>
</td>
				<td><?php 
            echo $weixin_robot_credit->time;
            ?>
</td>
				<td><?php 
            echo $weixin_robot_credit->note;
            ?>
</td>
			</tr>
		<?php 
        }
        ?>
		</tbody>
		</table>
	</form>
	<?php 
        wpjam_admin_pagenavi($total_count, $number_per_page);
        ?>
	<?php 
    } else {
        ?>
		<p>还没有积分历史记录</p>
	<?php 
    }
}
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 
    }
}