<?php
if (!defined('ABSPATH')) {die;} // Cannot access directly.
date_default_timezone_set('Asia/Shanghai');

global $wpdb,$order_table_name,$aff_table_name,$down_table_name;
function query_day($star,$end){
	global $wpdb,$order_table_name;
	$sql = $wpdb->get_var("SELECT SUM(order_price) FROM $order_table_name WHERE create_time > {$star} AND create_time < {$end}");
	$sql = ($sql) ? sprintf("%.2f",$sql) : 0 ;
	$sql_ok = $wpdb->get_var("SELECT SUM(order_price) FROM $order_table_name WHERE create_time > {$star} AND create_time < {$end} AND status=1");
	$sql_ok = ($sql_ok) ? sprintf("%.2f",$sql_ok) : 0 ;
	return array('sum' => $sql,'sum_ok' => $sql_ok,'sum_no' => round(($sql-$sql_ok),2));
}

// 时间安排

$arr_itme = [
	[
		'name' => '今日',
		'time' => Ceomax_Time::today(),
	],
	[
		'name' => '本周',
		'time' => Ceomax_Time::week(),
	],
	[
		'name' => '本月',
		'time' => Ceomax_Time::month(),
	],
	[
		'name' => '今年',
		'time' => Ceomax_Time::year(),
	],

];

?>

<link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri();?>/static/assets/css/admin-index.css">

<!-- 主页面商城管理统计/总览 -->
<div class="wrap">
	<h2 style=" margin-bottom: 20px; "><i class="fa fa-bar-chart"></i> 商城数据统计</h2>
	<hr class="wp-header-end">
	<div class="layui-div">
		<!-- 导航四 -->
		<div class="layui-row layui-col-space15">

            <div class="layui-col-sm6 layui-col-md3">
                <div class="layui-card">
                    <?php
                    $tongji         = new Shoptongji();
                    $ice_time_where = $tongji->dayDatetime();
                    $ice_money      = $tongji->icemoneyDay();
                    ?>
                    <div class="layui-card-header"><span class="layui-badge-dot layui-bg-orange"></span> 今日充值统计
                        <span class="layui-badge layui-bg-blue layuiadmin-badge"><?php $_count_ok=$tongji->icemoneyDayNum();
                            echo $_count_ok?$_count_ok:0;
                            ?> 条</span>
                    </div>
                    <div class="layui-card-body layuiadmin-card-list">
                        <p class="layuiadmin-big-font"><?php
                            echo $ice_money?$ice_money:0;
                            ?></p>
                        <p>订单总数 <span class="layui-badge-rim"><?php echo $_count_ok?$_count_ok:0;?>条</span>
                            <span class="layuiadmin-span-color"><span class="layui-badge-dot"></span> 订单总额 <span class="layui-badge-rim">￥<?php echo $ice_money?$ice_money:0;;?></span></span>
                            </p>
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6 layui-col-md3">


                <div class="layui-card">
                    <?php
                    $ice_time_where=$tongji->monthDatetime();
                    $ice_money=$tongji->icemoneyMonth();
                    ?>
                    <div class="layui-card-header"><span class="layui-badge-dot layui-bg-orange"></span> 本月充值统计
                        <span class="layui-badge layui-bg-blue layuiadmin-badge"><?php
                            $_count_ok=$tongji->icemoneyMonthNum();
                            echo $_count_ok?$_count_ok:0;
                            ?> 条</span>
                    </div>
                    <div class="layui-card-body layuiadmin-card-list">
                        <p class="layuiadmin-big-font"><?php
                            echo $ice_money?$ice_money:0;
                            ?></p>
                        <p>订单总数 <span class="layui-badge-rim"><?php echo $_count_ok?$_count_ok:0;?>条</span>
                            <span class="layuiadmin-span-color"><span class="layui-badge-dot"></span> 订单总额 <span class="layui-badge-rim">￥<?php echo $ice_money?$ice_money:0;;?></span></span>
                            </p>
                    </div>
                </div>

            </div>
            <div class="layui-col-sm6 layui-col-md3">
                <div class="layui-card">
                    <div class="layui-card-header"><span class="layui-badge-dot layui-bg-orange"></span> 全部充值统计
                        <span class="layui-badge layui-bg-blue layuiadmin-badge"><?php $_count_ok=$tongji->icemoneyAllNum();
                        echo $_count_ok?$_count_ok:0;
                        ?> 条</span>
                    </div>
                    <div class="layui-card-body layuiadmin-card-list">
                        <p class="layuiadmin-big-font"><?php
                            $ice_money=$tongji->icemoneyAll();
                            echo $ice_money?$ice_money:0;
                            ?></p>
                        <p>订单总数 <span class="layui-badge-rim"><?php echo $_count_ok?$_count_ok:0;?>条</span>

                            <span class="layuiadmin-span-color"><span class="layui-badge-dot"></span> 订单总额 <span class="layui-badge-rim">￥<?php echo $ice_money?$ice_money:0;;?></span></span>
                            </p>
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6 layui-col-md3">
                <div class="layui-card">
                    <?php
                    global $wppay_table_name;
                    $ice_money = $tongji->wppaySum();
                    ?>
                    <div class="layui-card-header"><span class="layui-badge-dot layui-bg-orange"></span> 免登录购买统计
                        <span class="layui-badge layui-bg-blue layuiadmin-badge"><?php $_count_ok = $tongji->wppayNum();
                            echo $_count_ok?$_count_ok:0;
                            ?> 条</span>
                    </div>
                    <div class="layui-card-body layuiadmin-card-list">
                        <p class="layuiadmin-big-font"><?php
                            echo $ice_money?$ice_money:0;
                            ?></p>
                        <p>订单总数 <span class="layui-badge-rim"><?php echo $_count_ok?$_count_ok:0;?> 条</span>
                            <span class="layuiadmin-span-color"><span class="layui-badge-dot"></span> 订单总额 <span class="layui-badge-rim">￥<?php echo $ice_money?$ice_money:0;;?></span></span>
                            </p>
                    </div>
                </div>
            </div>

