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

use Yii;
use backend\models\DashboardApi;
use yii\web\NotFoundHttpException;
use yii\helpers\Url;
use common\helpers\UserAgentHelper;
use common\helpers\ExcelHelper;

/**
 * 首页
 */
class DashboardController extends BaseController
{
    /**
     * @return string
     * @throws NotFoundHttpException
     */
    public function actionIndex()
    {
        $api = new DashboardApi();
        $result = $api->getData();
        if ($result['code'] == 0) {
            throw new NotFoundHttpException($result['message']);
        }
        $data = $result['data'];

        return $this->render('index', [
            'mt4user' => Yii::$app->getUser()->getIdentity(false)->getMt4user(),
            'fvSum' => $data['fvSum'] / 100,
            'mvSum' => $data['mvSum'] / 100,
            'cvSum' => $data['cvSum'] / 100,
            'tvSum' => $data['tvSum'] / 100,
            'endDate' => date('Y-m-d', strtotime($data['endDate'])),
            'directlyUserCount' => $data['directlyUserCount'],
            'depositSumByDay' => json_encode($data['depositSumByDay']),
            'isHaveAdmin' => Yii::$app->user->identity->isHaveAdmin(),
        ]);
    }

    /**
     * 申请进度情况
     */
    public function actionApplyList()
    {
        $get = Yii::$app->request->get();
        $orderInt = isset($get['order'][0]['column']) ? (int) $get['order'][0]['column'] : 1;
        $order = isset($get['columns'][$orderInt]['data']) ? $get['columns'][$orderInt]['data'] : '';
        $orderBy = isset($get['order'][0]['dir']) ? $get['order'][0]['dir'] : 'desc';
        $search = isset($get['search']['value']) ? trim($get['search']['value']) : '';

        $start = isset($get['start']) ? (int) $get['start'] : 0;
        $length = isset($get['length']) ? (int) $get['length'] : 20;
        $draw = isset($get['draw']) ? $get['draw'] : 1;

        $params = [
            'order' => $order,
            'orderBy' => $orderBy,
            'search' => $search,
            'start' => $start,
            'length' => $length,
            'draw' => $draw,
        ];

        if ($params['length'] > 5000) {
            if (Yii::$app->getRequest()->getIsAjax()) {
                Yii::$app->getResponse()->redirect(Url::current(), UserAgentHelper::isIE() ? 200 : 302);
            } else {
                $this->exportXls($params);
            }
            Yii::$app->end();
        }

        $api = new DashboardApi();
        $result = $api->getApplyList($params);
        return json_encode($result['data']);
    }

    /**
     * @param array $params
     */
    protected function exportXls($params)
    {
        ExcelHelper::init();
        $attachmentName = Yii::$app->user->identity->name . "_工单申请进度情况_" . date('Y-m-d') . '.xlsx';
        $header = ['账户', '姓名', '业务', '申请时间', '处理状态', '完成时间'];
        $source = [];
        $api = new DashboardApi();
        $params['length'] = 5000;
        while (true) {
            $result = $api->getApplyList($params);
            if ($result['code'] == 1 && !empty($result['data']['data'])) {
                foreach ((array)$result['data']['data'] as $key => $row) {
                    $arr = [];
                    $arr[] = $row['login'];
                    $arr[] = $row['name'];
                    $arr[] = $row['business'];
                    $arr[] = $row['in_time'];
                    $arr[] = $row['type'];
                    $arr[] = $row['complete_time'];
                    $source[] = $arr;
                }
            } else {
                break;
            }
            $params['start'] += $params['length'];
            if ($params['start'] >= $result['data']['recordsTotal']) {
                break;
            }
        }
        ExcelHelper::output($source, $header, $attachmentName);
    }

}




