<?php
/**
 * User: nickbai
 * Date: 2017/10/23 13:33
 * Email: 1902822973@qq.com
 */
namespace app\admin\controller;

use app\admin\model\Servicenotice as ServicenoticeModel;
use app\admin\model\Users as Usersmodel;

class Kfnotice extends Base
{
    // 管理员列表
    public function index()
    {

        if(request()->isAjax()){

            $param = input('param.');

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

            $where = [];
            if (!empty($param['searchText'])) {
                $where['atext'] = ['like', '%' . $param['searchText'] . '%'];
            }

            $model = new ServicenoticeModel();
            $result = $model->where($where)->limit($offset, $limit)->order('id','desc')->select();

            foreach($result as $key=>$vo){
                $vo->readtime = empty($vo->readtime) ? '' : date('Y-m-d H:i:s', $vo->readtime);
                $vo->uid = $vo->kfuser->user_name;
                $result[$key] = array_merge($vo->toArray(),['operate'=>$this->makeBtn($vo->id)]) ;
            }
            $return['total'] = db('servicenotice')->where($where)->count();  //总数据
            $return['rows'] = $result;

            return json($return);

        }

        return $this->fetch();
    }

    // 添加留言
    public function add()
    {
        if(request()->isPost()){

            $atext = input('post.atext');
            $uid = intval(input('post.uid',0));

            if (empty($atext)){
                return json(['code' => -1, 'data' => '', 'msg' => '内容不能为空']);
            }

            if ($uid){
                $arr = [
                    'uid'=>$uid,
                    'atext'=>$atext
                ];
                $datas[] = $arr;
            }else{
                $Users = (new Usersmodel())->where(['status'=>1])->select();
                if (!$Users){
                    return json(['code' => -2, 'data' => '', 'msg' => '没有可用客服']);
                }

                $datas = [] ;
                foreach ($Users as $val){
                    $arr = [
                        'uid'=>$val->id,
                        'atext'=>$atext
                    ];
                    $datas[] = $arr;
                }
            }

            try{
                db('servicenotice')->insertAll($datas);
            }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 del()
    {
        if(request()->isAjax()){
            $id = input('param.id/d');
            //return $id;

            try{
                db('servicenotice')->where('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="javascript:Del(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
        $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';

        return $operate;
    }
}