<!--            2-->

            <div class="layui-col-sm6 layui-col-md3">
                <div class="layui-card">
                    <div class="layui-card-header">
                        <span class="layui-badge-dot layui-bg-orange"></span> 用户总数
                        <span class="layui-badge layui-bg-blue layuiadmin-badge"><?php echo $tongji->usersCount(); ?> 位</span>
                    </div>
                    <div class="layui-card-body layuiadmin-card-list">
                        <p class="layuiadmin-big-font"><?php echo $tongji->usersCount(); ?></p>
                        <p>注册用户数量统计
                        <span class="layuiadmin-span-color"><span class="layui-badge-dot"></span> 今日注册 <span class="layui-badge-rim"><?php echo $tongji->user_registered_day(); ?> 位</span></span>
                        </p>
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6 layui-col-md3">
                <div class="layui-card">
                    <div class="layui-card-header">
                        <span class="layui-badge-dot layui-bg-orange"></span> VIP会员总数
                        <span class="layui-badge layui-bg-blue layuiadmin-badge"><?php echo $tongji->vipUsersCount(); ?> 位</span>
                    </div>
                    <div class="layui-card-body layuiadmin-card-list">
                        <p class="layuiadmin-big-font"><?php echo $tongji->vipUsersCount(); ?></p>
                        <p>VIP会员数量统计
                        <span class="layuiadmin-span-color"><span class="layui-badge-dot"></span> 今日开通 <span class="layui-badge-rim"><?php echo $tongji->vipUsersCountToday(); ?> 位</span></span>
                        </p>
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6 layui-col-md3">
                <div class="layui-card">
                    <div class="layui-card-header">
                        <span class="layui-badge-dot layui-bg-orange"></span> 文章总数
                        <span class="layui-badge layui-bg-blue layuiadmin-badge"><?php echo $tongji->postsCount(); ?> 篇</span>
                    </div>
                    <div class="layui-card-body layuiadmin-card-list">
                        <p class="layuiadmin-big-font"><?php echo $tongji->postsCount();; ?></p>
                        <p>文章总数量统计
                        <span class="layuiadmin-span-color"><span class="layui-badge-dot"></span> 今日发布 <span class="layui-badge-rim"><?php echo $tongji->postsCountToday(); ?> 篇</span></span>
                        </p>
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6 layui-col-md3">
                <div class="layui-card">
                    <div class="layui-card-header" style="overflow: hidden">
                        <span class="layui-badge-dot layui-bg-orange"></span> 资源总数
                        <span class="layui-badge layui-bg-blue layuiadmin-badge"><?php echo $tongji->postsDownCount(); ?> 个</span>
                    </div>
                    <div class="layui-card-body layuiadmin-card-list">
                        <p class="layuiadmin-big-font"><?php echo $tongji->postsDownCount(); ?></p>
                        <p>商城资源数量统计<span class="layuiadmin-span-color"><span class="layui-badge-dot"></span> 今日发布 <span class="layui-badge-rim"><?php echo $tongji->postsDownCountToday(); ?> 个</span></span>
                        </p>
                    </div>
                </div>
            </div>

			<?php foreach ($arr_itme as $key => $item) {
			    break;//tmp
				// 获取今日总订单
				$_time = $item['time'];
				$_count = $wpdb->get_var("SELECT COUNT(id) FROM $order_table_name WHERE create_time > {$_time[0]} AND create_time < {$_time[1]}");
				$_count = ($_count) ? $_count : 0 ;
				$_count_ok = $wpdb->get_var("SELECT COUNT(id) FROM $order_table_name WHERE create_time > {$_time[0]} AND create_time < {$_time[1]} AND status=1");
				$_count_ok = ($_count_ok) ? $_count_ok : 0 ;

				$_sum = $wpdb->get_var("SELECT SUM(order_price) FROM $order_table_name WHERE create_time > {$_time[0]} AND create_time < {$_time[1]}");
				$_sum = ($_sum) ? $_sum : 0 ;
				$_sum_ok = $wpdb->get_var("SELECT SUM(order_price) FROM $order_table_name WHERE create_time > {$_time[0]} AND create_time < {$_time[1]} AND status=1");
				$_sum_ok = ($_sum_ok) ? $_sum_ok : 0 ;
				?>
				<div class="layui-col-sm6 layui-col-md3">
					<div class="layui-card">
			          <div class="layui-card-header"><span class="layui-badge-dot layui-bg-orange"></span> <?php echo $item['name'];?>已付款</span>
			          <span class="layui-badge-rim">付款率 <?php echo $retVal = ($_count_ok==0 || $_count==0) ? 0 : sprintf("%.2f",$_count_ok/$_count) ;?>%</span>
			          	<span class="layui-badge layui-bg-blue layuiadmin-badge"><?php echo $_count_ok;?> 条</span>
			          </div>
			          <div class="layui-card-body layuiadmin-card-list">
			            <p class="layuiadmin-big-font">￥<?php echo $_sum_ok;?></p>
			            <p>订单总数 <span class="layui-badge-rim"><?php echo $_count;?>条</span>

			             	<span class="layuiadmin-span-color"><span class="layui-badge-dot"></span> 订单总额 <span class="layui-badge-rim">￥<?php echo $_sum;?></span></span> </p>
			          </div>
			        </div>
				</div>
			<?php } ?>
			<!-- 图表 -->
			<div class="layui-col-sm12" style="display: none">
		        <div class="layui-card">
		          <div class="layui-card-header">
		            最近七天商城统计概览
		          </div>
		          <div class="layui-card-body">
		            <div class="layui-row">
		              <div class="layui-col-sm8">
		                  <div id="conversionsChart" style="width: auto;height:500px;"></div>
		              </div>
		              <div class="layui-col-sm4">
		                <div class="layuiadmin-card-list">
		                  <p class="layuiadmin-normal-font">月访问数</p>
		                  <span>同上期增长</span>
		                  <div class="layui-progress layui-progress-big" lay-showpercent="yes">
		                    <div class="layui-progress-bar" lay-percent="30%" style="width: 30%;"><span class="layui-progress-text">30%</span></div>
		                  </div>
		                </div>
		                <div class="layuiadmin-card-list">
		                  <p class="layuiadmin-normal-font">月下载数</p>
		                  <span>同上期增长</span>
		                  <div class="layui-progress layui-progress-big" lay-showpercent="yes">
		                    <div class="layui-progress-bar" lay-percent="20%" style="width: 20%;"><span class="layui-progress-text">20%</span></div>
		                  </div>
		                </div>
		                <div class="layuiadmin-card-list">
		                  <p class="layuiadmin-normal-font">月收入</p>
		                  <span>同上期增长</span>
		                  <div class="layui-progress layui-progress-big" lay-showpercent="yes">
		                    <div class="layui-progress-bar" lay-percent="25%" style="width: 25%;"><span class="layui-progress-text">25%</span></div>
		                  </div>
		                </div>
		                <div class="layuiadmin-card-list">
		                  <p class="layuiadmin-normal-font">月收入</p>
		                  <span>同上期增长</span>
		                  <div class="layui-progress layui-progress-big" lay-showpercent="yes">
		                    <div class="layui-progress-bar" lay-percent="45%" style="width: 45%;"><span class="layui-progress-text">45%</span></div>
		                  </div>
		                </div>
		              </div>
		            </div>
		          </div>
		        </div>
		      </div>

		</div>
	</div>

	<br class="clear">
