<?php

namespace App\Http\Controllers;

use App\Imports\ExportersImport;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\Exporter;
use App\Models\User;
use App\Models\Meeting;
use Illuminate\Http\Request;
use App\Mail\VerifyUserEmail;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Auth;
use Hash;

class ExporterController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
         $meeting = Meeting::select('*')->where('notify', 0)->where('buyer_id', Auth::user()->buyer_exporter_id)->count('*');
        $exporters = Exporter::all();
        $industries = Exporter::select('industry')->distinct()->orderBy('industry')->get()->toArray();
        $industries = array_column($industries, 'industry');

        return view('dashboard.exporters.index', array(
            'exporters' => $exporters,
            'industries' => $industries,
        ), compact('meeting'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store_exporter(Request $request)
    {
        // dd($request->all());
        $request->validate([
            'contact_name'  => 'required|max:50',
            'category' => 'required',
            'phone'   => 'required|max:50',
            'country_code' => 'required',
            'email'  => 'required|unique:users|max:100',
            'country'  => 'required',
            'website'  => 'required|max:250',
            'company_name'  => 'required|max:200',
            'is_pasha_member' => 'required',
            'password' => ['string','min:4'],
            'services_offered' => 'required|max:250',
            'about_us' => 'required|max:250'
         
        ]);

        $exporter = new Exporter();
        $exporter->contact_name = $request->contact_name;
        $exporter->country  = $request->country;
        $exporter->email  = $request->email;
        $exporter->country_code = $request->country_code;
        $exporter->phone = $request->country_code.''.$request->phone;
        $exporter->website = $request->website;
        $exporter->company_name = $request->company_name;
        $exporter->services_offered = $request->services_offered;
        $exporter->about_us = $request->about_us;
        if ($request->category == "Other") {
            // $request->category = $request->other_category_buyer;
            $exporter->industry = $request->other_category;
        }
        else
        {
             $exporter->industry = $request->category;
        }
       
        $exporter->is_pasha_member = $request->is_pasha_member;
        //  dd($exporter);
        $exporter->save();

        $user = new User();
        $token = Str::random(40);    
        $user->name = $request->contact_name;
        $user->email = $request->email;
        $user->menuroles = 'exporter';
        $user->activation_token = $token;
        $user->password = Hash::make($request->password);
        $user->buyer_exporter_id = $exporter->id;
        //  dd($user);
        if ($user->save()) {
            $data['name'] = $request->contact_name;
                $data['token'] = $token;
                $data['email'] = $request->email;
                $response= Mail::to($request->email)->send(new VerifyUserEmail($data));
                
                $request->session()->flash('message', 'Thank you, You have been registered as P@SHA MEMBER successfully. An email has been sent to your email address containing an activation link. Please check your email and click on the link to activate your account. If you do not click the link your account will remain inactive and you will not be able to login. If you do not receive the email within a few minutes, please check your spam folder.');
            return redirect()->route('login');
        } 
        else {
            $request->session()->flash('message', 'Something went wrong!');
            // return redirect()->route('login');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Exporter  $exporter
     * @return \Illuminate\Http\Response
     */
    public function show(Exporter $exporter)
    {
        return view('dashboard.exporters.show', $exporter);
    }

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

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Exporter  $exporter
     * @return \Illuminate\Http\Response
     */
    public function destroy(Exporter $exporter, Request $request)
    {
        $exporter->delete();
        $request->session()->flash('message', "Successfully deleted exporter");
        $request->session()->flash('back', 'exporters.index');
        return view('dashboard.shared.universal-info');
    }

    /**
     * Import Data from excel sheet.
     */

    public function import(Request $request)
    {
        $validated = $request->validate([
            'exporters_list' => 'required|file|mimes:csv,xlsx'
        ]);

        Excel::import(new ExportersImport, request()->file('exporters_list')->store('temp'));

        $request->session()->flash('message', "Successfully imported exporters");
        $request->session()->flash('back', 'exporters.index');
        return view('dashboard.shared.universal-info');
    }

    public function available_time_slots(Request $request)
    {
        $date = $request->input('date');
        $exporter_id = $request->input('exporter_id');
        $booked_slots = Meeting::where('exporter_id', $exporter_id)->where('date', $date)->pluck('time')->all();
           $all_time_slots = [ 
             '09:00 - 09:25',
             '09:30 - 09:55',
             '10:00 - 10:25',
             '10:30 - 10:55',
             '11:00 - 11:25',
             '11:30 - 11:55',
             '12:00 - 12:25',
             '12:30 - 12:55',
             '13:00 - 13:25',
             '13:30 - 13:55'];
        $available_time_slots = array_diff($all_time_slots,$booked_slots);
        echo json_encode($available_time_slots);
    }

    public function exporters_sector(Request $request)
    {
        $sector = $request->input('sector');
        if (empty($sector)) {
            $exporters = Exporter::orderBy('company_name')->pluck('id', 'company_name')->all();
        } else {
            $exporters = Exporter::where('industry', $sector)->orderBy('company_name')->pluck('id', 'company_name')->all();
        }

        echo json_encode($exporters);
    }

    public function exporters_visits(Request $request)
    {
         $exporter = Exporter::find($request->exporter);
        $exporter->visits = $request->visits;
        $exporter->leads = $request->leads;
        $exporter->orders = $request->orders;
        $exporter->amount = $request->amount;
        $exporter->portal_feedback = $request->portal_feedback;
        $exporter->companies = $request->companies;
        $exporter->countries = $request->countries;
        $exporter->enquiries = $request->enquiries;
        $exporter->orders_booked = $request->orders_booked;
        $exporter->his_feedback = $request->his_feedback;
        $exporter->save();
        // $exporter = Exporter::find($request->exporter);

        // if ($request->date == "2021-11-24") {
        //     $exporter->d1_visits = $request->visits;
        //     $exporter->d1_leads = $request->leads;
        //     $exporter->d1_amount = $request->amount;
        //     $exporter->d1_feedback = $request->feedback;
        // }else if ($request->date == "2021-11-25") {
        //     $exporter->d2_visits = $request->visits;
        //     $exporter->d2_leads = $request->leads;
        //     $exporter->d2_amount = $request->amount;
        //     $exporter->d2_feedback = $request->feedback;
        // }
        // $exporter->save();
        $request->session()->flash('message', 'Feedback has been submitted.');
        return redirect()->route('visits.index');
    }

    public function get_visits(Request $request)
    {
        $exporter = Exporter::where('id' , $request->exporter)->first();
        echo json_encode($exporter);
    }

}
