<?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\Client;
use App\Models\User;

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("client_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(), [

            'user_title' => 'string',   // required and email format validation
            'mname' =>'string', // required and number field validation
            'fname' => 'string',
            'lname' => 'string',
            'gender' => 'string',
            'country' => 'required|string',
            // 'company_name' => 'string',
            // 'ADB' => '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',
            // 'pec_register' => 'string',
            // 'postal_code' => 'string',
            // 'website_link' => 'string',
            // 'filename' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
            // 'imgname' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
            // 'imagename' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
            // 'engineering' => 'string',
            // 'allied' => 'string',
            'email1' => 'string', 'email', 'max:255', 'unique:users',

            // '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('filename')){
                request()->validate([
                    'filename' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
                ]);
            }
            if (request()->hasFile('imgname')){
                request()->validate([
                    'imgname' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
                ]);
            }
            if (request()->hasFile('imagename')){
                request()->validate([
                    'imagename' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
                ]);
            }

        } else {
            //validations are passed, save new user in database

            $Client = new Client;
            $Client->user_title = $request->user_title;
            $Client->mname = $request->mname;
            $Client->fname = $request->fname;
            $Client->lname = $request->lname;
            $Client->gender = $request->gender;
            $Client->country = $request->country;
            // $Client->company_name = $request->company_name;
            // $Client->ADB = $request->ADB;
            // $Client->ownership = $request->ownership;
            // $Client->mailing_address = $request->mailing_address;
            // $Client->parnanent_address = $request->parnanent_address;
            // $Client->street_address = $request->street_address;
            // $Client->city = $request->city;
            // $Client->region = $request->region;
            // $Client->province = $request->province;
            // $Client->phone = $request->phone;
            // $Client->fax = $request->fax;
            // $Client->pec_register = $request->pec_register;
            // $Client->postal_code = $request->postal_code;
            // $Client->website_link = $request->website_link;
            // $Client->filename = $request->filename;
            // $Client->imgname = $request->imgname;
            // $Client->imagename = $request->imagename;
            // $Client->engineering = $request->engineering;
            // $Client->allied = $request->allied;
            $Client->email1 = $request->email1;
            // $Client->alternative_email = $request->alternative_email;
            $Client->employee = $request->employee;
            $Client->save();

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

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

        }
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\client  $client
     * @return \Illuminate\Http\Response
     */
    public function edit(client $client)
    {
        // return view('clients.edit', ['client' => $client]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\client  $client
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $validator = Validator::make($request->all(), [

        'user_title' => 'string',   // required and email format validation
        'mname' =>'string', // required and number field validation
        'fname' => 'string',
        'lname' => 'string',
        'gender' => 'string',
        'country' => 'required|string',
        'company_name' => 'string',
        'ADB' => 'string',
        'ownership' => 'string',
        'mailing_address' => 'string',
        'parnanent_address' => 'string',
        'experience' => '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',
        'pec_register' => 'string',
        'postal_code' => 'string',
        'website_link' => 'string',
        'filename' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
        'imgname' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
        'imagename' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
        'engineering' => 'string',
        'allied' => 'string',
      

        'alternative_email' => 'string',
        'employee' => 'string',
        ]);

    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('filename')){
            request()->validate([
                'filename' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
            ]);
        }
        if (request()->hasFile('imgname')){
            request()->validate([
                'imgname' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
            ]);
        }
        if (request()->hasFile('imagename')){
            request()->validate([
                'imagename' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
            ]);
        }

    } else {
        //validations are passed, save new user in database

        $Client = Client::where('email1', Auth::user()->email)->first();
        $Client->user_title = $request->user_title;
        $Client->mname = $request->mname;
        $Client->fname = $request->fname;
        $Client->lname = $request->lname;
        $Client->gender = $request->gender;
        $Client->country = $request->country;
        $Client->company_name = $request->company_name;
        $Client->ADB = $request->ADB;
        $Client->ownership = $request->ownership;
        $Client->mailing_address = $request->mailing_address;
        $Client->parnanent_address = $request->parnanent_address;
        $Client->experience = $request->experience;
        $Client->street_address = $request->street_address;
        $Client->city = $request->city;
        $Client->region = $request->region;
        $Client->province = $request->province;
        $Client->phone = $request->phone;
        $Client->fax = $request->fax;
        $Client->pec_register = $request->pec_register;
        $Client->postal_code = $request->postal_code;
        $Client->website_link = $request->website_link;
        $Client->filename = $request->filename;
        $Client->imgname = $request->imgname;
        $Client->imagename = $request->imagename;
        $Client->engineering = $request->engineering;
        $Client->allied = $request->allied;
       
        $Client->alternative_email = $request->alternative_email;
        $Client->employee = $request->employee;
        $Client->save();

        return redirect("client/profile");
    }
}
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\client  $client
     * @return \Illuminate\Http\Response
     */
    public function destroy(client $client)
    {
        //
    }
    public function edit_profile()
    {
        $user = Client::where('email1', Auth::user()->email)->first();
        return view('dashboard.clients.profile', compact('user'));
    }
    public function client_projects()
    {
        $user = Client::where('email1', Auth::user()->email)->first();
        return view('dashboard.clients.client_projects', compact('user'));
    }
}
