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

class Accounts 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['account_name'] = $param['searchText'];
            }
            $result = db('accounts')->where($where)->limit($offset, $limit)->order('add_time', 'desc')->select();
            foreach($result as $key=>$vo){
                $result[$key]['add_time'] = date('Y-m-d H:i:s',$result[$key]['add_time']);
                // 生成操作按钮
                $result[$key]['operate'] = $this->makeBtn($vo['id']);
            }
            $return['total'] = db('accounts')->count();  //总数据
            $return['rows'] = $result;
            return json($return);
        }
        return $this->fetch();
    }

    // 重置用户密码为 123456
    public function resetPwd()
    {
        if(request()->isAjax()){
            $id = input('param.id/d');
            $password = md5('123456' . config('salt'));
            try{
                db('accounts')->where('id', $id)->update(['password' => $password]);
            }catch(\Exception $e){
                return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
            }
            return json(['code' => 1, 'data' => 'accounts/index', 'msg' => '重置密码成功']);
        }
    }

    // 编辑用户
    public function edit()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $info = array();
            $rules = '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/';

            if(!preg_match($rules,$param['account_email'])){
                return json(['code' => -9, 'data' => '', 'msg' => '请输入正确的邮箱']);
            }

            if(!preg_match("/^1[34578]\d{9}$/", $param['account_phone'])){
                return json(['code' => -10, 'data' => '', 'msg' => '请输入正确的手机号码']);
            }

            $info['account_email'] = $param['account_email'];
            $info['account_phone'] = $param['account_phone'];
            $info['address'] = $param['address'];
            $info['label'] = $param['label'];
            $info['remark'] = $param['remark'];
            $info['status'] = $param['status'];
            $info['user_id'] = $param['user_id'];

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

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

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

        //转为日期时间格式
        $info['add_time'] = date('Y-m-d H:i:s',$info['add_time']);

        //所有客服
        $users = db('users')->select();

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

    // 删除用户
    public function delAccount()
    {
        if(request()->isAjax()){
            $id = input('param.id/d');
            try{
                db('accounts')->where('id', $id)->delete();
            }catch(\Exception $e){
                return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
            }
            return json(['code' => 1, 'data' => 'accounts/index', 'msg' => '删除客服成功']);
        }
    }

    // 生成按钮
    private function makeBtn($id)
    {
        $operate = '<a href="javascript:resetPwd(' . $id . ')"><button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 重置密码</button></a>';

        $operate .= '<a style="margin-left:5px;" href="' . url('Accounts/edit', ['id' => $id]) . '"><button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';

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

        return $operate;
    }
}