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

use common\helpers\MongodbSearchFilter;
use common\libs\dataList\DataListInterface;
use common\libs\dataList\column\DateTime;
use common\models\LogSystem;

class SystemList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '系统日志';
    }

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

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if(!$this->columns){
            $this->columns = [
                'opt_type' => [
                    'header' => '操作类型',
                    'headerOther' => [
                        'width' => '120',
                    ],
                    'value' => function($row) {
                        return (isset($row['opt_type']) && $row['opt_type'] == 'call' )? '调用' : '被调用';
                    },
                    'valueOther' => function($row) {
                        return [
                            'tag'=>['type'=>(isset($row['opt_type']) && $row['opt_type'] == 'call' )? 'success' : 'danger', 'size' => 'small']
                        ];
                    },
                ],
                'opt_target_name' => [
                    'header' => '操作对象',
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'opt_obj_name' => [
                    'header' => '对象编号',
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'request_content' => [
                    'header' => '请求内容',
                    'headerOther' => [
                        'show-overflow-tooltip'=>true,
                        'min-width'=>'250',
                    ],
                ],
                'response_content' => [
                    'header' => '返回内容',
                    'headerOther' => [
                        'show-overflow-tooltip'=>true,
                        'min-width'=>'250',
                    ],
                ],
                'opt_user' => [
                    'header' => '操作人',
                    'headerOther' => [
                        'width' => '130',
                    ],
                ],
                'created_at' => [
                    'header' => '操作时间',
                    'value' => function($row) {
                        return (new DateTime([
                            'value' => $row['created_at'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '180'],
                ],
                'period_num' => '期数',
                'ip' => [
                    'header' => 'IP地址',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'device' => [
                    'header' => '客户端',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'user_agent' => [
                    'header' => '操作系统',
                    'headerOther' => [
                        'width' => '350',
                    ],
                ],
                'request_route' => [
                    'header' => '请求路径',
                    'headerOther' => [
                        'width' => '250',
                    ],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'opt_type' => ['isUserTable'=>false, 'name'=>'操作类型', 'other'=> 'select', 'selectData'=> [['id'=> 'call', 'name'=> '调用'], ['id'=> 'called', 'name'=> '被调用']]],
                'opt_target_name' => ['isUserTable'=>false, 'name'=>'操作对象'],
                'opt_obj_name' => ['isUserTable'=>false, 'name'=>'对象编号'],
                'opt_user' => ['isUserTable'=>false, 'name'=>'操作人'],
                'created_at' => ['isUserTable'=>false, 'name'=>'操作时间', 'other'=> 'date'],
                'period_num' => ['isUserTable'=>false, 'name'=>'期数'],
                'ip' => ['isUserTable'=>false, 'name'=>'IP地址'],
                'device' => ['isUserTable'=>false, 'name'=>'客户端'],
                'user_agent' => ['isUserTable'=>false, 'name'=>'操作系统'],
                'request_route' => ['isUserTable'=>false, 'name'=>'请求路径'],
            ];
        }
        return $this->filterTypes;
    }
}