function weixin_robot_qrcode_stats_page()
{
    ?>
	<h3>二维码扫描统计分析</h3>
	<?php 
    global $wpdb, $plugin_page;
    $qrcode_types = weixin_robot_get_qrcode_types();
    $start_date = weixin_robot_stats_get_start_date();
    $end_date = weixin_robot_stats_get_end_date();
    $end_time = $end_date . ' 23:59:59';
    $where = 'CreateTime > ' . strtotime($start_date) . ' AND CreateTime < ' . strtotime($end_time);
    weixin_robot_stats_header();
    $weixin_robot_qrcodes = $wpdb->get_results("SELECT * FROM {$wpdb->weixin_qrcodes};");
    if ($weixin_robot_qrcodes) {
        $scenes = array();
        $tickets = array();
        foreach ($weixin_robot_qrcodes as $weixin_robot_qrcode) {
            $scenes[] = $weixin_robot_qrcode->scene;
            $qrscenes[] = 'qrscene_' . $weixin_robot_qrcode->scene;
        }
        if ($scenes) {
            $scenes = implode(',', $scenes);
            $sql = "SELECT EventKey, count(*) as count FROM {$wpdb->weixin_messages} WHERE 1=1 AND {$where} AND MsgType = 'event' AND Event = 'SCAN' AND EventKey in({$scenes}) GROUP BY EventKey";
            $scene_counts = $wpdb->get_results($sql, OBJECT_K);
        }
        if ($qrscenes) {
            $qrscenes = "'" . implode("','", $qrscenes) . "'";
            $sql = "SELECT EventKey, count(*) as count FROM {$wpdb->weixin_messages} WHERE 1=1 AND {$where} AND MsgType = 'event' AND Event = 'subscribe' AND EventKey in({$qrscenes}) GROUP BY EventKey";
            $qrscene_counts = $wpdb->get_results($sql, OBJECT_K);
        }
    }
    ?>
	
	<?php 
    if ($weixin_robot_qrcodes && $scene_counts) {
        ?>
	<form action="<?php 
        echo admin_url('admin.php?page=' . $plugin_page);
        ?>
" method="POST">
		
		<table class="widefat" cellspacing="0">
		<thead>
			<tr>
				<?php 
        /*<th style="width:40px">ID</th>*/
        ?>
				<th>场景 ID</th>
				<th>名称</th>
				<th>类型</th>
				<th>过期时间</th>
				<th>二维码</th>
				<th>关注</th>
				<th>扫描</th>
			</tr>
		</thead>
		<tbody>
		<?php 
        $alternate = '';
        ?>
		<?php 
        foreach ($weixin_robot_qrcodes as $weixin_robot_qrcode) {
            ?>
			<?php 
            $alternate = $alternate ? '' : 'alternate';
            $scene = $weixin_robot_qrcode->scene;
            $type = $weixin_robot_qrcode->type;
            $name = $weixin_robot_qrcode->name;
            $ticket = $weixin_robot_qrcode->ticket;
            $expire = $weixin_robot_qrcode->expire;
            ?>
			<tr class="<?php 
            echo $alternate;
            ?>
">
				<?php 
            /*<td><?php echo $weixin_robot_qrcode->id; ?></td>*/
            ?>
				<td><?php 
            echo $weixin_robot_qrcode->scene;
            ?>
</td>
				<td><?php 
            echo $name;
            ?>
</td>
				<td><?php 
            echo $qrcode_types[$type];
            ?>
</td>
				<td><?php 
            echo $type == 'QR_SCENE' ? $expire - time() > 0 ? $expire - time() : '已过期' : '';
            ?>
</td>
				<td><img src="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=<?php 
            echo urlencode($ticket);
            ?>
" width="100"></td>
				<td><?php 
            echo isset($qrscene_counts['qrscene_' . $weixin_robot_qrcode->scene]) ? $qrscene_counts['qrscene_' . $weixin_robot_qrcode->scene]->count : '';
            ?>
</td>
				<td><?php 
            echo isset($scene_counts[$weixin_robot_qrcode->scene]) ? $scene_counts[$weixin_robot_qrcode->scene]->count : '';
            ?>
</td>
			</tr>
		<?php 
        }
        ?>
		</tbody>
		</table>
	</form>
	<?php 
    }
}
function weixin_robot_qrcode_add()
{
    global $wpdb, $id, $plugin_page;
    if (isset($id)) {
        $weixin_robot_qrcode = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->weixin_qrcodes} WHERE id=%d LIMIT 1", $id));
        $type = $weixin_robot_qrcode->type;
        $scene = $weixin_robot_qrcode->scene;
        $name = $weixin_robot_qrcode->name;
        $expire = $weixin_robot_qrcode->expire - time();
    } else {
        $id = '';
    }
    ?>
	<h3 id="edit"><?php 
    echo $id ? '修改' : '新增';
    ?>
带参数的二维码 <?php 
    if ($id) {
        ?>
 <a href="<?php 
        echo admin_url('admin.php?page=' . $plugin_page . '&add');
        ?>
" class="add-new-h2">新增另外一条自定义回复</a> <?php 
    }
    ?>
</h3>

	<?php 
    $form_fields = array('scene' => array('title' => '场景 ID', 'type' => 'text', 'value' => $id ? $scene : '', 'description' => '临时二维码时为32位非0整型,永久二维码时最大值为100000(目前参数只支持1--100000)'), 'name' => array('title' => '名称', 'type' => 'text', 'value' => $id ? $name : '', 'description' => '二维码名称无实际用途,仅用于更加容易区分。'), 'type' => array('title' => '类型', 'type' => 'select', 'value' => $id ? $type : '', 'options' => weixin_robot_get_qrcode_types()), 'expire' => array('title' => '过期时间', 'type' => 'text', 'value' => $id ? $expire : '', 'description' => '二维码有效时间,以秒为单位。最大不超过1800'));
    ?>
	<form method="post" action="<?php 
    echo admin_url('admin.php?page=' . $plugin_page . '&edit&id=' . $id);
    ?>
" enctype="multipart/form-data" id="form">
		<?php 
    wpjam_admin_display_fields($form_fields);
    ?>
		<?php 
    wp_nonce_field('weixin_robot', 'weixin_robot_qrcode_nonce');
    ?>
		<input type="hidden" name="action" value="edit" />
		<p class="submit"><input class="button-primary" type="submit" value="  <?php 
    echo $id ? '修改' : '新增';
    ?>
  " /></p>
	</form>
	<script type="text/javascript">
	jQuery(function(){
		jQuery('#tr_expire').hide();
	<?php 
    if ($id) {
        ?>
		jQuery('#scene').attr('readonly','readonly'); 
		<?php 
        if ($type == 'QR_SCENE') {
            ?>
			jQuery('#tr_expire').show();
		<?php 
        }
        ?>
	<?php 
    }
    ?>
		jQuery("select#type").change(function(){
			var selected = jQuery("select#type").val();

			if(selected == 'QR_LIMIT_SCENE'){
				jQuery('#tr_expire').hide();
			}else if(selected == 'QR_SCENE'){
				jQuery('#tr_expire').show();
			}
		});
	});
	</script> 
<?php 
}