<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use GuzzleHttp\Client;
class AdminuserController extends Controller{
    public function add_user(Request $request){
        $data=$request->all();
        $data['password']=MD5($request->password);
        $user=DB::table('admin_user')->where('name',$request->name)->first();
        if ($user){
            return $this->error(300);
        }
        DB::table('admin_user')->insert($data);
        return $this->success([]);
    }

    public function del_user(Request $request){
        DB::table('admin_user')->where('id',$request->id)->delete();
        return $this->success([]);
    }
    public function update_user(Request $request){
        $data['name']=$request->name;
//        $name=DB::table('admin_user')->where('id',$request->id)->value('name');
//        $data['avatar']=$request->avatar;
        $user=DB::table('admin_user')->where('name',$request->name)->first();
        if ($user){
            if ($user->id==$request->id){
                $data['role']=$request->role;
                DB::table('admin_user')->where('id',$request->id)->update($data);
                return $this->success([]);
            }else{
                return $this->error(300);
            }
        }

        $data['role']=$request->role;
        DB::table('admin_user')->where('id',$request->id)->update($data);
        return $this->success([]);



    }

    public function all_user(){
        $user=DB::table('admin_user')->whereNotIn('name',['admin','root'])->get();
        return $this->success_list($user,'ok',0);
    }


    public function update_name(Request $request){
        $data=DB::table('service')->where('war_names',$request->war_name)->select('uid')->first();
        $user=DB::table('user')->where('id',$data->uid)->first();
        DB::table('user')->where('up_phone',$user->phone)->update(['up_name'=>$request->name]);
        DB::table('user')->where('id',$data->uid)->update(['nickname'=>$request->name,'name'=>$request->name]);
        return $this->success([]);
    }
    public function add_phone(Request $request){
        $data=DB::table('service')->where('war_names',$request->war_name)->first();
        $d=DB::table('user')->where('id',$data->uid)->first();
        $re=DB::table('service')->where('phone',$request->phone)->first();
        if ($re){
            $res=DB::table('user')->where('phone',$request->phone)->first();
            if ($res){
                DB::table('user')->where('id',$res->id)->update(['level_name'=>'代理公司','up_name'=>$d->nickname,'up_phone'=>$d->phone]);
            }
            return $this->error(200);
        }
        $res=DB::table('user')->where('phone',$request->phone)->first();
        if ($res){
            DB::table('user')->where('id',$res->id)->update(['level_name'=>'代理公司','up_name'=>$d->nickname,'up_phone'=>$d->up_phone]);
        }
        DB::table('service')
            ->where('war_names',$request->war_name)
            ->insert(['phone'=>$request->phone,'uid'=>$data->uid,'war_names'=>$request->war_name]);
        return $this->success([]);
    }

}