<?php
namespace backend\modules\ib\controllers;

use Yii;
use backend\models\MemberApi;

/**
 * 历史报表
 */
class HistoryController extends BaseController
{
    public function actionIndex()
    {
        $request = Yii::$app->request;
        $ibId = (int) $request->get('ibId');
        $name = trim($request->get('name', ''));
        $sTime = $request->get('sTime', $this->defaultStartTime()); // 开始时间，默认从上个星期天开始
        $eTime = $request->get('eTime', $this->defaultEndTime()); // 结束时间

        $api = new MemberApi();
        $ibs = $api->getIbs()['data'];

        /** @var \backend\models\MemberIdentity Yii::$app->user->identity */
        return $this->render('index', [
            'type' => 'history',
            'ibId' => $ibId,
            'name' => $name,
            'sTime' => $sTime,
            'eTime' => $eTime,
            'ibs' => $ibs,
            'isHaveAdmin' => Yii::$app->user->identity->isHaveAdmin(),
        ]);
    }

    /**
     * 默认开始时间，默认从上个星期天开始
     * @return string
     */
    protected function defaultStartTime()
    {
        return date('Y-m-d', strtotime('last sunday'));
    }

    /**
     * 默认结束时间，默认今天
     * @return string
     */
    protected function defaultEndTime()
    {
        return date('Y-m-d');
    }

}