<?php
namespace app\service\controller;

class Services extends Base
{
    //客服信息
    public function index()
    {
        $token = input("param.token/s");
        $res = model('Services')->checktoken($token);
        if($res == -1){
            return $res;
        }
        $user_id = $res;
        //客服信息
        $service = db('users')->where('id',$user_id )->select();
        //print_r($service);exit;
        return $service;
    }

    //用户信息
    public function account()
    {
        if(request()->isPost()) {
            $account_id = input("param.account_id/s");
            //客服信息
            $account = db('accounts')->where('id', $account_id)->select();

            return $account;
        }
    }

    //修改/新增用户信息
    public function update()
    {
        if(request()->isPost()) {
            $account_id = input("param.account_id/s");
            $account_email = input("param.account_email/s");
            $phone = input("param.phone/s");
            $address = input("param.address/s");
            $remark = input("param.remark/s");
            $label_id = input("param.label_id/s");
            $nick_name = input("param.nick_name/s");

            if(empty($label_id) || $label_id == 0){
                $param = [
                    'account_email' => $account_email,
                    'account_phone' => $phone,
                    'address' => $address,
                    'nick_name' => $nick_name,
                    'remark' => $remark
                ];
            }else{
                $param = [
                    'account_email' => $account_email,
                    'account_phone' => $phone,
                    'address' => $address,
                    'label_id' => $label_id,
                    'nick_name' => $nick_name,
                    'remark' => $remark
                ];
            }

            $account = db('accounts')->where('id', $account_id)->select();
            if(!empty($account)){
                try{
                    db('accounts')->where('id', $account_id)->update($param);
                    return json(['code' => 1, 'data' => '', 'msg' => '保存成功']);
                }catch(\Exception $e){
                    return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
                }
            }else{
                try{
                    db('accounts')->insertGetId($param);
                    return json(['code' => 1, 'data' => '', 'msg' => '保存成功']);
                }catch(\Exception $e){
                    return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
                }
            }
        }
    }

    //获取用户信息
    public function accountInfo()
    {
        if(request()->isPost()) {
            $account_id = input("param.account_id/s");

            $account = db('accounts')->field('id,account_name,nick_name,account_email,account_phone,address,remark,label_id')->where('id', $account_id)->find();
            if(empty($account)){
                return json(['code' => -1, 'data' => '', 'msg' => '用户不存在']);
            }

            $label = db('accountslabel')->where('id', $account['label_id'])->find();
            if(!empty($label)){
                $account['label'] = $label['name'];
            }
            return json(['code' => 1, 'data' => $account, 'msg' => '成功']);
        }
    }

    // 客服信息修改
    public function updateinfo()
    {
        if(request()->isPost()){

            $token = input("param.token/s");
            $res = model('Services')->checktoken($token);
            if($res == -1){
                return $res;
            }
            $user_id = $res;

            //$user_id = input("param.user_id/s");
            $user_name = input("param.user_name/s");
            $user_email = input("param.user_email/s");
            $phone = input("param.phone/s");
            $signature = input("param.signature/s");
            $fullname = input("param.fullname/s");

//            $username = db('users')->where('user_name', $user_name)->where('id', '<>', $user_id)->find();
//            if(!empty($username)){
//                return json(['code' => -1, 'data' => '', 'msg' => '该客服已经存在']);
//            }

            // 更新客服信息
            $param = [
                'user_name' => $user_name,
                'user_email' => $user_email,
                'signature' => $signature,
                'fullname' => $fullname,
                'phone' => $phone,
                'expire_time' => time()
            ];
            db('users')->where('id', $user_id)->update($param);
            $user = db('users')->where('id', $user_id)->select();
            $group = db('groups')->where('id', $user[0]['group_id'])->find();
            $user[0]['group'] = $group['name'];

            return json(['code' => 1, 'data' => ['user' => $user[0]], 'msg' => '修改成功']);
        }
    }

    // 客服密码修改
    public function updatepwd()
    {
        if(request()->isPost()){

            $token = input("param.token/s");
            $res = model('Services')->checktoken($token);
            if($res == -1){
                return json(['code' => -1, 'data' => '', 'msg' => '请先登陆']);
            }
            $user_id = $res;

            //$user_id = input("param.user_id/s");
            $password = input("param.password/s");
            $new_password = input("param.new_password/s");

            $user = db('users')->where('id', $user_id)->find();
            if(empty($user)){
                return json(['code' => -1, 'data' => '', 'msg' => '客服不存在']);
            }else{
                if($user['user_pwd'] != md5($password . config('salt'))){
                    return json(['code' => -2, 'data' => '', 'msg' => '原密码不正确']);
                }
            }

            // 更新密码
            $param = [
                'user_pwd' => md5($new_password . config('salt'))
            ];
            db('users')->where('id', $user_id)->update($param);

            return json(['code' => 1, 'data' => '', 'msg' => '密码修改成功']);
        }
    }

}
