<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;


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 = '/admin';

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

    /**
     * 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',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
            // 'role' => 'string',
            // 'class' => 'string',
            // 'roll' => 'string',
            // 'studentid' => 'string',
            // 'fathername' => 'string',
            // 'f_occupation' => 'string',
            // 'mothername' => 'string',
            // 'm_occupation' => 'string',
            // 'present_address' => 'string',
            // 'parmanent_address' => 'string',
            // 'dob' => 'string',
            // 'age' => 'string',
            // 'religion' => 'string',
            // 'bloodgroup' => 'string',
            // 'jscroll' => 'string',
            // 'sibling' => 'string',
            // 'year' => 'string',
            // 'mobile' => 'string',
            // 'picture' => 'string',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param array $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        $request = request();

        $profileImage = $request->file('picture');
        if (!empty($profileImage)) {
            $profileImageSaveAsName = time() . '.' . $profileImage->getClientOriginalExtension();
            $upload_path = public_path('/images');
            $profile_image_url = $profileImageSaveAsName;
            $success = $profileImage->move($upload_path, $profileImageSaveAsName);
        } else {
            $profile_image_url = '';
        }

        $user = User::where('email', $data['email'])->first();
        
        if(!$user){
            return User::create([
                'category' => $data['category'],
                'name' => $data['name'],
                'email' => $data['email'],
                'password' => bcrypt($data['password']),
                'role' => $data['role'],
                'class' => $data['class'],
                'roll' => $data['roll'],
                'group_name' => $data['group_name'],
                'section' => $data['section'],
                'session' => $data['session'],
                'skill' => $data['skill'],
                'fathername' => $data['fathername'],
                'f_occupation' => $data['f_occupation'],
                'mothername' => $data['mothername'],
                'm_occupation' => $data['m_occupation'],
                'present_address' => $data['present_address'],
                'parmanent_address' => $data['parmanent_address'],
                'dob' => $data['dob'],
                'age' => $data['age'],
                'religion' => $data['religion'],
                'bloodgroup' => $data['bloodgroup'],
                'hobby' => $data['hobby'],
                'institute' => $data['institute'],
                'year' => $data['year'],
                'mobile' => $data['mobile'],
                'picture' => $profile_image_url,
                'permission' => $data['permission'],
    
    
            ]);
        }else{
            return back()->with('error', 'Email already registered.');
        }
    }


}