<?php

namespace App\Http\Controllers;

use App\Address;
use App\Store;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Countries;
use Auth;
class StoreController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $stores = Store::all();
        return view('store.index', compact('stores'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $countries = Countries::all()->pluck('name.common');
        $states = Countries::where('name.common', 'Nigeria')
            ->first()
            ->states
            ->sortBy('name')
            ->pluck('name');

        return view('store.create', compact('countries', 'states'));

    }

    public function getStates(Request $request)
    {
        $country = $request->country;
        $states = Countries::where('name.common', $country)
            ->first()
            ->states
            ->sortBy('name')
            ->pluck('name');

        return view('includes.states', compact('states'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $address = new Address([
            'line_1'=> $request->address1,
            'line_2'=> $request->address2,
            'city'=> $request->city,
            'country'=> $request->address2,
            'state'=> $request->state,
            'phone'=> $request->phone,
            'postal_code'=> $request->postal,
            'closest_land_mark'=> $request->landmark,
        ]);

        $address->save();



        $store = new Store([
           'name'=> $request->name,
            'country'=> $request->country,
           'state'=> $request->state,
           'address_id'=> $address->id,
           'created_by'=> Auth::user()->id,
            'updated_by'=> Auth::user()->id
        ]);

        $store->save();



        return redirect()->back()->with('success', 'Store '.$store->name.' Created Successfully');

    }

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

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

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

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