<?php
namespace app\admin\controller;

/**
 * 智能问答类
 */
class Robot extends Base
{


    /**
     * 智能问答列表
     *
     * @access public
     * @return array JsonString
     */
    public function index()
    {
        if (request()->isAjax()) {
            $param  = input('param.');
            $limit  = $param['pageSize'];
            $offset = (($param['pageNumber'] - 1) * $limit);
            $where  = [];
            if (empty($param['searchText']) === false) {
                // $where['robot_name'] = $param['searchText'];
                $where['robot_name'] = ['like', '%' . $param['searchText'] . '%'];
            }

            $field  = ['robot_id', 'robot_name', 'robot_content', 'robot_host', 'robot_status', 'user_name', 'robot_updateTime'];
            $join   = [
                'admins b'      => 'a.admin_id = b.id',
            ];
            $result = model('Robot')->selectJoin($field, $join, $where, $offset, $limit);
            foreach ($result as $key => $vo) {
                // 优化显示状态.
                if(1 === $vo['robot_status']) {
                    $result[$key]['robot_status'] = '<span style="color: #2fbe1b">启用</span>';
                } else {
                    $result[$key]['robot_status'] = '<span style="color: red">禁用</span>';
                }

                // 优化显示热点问题.
                if (1 === $vo['robot_host']) {
                    $result[$key]['robot_host'] = '<span style="color: #2fbe1b">是</span>';
                } else {
                    $result[$key]['robot_host'] = '<span style="color: red">否</span>';
                }

                // 生成操作按钮.
                $result[$key]['operate'] = $this->makeBtn($vo['robot_id']);
            }
// var_dump($where);die;
            // 总数据.
            $return['total'] = model('Robot')->count($where);
            $return['rows'] = $result;

            return json($return);

        }

        return $this->fetch();
    }//end index()


    // 添加智能问答
    public function addWord()
    {
        if(request()->isPost()){

            $param = input('post.');
            $param['robot_content'] = trim($param['robot_content']);
            $robotWhere = [
                'robot_name' => $param['robot_name'],
            ];

            $has = db('robot')->field('robot_id')->where($robotWhere)->find();
            if(!empty($has)){
                return json(['code' => -1, 'data' => '', 'msg' => '该智能问答已经存在']);
            }

            $param['robot_addTime']    = date('Y-m-d H:i:s');
            $param['robot_updateTime'] = date('Y-m-d H:i:s');
            $param['groups_id']        = 1;
            $param['robotgroups_id']   = 1;
            $param['admin_id']         = session('user_id');
            try{
                db('robot')->insert($param);
            }catch(\Exception $e){
                return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
            }

            return json(['code' => 1, 'data' => '', 'msg' => '添加智能问答成功']);
        }
        $groups = db('groups')->where('status', 1)->select();
        $robotgroups = db('robotgroups')->where('robotgroups_status', 1)->select();
        $this->assign([
            'status' => config('kf_status'),
            'host' => config('host'),
            'groups' => $groups,
            'robotgroups' => $robotgroups,
        ]);

        return $this->fetch('addword');
    }

    // 编辑智能问答
    public function editWord()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $param['robot_content'] = trim($param['robot_content']);
            $param['robot_updateTime'] = date('Y-m-d H:i:s');
            $robotWhere = [
                'robot_name' => $param['robot_name'],
            ];

            // 检测用户修改的智能问答是否重复
            $has = db('robot')->where($robotWhere)->count();
            if($has>1){
                return json(['code' => -1, 'data' => '', 'msg' => '该智能问答已经存在']);
            }
            $param['groups_id'] = 1;
            $param['robotgroups_id'] = 1;
            $param['admin_id']   = session('user_id');

            try{
                db('robot')->where('robot_id', $param['robot_id'])->update($param);
            }catch(\Exception $e){
                return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
            }

            return json(['code' => 1, 'data' => '', 'msg' => '编辑智能问答成功']);
        }

        $id = input('param.robot_id/d');
        $info = db('robot')->where('robot_id', $id)->find();
        $groups = db('groups')->where('status', 1)->select();
        $robotgroups = db('robotgroups')->where('robotgroups_status', 1)->select();

        $this->assign([
            'info' => $info,
            'host' => config('host'),
            'groups' => $groups,
            'robotgroups' => $robotgroups,
            'status' => config('kf_status')
        ]);
        return $this->fetch('editword');
    }

    // 删除智能问答
    public function delWord()
    {
        if(request()->isAjax()){
            $id = input('param.id/d');

            try{
                db('robot')->where('robot_id', $id)->delete();
            }catch(\Exception $e){
                return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
            }

            return json(['code' => 1, 'data' => '', 'msg' => '删除智能问答成功']);
        }
    }

    // 生成按钮
    private function makeBtn($id)
    {
        $operate = '<a href="' . url('robot/editword', ['robot_id' => $id]) . '">';
        $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';

        $operate .= '<a href="javascript:userDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
        $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';

        return $operate;
    }
}
