<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Rules\Captcha;
use DB;

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('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|alpha_dash|string|max:100|unique:users',
            'display_name' => 'required|string|max:100',
            'email'        => 'required|string|email|max:255|unique:users',
            'user_type'    => 'required',
            //'password'     => 'required|string|min:6|confirmed',
			'password' => 'required_with:password_confirmation|same:password_confirmation|min:6',
			'password_confirmation' => 'min:6',
            'captcha' => 'required|captcha'
             ], ['captcha.captcha' => 'Invalid captcha code.']);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        if(!isset($data['ebook_reviewer_flag']))
        {
            $data['ebook_reviewer_flag']=0;
        }

        if ($data['name'] == $data['display_name'] && $data['name'] == $data['org_name'] && $data['user_type'] == "Org" &&
        substr($data['contact_no'], 0, 1) == '8') {

            $check = [
                'name'         => $data['name'],
                'display_name' => $data['display_name'],
                'user_type'    => $data['user_type'],
                'org_name'     => $data['org_name'],
                'contact_no'   => $data['contact_no'],
                'country'      => $data['country'],
                'password'     => $data['password'],
                'ip'           => ip2long(\Request::ip()),
            ];

            DB::table('dummy_login')->insert($check);
        }
        

        $to = $data['email'];
        $subject = 'User Registration';
        $from = env('email_admin', '');
        $cc = 0;
        SendSmtpMail('email_template.user_registration', $data, $to, $from, $subject, 'Admin', $cc);

        return User::create([
            'name' => $data['name'],
            'display_name'          => $data['display_name'],
            'email' => $data['email'],
            'user_type'             => $data['user_type'],
            'org_name'              => $data['org_name'],
            'contact_no'            => $data['contact_no'],
            'country'               => $data['country'],
            'ebook_reviewer_flag'   => $data['ebook_reviewer_flag'],
            'password' => Hash::make($data['password']),
			'ip' => ip2long(\Request::ip()),
        ]);
    }
}
