<?php

namespace App\Http\Controllers;

use Auth;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Auth\Events\Registered;
use App\Models\Consultant;
use App\Models\User;

class ConsultantController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $consultants = Consultant::all();
        return view('dashboard.consultants.index', compact('consultants'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("register");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
     {
         $validator = Validator::make($request->all(), [
             'pec_register' => 'string',
             'user_title' => 'required|string',   // required and email format validation
             'client_reg' => 'required|string',
            'mname' =>'required|string', // required and number field validation
            'fname' => 'required|string',
            'lname' => 'required|string',
            'gender' => 'required|string',
            'country' => 'required|string',
            'individual_firm' => 'string',
            'email1' => 'string',
            // 'company_name' => 'string',
            // 'ownership' => 'string',
            // 'mailing_address' => 'string',
            // 'parnanent_address' => 'string',
            // 'street_address' => 'string',
            // 'city' => 'string',
            // 'region' => 'string',
            // 'province' => 'string',

            // 'phone' => 'string',
            // 'fax' => 'string',
            // 'project_profile_codes' => 'string',
            // 'description' => 'string',
            // 'pec_certificate' => 'sometimes|files|image|mimes:jpeg,png,jpg,gif,svg|max:5000',

            // 'postal_code' => 'string',
            // 'website_link' => 'string',
            // 'filename' => 'required',
            // 'imgname' => 'required',
            // 'imagename' => 'required',
            // 'engineering' => 'string',
            // 'allied' => 'string',
            // 'alternative_email' => 'string',
            // 'employee' => 'string',


        ]); // create the validations
        if ($validator->fails())   //check all validations are fine, if not then redirect and show error messages
        {

            return back()->withInput()->withErrors($validator);
            // validation failed redirect back to form
            // if (request()->hasFile('pec_certificate')){
            //     request()->validate([
            //         'pec_certificate' => 'sometimes|files|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
            //     ]);
            // }



        } else {
            //validations are passed, save new user in database
            $Consultant = new Consultant;
            $Consultant->pec_register = $request->pec_register;
            $Consultant->client_reg = $request->client_reg;
            $Consultant->user_title = $request->user_title;
            $Consultant->mname = $request->mname;
            $Consultant->fname = $request->fname;
            $Consultant->lname = $request->lname;
            $Consultant->gender = $request->gender;
            $Consultant->country = $request->country;
            $Consultant->individual_firm = $request->individual_firm;
            $Consultant->email1 = $request->email1;
            // $Consultant->company_name = $request->company_name;
            // $Consultant->ADB = $request->ADB;
            // $Consultant->ownership = $request->ownership;
            // $Consultant->mailing_address = $request->mailing_address;
            // $Consultant->parnanent_address = $request->parnanent_address;
            // $Consultant->street_address = $request->street_address;
            // $Consultant->firm = $request->firm;
            // $Consultant->city = $request->city;
            // $Consultant->region = $request->region;
            // $Consultant->province = $request->province;
            // $Consultant->email = $request->email;
            // $Consultant->phone = $request->phone;
            // $Consultant->fax = $request->fax;
            // $Consultant->project_profile_codes = $request->project_profile_codes;
            // $Consultant->description = $request->description;
            // $Consultant->pec_certificate = $request->pec_certificate;
            // $Consultant->individual_codes = $request->individual_codes;
            // // $Consultant->individual_certificate = $request->individual_certificate;
            // $Consultant->postal_code = $request->postal_code;
            // $Consultant->website_link = $request->website_link;
            // $Consultant->filename = $request->filename;
            // $Consultant->imgname = $request->imgname;
            // $Consultant->imagename = $request->imagename;
            // $Consultant->engineering = $request->engineering;
            // $Consultant->allied = $request->allied;
            // $Consultant->alternative_email = $request->alternative_email;
            // $Consultant->employee = $request->employee;
            $Consultant->save();

            $user = User::create([
                'fname' => $request->fname,
                'lname' => $request->lname,
                'password' => bcrypt($request->password),
                'email' => $request->email1,
                'contact_number'=> $request->phone,
                 'menuroles' => 'consultant',
            ]);
            $user->assignRole('consultant');
            event(new Registered($user));

            if (auth()->attempt(array('email' => $request->email1, 'password' => $request->password),true)) {
                return redirect("/consultant_dashboard")->with('success', 'You have successfully registered, Login to access your dashboard');
            }
            // return redirect("dashboard")->with('success', 'You have successfully registered, Login to access your dashboard');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Consultant  $consultant
     * @return \Illuminate\Http\Response
     */
    public function show(Consultant $consultant)
    {
        //
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Consultant  $consultant
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'pec_register' => 'string',
            'user_title' => 'required|string',   // required and email format validation
            'client_reg' => 'required|string',
            'mname' =>'required|string', // required and number field validation
            'fname' => 'required|string',
            'lname' => 'required|string',
            'gender' => 'required|string',
            'country' => 'required|string',
            'employee' => 'string',

            'individual_firm' => 'string',
            'company_name' => 'string',
            'ownership' => 'string',
            'mailing_address' => 'string',
            'parnanent_address' => 'string',
            'street_address' => 'string',
            'city' => 'string',
            'region' => 'string',
            'province' => 'string',

            'phone' => 'string',
            'fax' => 'string',
            'project_profile_codes' => 'string',
            'description' => 'string',
            'pec_certificate' => 'sometimes|files|image|mimes:jpeg,png,jpg,gif,svg|max:5000',

            'postal_code' => 'string',
            'website_link' => 'string',
            // 'filename' => 'required',
            // 'imgname' => 'required',
            // 'imagename' => 'required',
            'engineering' => 'string',
            'allied' => 'string',
            'alternative_email' => 'string',
        ]); // create the validations

        if ($validator->fails())   //check all validations are fine, if not then redirect and show error messages
        {
            return back()->withInput()->withErrors($validator);
        } else {
            //validations are passed, save new user in database
            $Consultant = Consultant::where('email1', Auth::user()->email)->first();
            $Consultant->pec_register = $request->pec_register;
            $Consultant->client_reg = $request->client_reg;
            $Consultant->user_title = $request->user_title;
            $Consultant->mname = $request->mname;
            $Consultant->fname = $request->fname;
            $Consultant->lname = $request->lname;
            $Consultant->gender = $request->gender;
            $Consultant->country = $request->country;
            $Consultant->individual_firm = $request->individual_firm;
            $Consultant->company_name = $request->company_name;
            $Consultant->ADB = $request->ADB;
            $Consultant->ownership = $request->ownership;
            $Consultant->mailing_address = $request->mailing_address;
            $Consultant->parnanent_address = $request->parnanent_address;
            $Consultant->street_address = $request->street_address;
            // $Consultant->firm = $request->firm;
            $Consultant->city = $request->city;
            $Consultant->region = $request->region;
            $Consultant->province = $request->province;
            // $Consultant->email = $request->email;
            $Consultant->phone = $request->phone;
            $Consultant->fax = $request->fax;
            $Consultant->project_profile_codes = $request->project_profile_codes;
            $Consultant->description = $request->description;
            $Consultant->pec_certificate = $request->pec_certificate;
            // $Consultant->individual_codes = $request->individual_codes;
            // $Consultant->individual_certificate = $request->individual_certificate;
            $Consultant->postal_code = $request->postal_code;
            $Consultant->website_link = $request->website_link;
            // $Consultant->filename = $request->filename;
            // $Consultant->imgname = $request->imgname;
            // $Consultant->imagename = $request->imagename;
            $Consultant->engineering = $request->engineering;
            $Consultant->allied = $request->allied;
            $Consultant->alternative_email = $request->alternative_email;
            $Consultant->employee = $request->employee;
            $Consultant->save();

            return redirect("consultant/profile");
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Consultant  $consultant
     * @return \Illuminate\Http\Response
     */
    public function destroy(Consultant $consultant)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function edit_profile()
    {
        $user = Consultant::where('email1', Auth::user()->email)->first();
        return view('dashboard.consultants.profile', compact('user'));
    }
}
