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

class Settings 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 || intval($param['searchText']) == 0) {
//                $where['user_account|user_name|user_job_number|fullname'] = ['like', '%' . $param['searchText'] . '%'];
//            }
            //客服组
//            if ($param['group_id'] != '' && $param['group_id'] != 0) {
//                $where['group_id'] = $param['group_id'];
//            }
            $result = db('guestenter')->where($where)->limit($offset, $limit)->order('sort', 'asc')->select();
            //客服组
            $group = db('groups')->select();
            foreach($result as $key=>$vo){
                // 优化显示头像
                $result[$key]['image'] = '<img src="' . $vo['image'] . '" height="40px">';

                if($vo['group_id'] == 'all'){
                    $result[$key]['user_group'] = '所有客服组';
                }
                for($i=0;$i<count($group);$i++){
                    if($vo['group_id'] == $group[$i]['id']){
                        $result[$key]['user_group'] = $group[$i]['name'];
                    }
                }

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

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

            return json($return);

        }
        $settings = db('settings')->where('id',1)->find();

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

        return $this->fetch();
    }

    // 添加访客端入口
    public function add()
    {
        if(request()->isPost()){

            $param = input('post.');
            unset($param['file']); // 删除layui头像上传隐藏字段
            // 检测账号
            if(!empty($param['name'])){
                $user = db('guestenter')->where('name', $param['name'])->find();
                if(!empty($user)){
                    return json(['code' => -1, 'data' => '', 'msg' => '该名称已存在']);
                }
            }

            if(strlen($param['name'])>12) {
                return json(['code' => -8, 'data' => '', 'msg' => '名称字数超过限制']);
            }

            if(!is_numeric($param['sort'])) {
                return json(['code' => -6, 'data' => '', 'msg' => '排序未输入整数']);
            }

            if(empty($param['group_id'])){
                return json(['code' => -3, 'data' => '', 'msg' => '请选择分组']);
            }
             //检测头像
            if(empty($param['image'])){
                return json(['code' => -4, 'data' => '', 'msg' => '请上传图片']);
            }

            try{
                db('guestenter')->insert($param);
            }catch(\Exception $e){
                return json(['code' => -5, 'data' => '', 'msg' => $e->getMessage()]);
            }

            return json(['code' => 1, 'data' => 'settings/index', 'msg' => '添加功能菜单成功']);
        }

        $this->assign([
            'groups' => db('groups')->select(),
            'status' => config('kf_status')
        ]);

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

    // 编辑访客端入口
    public function edit()
    {
        if(request()->isAjax()){

            $param = input('post.');
            unset($param['file']); // 删除layui头像上传隐藏字段

            if(strlen($param['name'])>12) {
                return json(['code' => -8, 'data' => '', 'msg' => '名称字数超过限制']);
            }

            if(!is_numeric($param['sort'])) {
                return json(['code' => -6, 'data' => '', 'msg' => '排序未输入整数']);
            }

            if(empty($param['group_id'])){
                return json(['code' => -2, 'data' => '', 'msg' => '请选择分组']);
            }

            $name = db('guestenter')->where('name',$param['name'])->where('id','<>',$param['id'])->select();
            if(!empty($name)){
                return json(['code' => -3, 'data' => '', 'msg' => '此名称已存在']);
            }

            $info = array();
            $info['name'] = $param['name'];
            $info['image'] = $param['image'];
            $info['group_id'] = $param['group_id'];
            $info['sort'] = $param['sort'];

            try{
                db('guestenter')->where('id', $param['id'])->update($info);
            }catch(\Exception $e){
                return json(['code' => -6, 'data' => '', 'msg' => $e->getMessage()]);
            }
            return json(['code' => 1, 'data' => 'settings/index', 'msg' => '编辑功能菜单成功']);
        }
        $id = input('param.id/d');
        $info = db('guestenter')->where('id', $id)->find();

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

    // 删除访客端入口
    public function del()
    {
        if(request()->isAjax()){
            $id = input('param.id/d');

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

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

    // 上传客服头像
    public function upAvatar()
    {
        if(request()->isAjax()) {

            $file = request()->file('file');
            if (!empty($file)) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if ($info) {
                    $src = '/uploads' . '/' . date('Ymd') . '/' . $info->getFilename();
                    return json(['code' => 0, 'data' => ['src' => $src], 'msg' => 'ok']);
                } else {
                    // 上传失败获取错误信息
                    return json(['code' => -1, 'data' => '', 'msg' => $file->getError()]);
                }
            }
        }
    }

    // 生成按钮
    private function makeBtn($id)
    {
        $operate = '<a style="margin-left:5px;" href="' . url('settings/edit', ['id' => $id]) . '">';
        $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';

        $operate .= '<a style="margin-left:5px;" 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;
    }

    //自动邀请设置
    public function update(){
        if(request()->isPost()){

            $param = input('post.');

            $info['stop_time'] = $param['overtime'];
            $info['copywriting'] = $param['overtimeInfo'];

            try{
                db('settings')->where('id', $param['id'])->update($info);
            }catch(\Exception $e){
                return json(['code' => -5, 'data' => 'settings/index', 'msg' => $e->getMessage()]);
            }
            return json(['code' => 1, 'data' => $this->redirect('settings/index'), 'msg' => '自动邀请设置成功']);
        }
    }

    //访客端入口上边距设置
    public function margin(){
        if(request()->isPost()){

            $param = input('post.');

            $info['margin_top'] = $param['margin-top'];

            try{
                db('settings')->where('id', $param['id'])->update($info);
            }catch(\Exception $e){
                return json(['code' => -6, 'data' => 'settings/index', 'msg' => $e->getMessage()]);
            }
            return json(['code' => 1, 'data' => $this->redirect('settings/index'), 'msg' => '自动邀请设置成功']);
        }
    }
}
