<?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_name = input("param.account_name/s");
            $account_email = input("param.account_email/s");
            $phone = input("param.phone/s");
            $address = input("param.address/s");
            $label = input("param.label/s");
            $remark = input("param.remark/s");


            $param = [
                'account_name' => $account_name,
                'account_email' => $account_email,
                'phone' => $phone,
                'address' => $address,
                'label' => $label,
                'remark' => $remark
            ];

            $account = db('accounts')->where('id', $account_id)->select();
            if(!empty($account)){
                try{
                    db('accounts')->where('id', $account_id)->update($param);
                }catch(\Exception $e){
                    return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
                }
            }else{
                try{
                    db('accounts')->insertGetId($param);
                }catch(\Exception $e){
                    return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
                }
            }
            return json(['code' => 1, 'data' => '', '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_job_number = input("param.user_job_number/s");
            $user_email = input("param.user_email/s");
            $phone = input("param.phone/s");
            $user_avatar = input("param.user_avatar/s");

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

            $userjobnumber = db('users')->where('user_job_number', $user_job_number)->where('id', '<>', $user_id)->find();
            if(!empty($userjobnumber)){
                return json(['code' => -2, 'data' => '', 'msg' => '该工号已经存在']);
            }

            $useremail = db('users')->where('user_email', $user_email)->where('id', '<>', $user_id)->find();
            if(!empty($useremail)){
                return json(['code' => -3, 'data' => '', 'msg' => '该邮箱已经存在']);
            }


            // 更新客服信息
            $param = [
                'user_name' => $user_name,
                'user_job_number' => $user_job_number,
                'user_email' => $user_email,
                'user_avatar' => $user_avatar,
                'phone' => $phone
            ];
            db('users')->where('id', $user_id)->update($param);

            return json(['code' => 1, 'data' => url('service/index'), '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' => '密码修改成功']);
        }
    }

}
