<?php

namespace App\Http\Controllers;

use App\Models\Visit;
use App\Models\Exporter;
use Illuminate\Http\Request;

class VisitController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $visits = Visit::all();
        $exporters = Exporter::all()->sortBy('company_name');
        $industries = Exporter::select('industry')->distinct()->orderBy('industry')->get()->toArray();
        $industries = array_column($industries, 'industry');

        return view('dashboard.visits.visit_form', array(
            'visits' => $visits,
            'exporters' => $exporters,
            'industries' => $industries,
        ));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(array $data)
    {
        //
        $visits =  Visit::create([
            'sector' => $data['sector'],
            'company' => $data['company'],
            'no_of_visits' => $data['no_of_visits'],
            'no_of_leads' => $data['no_of_leads'],
        ]);
        return $visits;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        //$visit = $request->only('sector','company','no_of_visits','no_of_leads');
        //$visit['visit'] = json_encode($visit);
        //Visits::insert($visit);
        // return Redirect::to("laravel-json")->withSuccess('Great! Successfully store data in json format in datbase');
        $visit = new Visit();
        $visit->sector = $request->input('sector');
        $visit->company = $request->input('company');
        $visit->no_of_visits = $request->input('no_of_visits');
        $visit->no_of_leads = $request->input('no_of_leads');
        $visit->save();
        $request->session()->flash('message', 'Successfully created visit');
        return redirect()->route('visits.visit_form');
    }

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

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Visit  $visit
     * @return \Illuminate\Http\Response
     */
    public function edit(Visit $visit)
    {
        //
        return view('dashboard.visits.edit', array('visit' => $visit));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Visit  $visit
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Visit $visit)
    {
        //
        $validated = $request->validate([
            'sector' => 'required',
            'company' => 'required',
            'no_of_visits' => 'required',
            'no_of_leads' => 'required'
        ]);

        $visit->save();

        $request->session()->flash('message', 'Successfully updated visit');
        return redirect()->route('visits.edit', $visit->id);
    }

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