<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use App\UserDetail;
use Carbon\Carbon;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Str;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
//        $this->middleware('auth');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255', 'unique:users'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:6', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
    public function register(Request $request)
    {
        $headTo=$request->input('headTo')??'';
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));
        $phone=$request->input('phone');
        if ($phone) UserDetail::query()->create([
            'user_id'=>$user['id'],
            'full_name'=>$user['name'],
            'mobile_phone'=>$phone,
            'created_at'=>Carbon::now()->toDateTimeString(),
            'updated_at'=>Carbon::now()->toDateTimeString(),
        ]);
        $rolesIds=$request->input('role')??'';
        if($rolesIds){
            $roleIdArr=explode(',',$rolesIds);
            $user->roles()->sync($roleIdArr);
        }
        $supplierIds=$request->input('supplier')??'';
        if($supplierIds){
            $supplierIdArr=explode(',',$supplierIds);
            $user->suppliers()->sync($supplierIdArr);
        }
//        $this->guard()->login($user);
        $carrierIds=$request->input('carrier')??'';
        if ($carrierIds){
            $carrierIds=explode(',',$carrierIds);
            $user->logistics()->sync($carrierIds);
        }
        $userWorkgroupId=$request->input('userWorkgroupID');
        if ($userWorkgroupId){
            $user->userWorkgroups()->sync([$userWorkgroupId]);
        }
        app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),$user['id']);
        if($headTo){return redirect(url($headTo))->with('successTip',"录入用户 {$user->name} 成功");}
        return $this->registered($request, $user)
            ?: redirect($this->redirectPath());
    }
}
