<?php

namespace app\admin\controller\fastim;

use app\common\controller\Backend;

/**
 * 举报记录管理
 *
 * @icon fa fa-circle-o
 */
class Report extends Backend
{

    /**
     * Report模型对象
     * @var \app\admin\model\fastim\Report
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\fastim\Report;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            [$where, $sort, $order, $offset, $limit] = $this->buildparams();

            $list = $this->model->with([
                'user'       => ['fauser', 'admin'],
                'reportuser' => ['fauser', 'admin'],
                'chatgroup'
            ])->where($where)->order($sort, $order)->paginate($limit);

            foreach ($list as $row) {

                $row->getRelation('reportuser')->visible(['nickname']);
                $aNickname     = $row->reportuser->admin->nickname ?? '';
                $faNickname    = $row->reportuser->fauser->nickname ?? '';
                $row->nickname = $row->reportuser->nickname;
                if ($aNickname || $faNickname) {
                    $row->nickname = $row->nickname . '(' . ($aNickname ? $aNickname : $faNickname) . ')';
                }

                $row->getRelation('chatgroup')->visible(['nickname']);
                $row->getRelation('user')->visible(['nickname']);

                if ($row->type == 'group') {
                    $row->user->nickname = $row->chatgroup->nickname;
                } elseif ($row->type == 'user') {
                    $aNickname  = $row->user->admin->nickname ?? '';
                    $faNickname = $row->user->fauser->nickname ?? '';
                    if ($aNickname || $faNickname) {
                        $row->user->nickname = $row->user->nickname . '(' . ($aNickname ? $aNickname : $faNickname) . ')';
                    }
                } else {
                    $row->user->nickname = '-';
                }
            }

            $result = ["total" => $list->total(), "rows" => $list->items()];

            return json($result);
        }
        return $this->view->fetch();
    }

}
