<?php
/**
 * 客服
 * Date: 2019年1月9日
 * Time: 下午4:26:19
 */
namespace app\admin\controller;

use BM\Unit\Pusher;
use BM\Model\Chat;
use BM\Model\ChatList;
use BM\Model\ChatMessage;
use BM\Model\Reply;
use app\common\model\Admin;

class Service extends AdminControl
{
    
    /**
    * @客服聊天界面
    */
    public function index()
    {
        //通讯节点
        $node_info = [
            'address' => !empty(config('GATEWAY_NODE')) ? config('GATEWAY_NODE') : '',
        ];
        
        //第三方平台信息
        $site_info = [
            'code' => 'kf_100001_1234566121',
        ];
        
        //当前聊天列表
        $res       = ChatList::instance()->getChatList(['sid' => $this->admin_info['admin_id']]);
        $chat_list = $res['status'] == 1 ? $res['data'] : [];
        
        //客服常用语
        $where  = [
            'reply_company' => $this->admin_info['admin_pid'],
            'reply_type'    => 3,
            'reply_status'  => 1,
            '_fields_'      => [
                'reply_content'
            ],
        ];
        $replys = Reply::instance()->lists($where);
        
        $data = [
            'node'      => $node_info,
            'site'      => $site_info,
            'chat_list' => $chat_list,
            'replys'    => $replys,
        ];
        
        $this->assign('data', $data);
        
        return $this->fetch();
    }
    
    /**
    * @绑定客服
    */
    public function bind()
    {
        $client_id  = !empty($this->request->param('client_id')) ? $this->request->param('client_id') : '';
        
        if (empty($client_id)) {
            return ['status' => 0, 'msg' => 'error[expect client_id]'];
        }
        
        //绑定
        Pusher::instance()->bindUid($client_id, $this->admin_info['admin_id']);
        
        return [
            'status' => 200,
            'msg'    => 'success'
        ];
    }
    
    /**
    * @聊天
    */
    public function chat()
    {
        $to_id  = !empty($this->request->param('to_id')) ? $this->request->param('to_id') : '';
        $content= !empty($this->request->param('content')) ? $this->request->param('content') : '';
        $type   = !empty($this->request->param('type')) ? intval($this->request->param('type')) : ChatMessage::MESSAGE_TYPE_NORMAL;
        
        //获取聊天
        $chat    = ChatList::instance()->info(['sid' => $this->admin_info['admin_id'], 'vid' => $to_id, '_fields_' => 'log_id']);
        
        if (empty($chat)) {
            return ['status' => 0, 'msg' => 'fail["chat data not exist"]'];
        }
        
        $param   = [
            'log_id'  => $chat['log_id'],
            'uid'     => $this->admin_info['admin_pid'],
            'to_id'   => $to_id,//发送客户端
            'from_id' => $this->admin_info['admin_id'],//来源客户端
            'type'    => $type,//消息类型
            'content' => $content,//内容
        ];
        
        $res = Chat::instance()->sendMessage($param);
        
        if ($res['status'] != 1) {
            return $res;
        }
        
        return [
            'status' => 200,
            'msg'    => 'success'
        ];
    }
    
    /**
    * @消息记录
    */
    public function messageLog()
    {
        $to_id      = !empty($this->request->param('to_id')) ? $this->request->param('to_id') : '';
        $page       = !empty($this->request->param('page')) ? intval($this->request->param('page')) : 1;
        $page_size  = 50;
        
        if (empty($to_id)) {
            return [
                'status' => 0,
                'msg'    => '获取消息记录失败[无用户信息]',
            ];
        }
        
        $param = [
            'sid'       => $this->admin_info['admin_id'],
            'vid'       => $to_id,
            'page'      => $page,
            'page_size' => $page_size,
        ];
        
        $res = ChatMessage::instance()->messageLog($param);
        
        if ($res['status'] != 1) {
            return $res;
        }
        
        return [
            'status' => 200,
            'msg'    => 'success',
            'data'   => $res['data'],
        ];
    }
    
    /**
    * @删除访问列表
    */
    public function delChatList()
    {
        $to_id      = !empty($this->request->param('to_id')) ? $this->request->param('to_id') : '';
        
        if (empty($to_id)) {
            return [
                'status' => 0,
                'msg'    => 'fail[expect to_id]'
            ];
        }
        
        $where = [
            'sid' => $this->admin_info['admin_id'],
            'vid' => $to_id,
        ];
        
        ChatList::instance()->del($where);
        
        //断开连接
        $client_lists = Pusher::instance()->getClientIdByUid($to_id);
        
        $message = [
            'method' => 'delVisitor',
            'data'   => [
                'time' => date('Y-m-d H:i:s'),
            ],
        ];
        foreach ($client_lists as $item) {
            Pusher::instance()->closeClient($item, json_encode($message));
        }
        
        return [
            'status' => 200,
            'msg'    => 'success'
        ];
    }
}