<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    function __construct()
    {
        $this->middleware('role_or_permission:product access|product create|product edit|product delete', ['only' => ['index', 'show']]);
        $this->middleware('role_or_permission:product create', ['only' => ['create', 'store']]);
        $this->middleware('role_or_permission:product edit', ['only' => ['edit', 'update']]);
        $this->middleware('role_or_permission:product delete', ['only' => ['destroy']]);
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $datas = Product::latest()->get();
        return view('admin.product.index', compact('datas'));
    }

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

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

        $data['image'] = fileupload($request->file('image'), 'productimage');

        Product::create($data);

        $notification = array(
            'messege' => 'Created successfully',
            'alert-type' => 'success'
        );

        return back()->with($notification);
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Product::findOrFail($id);
        return view('admin.product.edit', compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $product = Product::findOrFail($id);
        $product->name = $request->name;
        if ($request->hasFile('image')) {
            $product->image = fileupload($request->file('image'), 'productimage');
        }
        $product->price = $request->price;
        $product->long_description = $request->long_description;
        $product->sort_description = $request->sort_description;
        $product->save();

        $notification = array(
            'messege' => 'Updated successfully',
            'alert-type' => 'success'
        );

        return back()->with($notification);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $product = Product::findOrFail($id);
        $product->delete();

        $notification = array(
            'messege' => 'Deleted successfully',
            'alert-type' => 'success'
        );

        return back()->with($notification);
    }
}
