function weixin_robot_create_buttons_request($weixin_robot_custom_menus)
{
    $weixin_robot_ordered_custom_menus = weixin_robot_get_ordered_custom_menus($weixin_robot_custom_menus);
    $request = $buttons_json = $button_json = $sub_buttons_json = $sub_button_json = array();
    foreach ($weixin_robot_ordered_custom_menus as $weixin_robot_custom_menu) {
        if (isset($weixin_robot_custom_menu['parent']) && isset($weixin_robot_custom_menu['sub'])) {
            $button_json['name'] = urlencode($weixin_robot_custom_menu['parent']['name']);
            foreach ($weixin_robot_custom_menu['sub'] as $weixin_robot_custom_menu_sub) {
                $sub_button_json['type'] = $weixin_robot_custom_menu_sub['type'];
                $sub_button_json['name'] = urlencode($weixin_robot_custom_menu_sub['name']);
                if ($sub_button_json['type'] == 'click') {
                    $sub_button_json['key'] = urlencode($weixin_robot_custom_menu_sub['key']);
                } elseif ($sub_button_json['type'] == 'view') {
                    $sub_button_json['url'] = urlencode($weixin_robot_custom_menu_sub['key']);
                }
                $sub_buttons_json[] = $sub_button_json;
                unset($sub_button_json);
            }
            $button_json['sub_button'] = $sub_buttons_json;
            unset($sub_buttons_json);
            $buttons_json[] = $button_json;
        } elseif (isset($weixin_robot_custom_menu['parent'])) {
            $button_json['type'] = $weixin_robot_custom_menu['parent']['type'];
            $button_json['name'] = urlencode($weixin_robot_custom_menu['parent']['name']);
            if ($button_json['type'] == 'click') {
                $button_json['key'] = urlencode($weixin_robot_custom_menu['parent']['key']);
            } elseif ($button_json['type'] == 'view') {
                $button_json['url'] = urlencode($weixin_robot_custom_menu['parent']['key']);
            }
            $buttons_json[] = $button_json;
        }
        unset($button_json);
    }
    $request['button'] = $buttons_json;
    unset($buttons_json);
    return $request;
}
function weixin_robot_custom_menu_stats_page()
{
    ?>
	<h3>自定义菜单点击统计分析</h3>
	<?php 
    global $wpdb, $plugin_page;
    $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_custom_menus = get_option('weixin-robot-custom-menus');
    $click_keys = array();
    if ($weixin_robot_custom_menus) {
        foreach ($weixin_robot_custom_menus as $weixin_robot_custom_menu) {
            if ($weixin_robot_custom_menu['key']) {
                $click_keys[] = $weixin_robot_custom_menu['key'];
            }
        }
        if ($click_keys) {
            $click_keys = "'" . implode("','", $click_keys) . "'";
            $sql = "SELECT EventKey, count(*) as count FROM {$wpdb->weixin_messages} WHERE 1=1 AND {$where} AND MsgType = 'event' AND EventKey in({$click_keys}) GROUP BY EventKey";
            $counts = $wpdb->get_results($sql, OBJECT_K);
            $sql = "SELECT count(*) as total FROM {$wpdb->weixin_messages} WHERE 1=1 AND {$where} AND MsgType = 'event' AND EventKey in({$click_keys})";
            $total = $wpdb->get_var($sql);
        }
    }
    ?>
	
	<?php 
    if ($weixin_robot_custom_menus && $click_keys) {
        ?>

	<!--<p>只有点击类型的菜单才能统计!!</p>-->

	<?php 
        $weixin_robot_ordered_custom_menus = weixin_robot_get_ordered_custom_menus($weixin_robot_custom_menus);
        ?>
	
	<table class="widefat" cellspacing="0">
	<thead>
		<tr>
			<th>按钮</th>
			<th>按钮位置/子按钮位置</th>
			<th>类型</th>
			<th>Key/URL</th>
			<th>点击数</th>
			<th>比率</th>
		</tr>
	</thead>
	<tbody>
	<?php 
        $alternate = '';
        ?>
	<?php 
        foreach ($weixin_robot_ordered_custom_menus as $weixin_robot_custom_menu) {
            $alternate = $alternate ? '' : 'alternate';
            ?>
		<?php 
            if (isset($weixin_robot_custom_menu['parent'])) {
                $weixin_menu = $weixin_robot_custom_menu['parent'];
                ?>
		<tr class="<?php 
                echo $alternate;
                ?>
">
			<td><?php 
                echo $weixin_menu['name'];
                ?>
</td>
			<td><?php 
                echo $weixin_menu['position'];
                ?>
</td>
			<td><?php 
                echo $weixin_menu['type'];
                ?>
</td>
			<td <?php 
                if (empty($counts[$weixin_menu['key']])) {
                    echo 'colspan="3"';
                }
                ?>
><?php 
                echo $weixin_menu['key'];
                ?>
</td>
			<?php 
                $id = $weixin_menu['id'];
                ?>
			<?php 
                if (isset($counts[$weixin_menu['key']])) {
                    $count = $counts[$weixin_menu['key']]->count;
                    ?>
			<td><?php 
                    echo $count;
                    ?>
</td>
			<td><?php 
                    echo round($count / $total * 100, 2) . '%';
                    ?>
			<?php 
                }
                ?>
		</tr>
		<?php 
            }
            ?>
		<?php 
            if (isset($weixin_robot_custom_menu['sub'])) {
                ?>
		<?php 
                foreach ($weixin_robot_custom_menu['sub'] as $weixin_menu) {
                    $alternate = $alternate ? '' : 'alternate';
                    ?>
		<tr colspan="4" class="<?php 
                    echo $alternate;
                    ?>
">
			<td> └── <?php 
                    echo $weixin_menu['name'];
                    ?>
</td>
			<td> └── <?php 
                    echo $weixin_menu['sub_position'];
                    ?>
</td>
			<td><?php 
                    echo $weixin_menu['type'];
                    ?>
</td>
			<td><?php 
                    echo $weixin_menu['key'];
                    ?>
</td>
			<?php 
                    $id = $weixin_menu['id'];
                    ?>
			<?php 
                    if (isset($counts[$weixin_menu['key']])) {
                        $count = $counts[$weixin_menu['key']]->count;
                        ?>
			<td><?php 
                        echo $count;
                        ?>
</td>
			<td><?php 
                        echo round($count / $total * 100, 2) . '%';
                        ?>
			<?php 
                    }
                    ?>
		<tr>
		<?php 
                }
                ?>
		<?php 
            }
            ?>
	<?php 
        }
        ?>
	</tbody>
	</table>
	<?php 
    }
}