<?php
namespace app\admin\controller;

class Sensitivec extends Base
{	
	//敏感词管理
    public function index()
    {
    	if (request()->isAjax()) {
            $param  = input('param.');

            $limit  = $param['pageSize'];
            $offset = (($param['pageNumber'] - 1) * $limit);

            $where  = [];
            if (empty($param['searchText']) === false) {
                $where['sensitivewords_word'] = ['like', '%' . $param['searchText'] . '%'];
            }
            
            $result = db('sensitivewords')->where($where)->limit($offset, $limit)->select();

            foreach ($result as $key => $vo) {
                // 优化显示状态.
                if(1 === $vo['sensitivewords_status']) {
                    $result[$key]['sensitivewords_status'] = '<span style="color: #2fbe1b">启用</span>';
                } else {
                    $result[$key]['sensitivewords_status'] = '<span style="color: red">禁用</span>';
                }

                // 生成操作按钮.
                $result[$key]['operate'] = $this->makeBtn($vo['sensitivewords_id']);
            }

            // 总数据.
            $return['total'] = db('sensitivewords')->where($where)->count();  //总数据
            $return['rows'] = $result;

            return json($return);

        }

    	return $this->fetch();
    }

    //敏感词添加
    public function addsensitive(){
    	if(request()->isPost()){

            $param = input('post.');

            $has = db('sensitivewords')->field('sensitivewords_id')->where('sensitivewords_word', $param['sensitivewords_word'])->find();
            if(!empty($has)){
                return json(['code' => -1, 'data' => '', 'msg' => '该敏感词已经存在']);
            }

			$param['sensitivewords_time'] = date('Y-m-d H:i:s');
			$param['sensitivewords_uid'] = 1;//uid
			// var_dump($param);die;
            try{
                db('sensitivewords')->insert($param);
            }catch(\Exception $e){
                return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
            }

            return json(['code' => 1, 'data' => '', 'msg' => '添加敏感词成功']);
        }

        $this->assign([
            'status' => config('kf_status')
        ]);

        return $this->fetch();
    }

    //敏感词编辑
    public function editsensitive(){
    	if(request()->isAjax()){

            $param = input('post.');

            // 检测用户修改的管理员是否重复
            $has = db('sensitivewords')->where('sensitivewords_word', $param['sensitivewords_word'])->where('sensitivewords_id', '<>', $param['sensitivewords_id'])->find();
            if(!empty($has)){
                return json(['code' => -1, 'data' => '', 'msg' => '该敏感词已经存在']);
            }

            $param['sensitivewords_time'] = date('Y-m-d H:i:s');
            $param['sensitivewords_uid'] = 1;//uid

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

            return json(['code' => 1, 'data' => '', 'msg' => '编辑敏感词成功']);
        }

        $id = input('param.sensitivewords_id/d');
        $info = db('sensitivewords')->where('sensitivewords_id', $id)->find();

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

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

            try{
                db('sensitivewords')->where('sensitivewords_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('sensitivec/editsensitive', ['sensitivewords_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;
    }


}