<?php
namespace backendApi\modules\v1\models\lists\log;

use common\helpers\MongodbSearchFilter;
use common\libs\dataList\DataListInterface;
use common\models\LogAdminLogin;
use common\libs\dataList\column\DateTime;
use common\models\LogUserLogin;
use yii\helpers\ArrayHelper;

class UserLoginList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '会员登录日志';
    }

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle()
    {
        if ( $this->condition ) {
            $condition = $this->condition;
        }else {
            $filter = MongodbSearchFilter::filterCondition([
                'opt_type' => 'opt_type',
                'user_name' => 'user_name',
                'success_times' => 'success_times',
                'created_at' => 'created_at',
                'period_num' => 'period_num',
                'ip' => 'ip',
                'device' => 'device',
                'user_agent' => 'user_agent',
                'request_route' => 'request_route',
                'version' => 'version',
            ]);
            $condition = $filter['condition'];
        }

        $data = LogUserLogin::lists($condition, [
            'pageSize' => $this->pageSize
        ]);
        unset($filter);
        $this->listData = $data;
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if(!$this->columns){
            $this->columns = [
                'opt_type' => [
                    'header' => 'Operation Type',//操作类型
                    'headerOther' => [
                        'width' => '120',
                    ],
                    'value' => function($row) {
                        return (isset($row['opt_type']) && $row['opt_type'] == 1 )? 'login succeeded' : 'login failed';//'登录成功' : '登录失败';
                    },
                    'valueOther' => function($row) {
                        return [
                            'tag'=>['type'=>(isset($row['opt_type']) && $row['opt_type'] == 1 )? 'success' : 'danger', 'size' => 'small']
                        ];
                    },
                ],
                'opt_obj' => [
                    'header' => 'Operation Object', // 操作对象
                    'headerOther' => [
                        'width' => '130',
                    ],
                    'value' => function($row) {
                        return 'member'; // 会员
                    },
                ],
                'user_name' => [
                    'header' => 'Object Number', // 对象编号
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'return_result' => [
                    'header' => 'Return Content', // 返回内容
                    'headerOther' => [
                        'width' => '250',
                    ],
                ],
                'success_times' => [
                    'header' => 'Login Success Times', // 登录成功次数
                    'headerOther' => [
                        'width' => '160',
                    ],
                ],
                'fail_times' => [
                    'header' => 'Number Of Login Failures', // 登录失败次数
                    'headerOther' => [
                        'width' => '190',
                    ],
                ],
                'created_at' => [
                    'header' => 'Operation Time', // 操作时间
                    'value' => function($row) {
                        return (new DateTime([
                            'value' => $row['created_at'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '180'],
                ],
                'period_num' => [
                    'header' => 'Number Of Periods', // 期数
                    'headerOther' => ['width' => '180'],
                    ],
                'ip' => [
                    'header' => 'IP Address', // IP地址
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'device' => [
                    'header' => 'Client', // 客户端
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'user_agent' => [
                    'header' => 'Operating System', // 操作系统
                    'headerOther' => [
                        'width' => '950',
                    ],
                ],
                'request_route' => [
                    'header' => 'Request Path', // 请求路径
                    'headerOther' => [
                        'width' => '250',
                    ],
                ],
                'version' => [
                    'header' => 'Version', // 版本
                    'headerOther' => [
                        'width' => '100',
                    ],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
//                'opt_type' => ['isUserTable'=>false, 'name'=>'操作类型', 'other'=> 'select', 'selectData'=> [['id'=> 1, 'name'=> '登录成功'], ['id'=> 0, 'name'=> '登录失败']]],
                'opt_type' => ['isUserTable'=>false, 'name'=>'Operation Type', 'other'=> 'select', 'selectData'=> [['id'=> 1, 'name'=> 'login succeeded'], ['id'=> 0, 'name'=> 'login failed']]],
                'user_name' => ['isUserTable'=>false, 'name'=>'Object Number'],//对象编号
                'success_times' => ['isUserTable'=>false, 'name'=>'Login Success Times'],//登录成功次数
                'fail_times' => ['isUserTable'=>false, 'name'=>'Number Of Login Failures'],//登录失败次数
                'created_at' => ['isUserTable'=>false, 'name'=>'Operation Time', 'other'=> 'date'],//操作时间
                'period_num' => ['isUserTable'=>false, 'name'=>'Number Of Periods'],//期数
                'ip' => ['isUserTable'=>false, 'name'=>'IP Address'],//IP地址
                'device' => ['isUserTable'=>false, 'name'=>'Client'],//客户端
                'user_agent' => ['isUserTable'=>false, 'name'=>'Operating System'],//操作系统
                'request_route' => ['isUserTable'=>false, 'name'=>'Request Path'],//请求路径
                'version' => ['isUserTable'=>false, 'name'=>'Version'],//版本
            ];
        }
        return $this->filterTypes;
    }
}