<?php

/*
 * 留存2
 */
include_once dirname(__FILE__) . '/../main.php';

$paras = query_paras();

$startTime = $paras['date1'];
$endTime = $paras['date2'];
$type = $paras['type'];

$endDate = strtotime("$endTime");

$array = array();
$labels = array();


$tabName = "tab_rolename";
for ($i = 0; $i <= 999; $i++) {
    $iday = strtotime("$startTime   +$i   day");
    $day = date("Y-m-d", $iday);
    if ($iday > $endDate) {
        break;
    }
    $labels[] = $day;

    $new = daoInst()->select('*')->from($tabName)
            ->where('ts')->ge($day . " 00:00:00")
            ->andWhere('ts')->le($day . " 23:59:59")
            ->count();

    if ($type == 0) {
        $array[] = $new;
    } else {
        $iday2 = strtotime("$day   +$type   day");
        $day2 = date("Y-m-d", $iday2);
        $total = daoInst()->select('*')->from($tabName)
                ->where('ts')->ge($day . " 00:00:00")
                ->andWhere('ts')->le($day . " 23:59:59")
                ->andWhere('lastlogin')->ge($day2 . " 00:00:00")
                ->count();
        $array[] = (int) ($total / $new * 100);
    }
}


$result = new stdClass();
$result->data = $array;
$result->labels = $labels;
echo json_encode($result);
