<?php

namespace app\admin\controller\fastim;

use app\common\controller\Backend;
use addons\fastim\library\Common;

/**
 * 群聊管理
 *
 * @icon fa fa-circle-o
 */
class Groupchat extends Backend
{

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

    protected $pageCount = 15;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\fastim\Groupchat;
        $this->view->assign("addModeList", $this->model->getAddModeList());
        $this->view->assign("inviteJoinGroupList", $this->model->getInviteJoinGroupList());
        $this->view->assign("speakList", $this->model->getSpeakList());
        $this->view->assign("historyMessageList", $this->model->getHistoryMessageList());
    }

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

    /**
     * 聊天记录
     */
    public function record($ids, $page = 1)
    {
        $groupChatInfo = Common::getImGroupChat($ids);
        $imSession     = Common::imSession('group', $groupChatInfo['leader'], $groupChatInfo['id']);
        $sessionInfo   = Common::sessionInfo($imSession['id'], $groupChatInfo['leader']);

        if (!$sessionInfo) {
            $this->error(__('No Results were found'));
        }

        if ($page == 1) {
            $min = 0;
        } else {
            $min = ($page - 1) * $this->pageCount;
        }

        if ($sessionInfo['type'] == 'group') {

            $message = Common::loadGroupChatRecord($sessionInfo, [
                'min'       => $min,
                'pageCount' => $this->pageCount
            ], $sessionInfo['user_one']);

            $sessionInfo['windowType'] = 'message';
        } else {
            $this->error('加载失败，请重试！');
        }

        $nextpage = (count($message) < $this->pageCount) ? false : true;
        $message  = Common::groupByTime($message);

        if ($this->request->isAjax()) {
            $this->success('ok', '', [
                'pageData'    => [
                    'nextpage' => $nextpage,
                    'page'     => $page
                ],
                'message'     => $message,
                'sessionInfo' => $sessionInfo
            ]);
        }

        $this->assignconfig('pageData', [
            'nextpage' => $nextpage,
            'page'     => $page,
            '__CDN__'  => $this->request->domain()
        ]);
        $this->assignconfig('message', $message);
        $this->assignconfig('sessionInfo', $sessionInfo);
        return $this->view->fetch();
    }

    /**
     * 查看
     */
    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(['fastimuser' => ['fauser', 'admin']])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {

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

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

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

}
