<?php


namespace app\admin\controller;

use app\BaseController;
use app\admin\model\Admin;
use app\admin\model\Sign;
use app\admin\model\KefuWork;
use Exception;


class Kefu extends BaseController
{

    public function statistics()
    {
        // 统计
        $data['visit_count'] = 0; //当前客服页访问量
        $data['online_count'] = 0; //当前客服在线人数
        $data['handling_count'] = 0; //当前客服接线中的总数
        $data['today_handling_count'] = 0; //今日客服接线总数

        return $this->success($data);
    }

    /**
     * @api {get} /today 今日在线客服列表
     */
    public function today()
    {
        try {
            $params = $this->request->param();
            $page = $params['page'] ?? 1;
            $limit = $params['limit'] ?? 15;
            $query = Admin::alias('admin')->join('kefu_work', 'kefu_work.admin_id = admin.id', 'left');
            if (isset($params['is_online'])) {
                $query->where('is_online', $params['is_online']);
            }

            $count = $query->count();
            $list = $query->field(['admin.*', 'kefu_work.chat_num','kefu_work.transfer_num','kefu_work.intervention_num','kefu_work.completed_num'])
                        ->order('admin.id', 'asc')            
                        ->limit($limit)
                        ->page($page)
                        ->select();
            foreach ($list as &$value) {
                $value['chat_num'] = intval($value['chat_num']);
                $value['transfer_num'] = intval($value['transfer_num']);
                $value['intervention_num'] = intval($value['intervention_num']);
                $value['completed_num'] = intval($value['completed_num']);
                $value['inprocess'] = 0;//进行中
                $value['unprocessed'] = 0;//待处理
            }
            
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success(['count' => $count, 'list' => $list]);
    }

    /**
     * @api {get} /list 客服数据展板列表
     */
    public function list()
    {
        try {
            $params = $this->request->param();
            $page = $params['page'] ?? 1;
            $limit = $params['limit'] ?? 15;

            $query = KefuWork::alias('kefu_work')->join('admin', 'kefu_work.admin_id = admin.id', 'left');
            if (isset($params['is_online'])) {
                $query->where('is_online', $params['is_online']);
            }
            if (!empty($params['start_time'])) {
                $query->where('kefu_work.created_at', '>=', $params['start_time'].'00:00:00');
            }
            if (!empty($params['end_time'])) {
                $query->where('kefu_work.created_at', '<=', $params['end_time'].'23:59:59');
            }

            $count = $query->count();
            $list = $query->field(['admin.username','admin.nickname','admin.is_online', 'kefu_work.*'])
                        ->order('kefu_work.id', 'desc')           
                        ->limit($limit)
                        ->page($page)
                        ->select();
            foreach ($list as &$value) {
                // $value['chat_num'] = intval($value['chat_num']);
                // $value['transfer_num'] = intval($value['transfer_num']);
                // $value['intervention_num'] = intval($value['intervention_num']);
                // $value['completed_num'] = intval($value['completed_num']);
                // $value['inprocess'] = 0;//进行中
                // $value['unprocessed'] = 0;//待处理
            }
            
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success(['count' => $count, 'list' => $list]);
    }

    //客服签到
    public function sign()
    {
        try {
            $admin_id = $this->admin_id;
            KefuWork::addNum($admin_id, 'sign_num');//客服签到次数更新
            //签到记录
            Sign::create([          
                'admin_id' => $admin_id,
            ]);
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

}