</div>

<script src="<?php echo get_template_directory_uri(); ?>/static/assets/js/echarts.min.js"></script>

<script>

<?php

$day=[];
// 获取七天时间
for ($i=0; $i < 7; $i++) {
	$_day = 7-$i;
	$time=mktime(0, 0, 0, date('m'), date('d') - $_day, date('Y'));

	$day[$i] = date('Y-m-d',$time);
}

echo "var time_arr =". json_encode($day).";";

$__day=[];
// 获取七天时间
for ($i=0; $i < 7; $i++) {
	$_d = 7-$i;
	$time=mktime(0, 0, 0, date('m'), date('d') - $_d, date('Y'));
	$__day[$i] = $time;
}
$__sum_data = [];
$__sum_ok_data = [];
$__sum_no_data = [];
foreach ($__day as $k => $time) {
	$end=$time+24*60*60;
	$query = query_day($time,$end);
	$__sum_data[$k] = $query['sum'];
	$__sum_ok_data[$k] = $query['sum_ok'];
	$__sum_no_data[$k] = $query['sum_no'];
}

echo "var __sum_data =". json_encode($__sum_data).";";
echo "var __sum_ok_data =". json_encode($__sum_ok_data).";";
echo "var __sum_no_data =". json_encode($__sum_no_data).";";

?>

console.log(__sum_data)

var myChart = echarts.init(document.getElementById('conversionsChart'));
var option = {
    tooltip: {
        trigger: 'axis',
        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
        }
    },
    legend: {
        data: ['商城总订单','已付款订单', '未付款订单', ]
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    xAxis: [
        {
            type: 'category',
            data: time_arr
        }
    ],
    yAxis: [
        {
            type: 'value'
        }
    ],
    series: [

        {
            name: '商城总订单',
            type: 'bar',
            data: __sum_data
        },
        {
            name: '已付款订单',
            type: 'bar',
            data: __sum_ok_data
        },
        {
            name: '未付款订单',
            type: 'bar',
            data: __sum_no_data
        },

    ]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

jQuery(document).ready(function($){


});
</script>

