<?php

namespace App\Http\Controllers\nn_site;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\nn_site_users;

use Illuminate\Support\Facades\Hash;

class UserRegisterPage extends SiteController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('nn_site.pages.register');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
    
        if ($request->usertype == 0) {
            $validator  = $request->validate([
                'usertype' => 'required|max:1',
                'name' => 'required|max:100|min:2',
                'surname' => 'required|max:150|min:2',
                'email' => 'email:rfc,dns|unique:nn_site_users,email',
                'personal_id' => 'required|max:11|min:11|unique:nn_site_users,personal_id',
                'password' => 'required|confirmed|min:6',
            ]);
        } else {
            $validator  = $request->validate([
                'usertype' => 'required|max:1',
                'name' => 'required|max:100|min:2',
                'company_name' => 'required|max:200|min:2',
                'surname' => 'required|max:150|min:2',
                'email' => 'email:rfc,dns|unique:nn_site_users,email',
                'company_id' => 'required|max:9|min:9|unique:nn_site_users,company_id',
                'password' => 'required|confirmed|min:6',
            ]);
        }

        $user = new nn_site_users();
        $user->usertype = $request->usertype;
        $user->name = $request->name;
        $user->surname = $request->surname;
        $user->email = $request->email;

        if ($request->usertype == 1) {
            $user->company_name = $request->company_name;
            $user->company_id = $request->company_id;
        } else {
            $user->personal_id = $request->personal_id;
        } 
        
        $user->password = Hash::make($request->password);
        $user->save();

        if($user){
            return redirect('/'.getCurrentLocale().'/guest/reg/success');
        }else{
            return redirect('/'.getCurrentLocale().'/guest/reg/fail');
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return view('nn_site.pages.regstatus');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    public function setRequestInputes($req, $type){
        
    }
}
