<?php

namespace App\Http\Controllers;
use App\Models\Apple;
use Illuminate\Http\Request;

class AppleController extends Controller
{
  /**
   * Display a listing of the resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function index()
  {
    $apples = Apple::orderBy('id', 'desc')->paginate(10);
    return view('apples.index', compact('apples'));
  }

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

  /**
   * Store a newly created resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return \Illuminate\Http\Response
   */
  public function store(Request $request)
  {
    $request->validate([
      'name' => 'required',
      'email' => 'required',
      'address' => 'required',
    ]);

    Apple::create($request->post());

    return redirect()
      ->route('apples.index')
      ->with('success', 'Apple has been created successfully.');
  }

  /**
   * Display the specified resource.
   *
   * @param  \App\Apple  $Apple
   * @return \Illuminate\Http\Response
   */
  public function show(Apple $apple)
  {
    return view('apples.show', compact('apple'));
  }

  /**
   * Show the form for editing the specified resource.
   *
   * @param  \App\Apple  $Apple
   * @return \Illuminate\Http\Response
   */
  public function edit(Apple $apple)
  {
    return view('apples.edit', compact('apples'));
  }

  /**
   * Update the specified resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \App\Apple  $Apple
   * @return \Illuminate\Http\Response
   */
  public function update(Request $request, Apple $Apple)
  {
    $request->validate([
      'name' => 'required',
      'email' => 'required',
      'address' => 'required',
    ]);

    $Apple->fill($request->post())->save();

    return redirect()
      ->route('apples.index')
      ->with('success', 'Apple Has Been updated successfully');
  }

  /**
   * Remove the specified resource from storage.
   *
   * @param  \App\Apple  $Apple
   * @return \Illuminate\Http\Response
   */
  public function destroy(Apple $Apple)
  {
    $Apple->delete();
    return redirect()
      ->route('apples.index')
      ->with('success', 'Apple has been deleted successfully');
  }
}
