<?php
namespace App\Http\Controllers\Admin;


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

class PlatformController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    function __construct()
    {
        $this->middleware('role_or_permission:Plat Form access|Plat Form create|Plat Form edit|Plat Form delete', ['only' => ['index', 'show']]);
        $this->middleware('role_or_permission:Plat Form create', ['only' => ['create', 'store']]);
        $this->middleware('role_or_permission:Plat Form edit', ['only' => ['edit', 'update']]);
        $this->middleware('role_or_permission:Plat Form delete', ['only' => ['destroy']]);
    }

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = new Platform;
        $request->validate([
            'image' => 'required|image|mimes:png,jpg,gif,jpeg,webp',
        ]);
        $data->status = $request->status;
        if ($request->hasFile('image')) {
            $image_path = $data->image;
            if (file_exists($image_path)) {
                unlink($image_path);
            }
            $logo = $request->file('image');
            $filename = time() . '.' . $logo->getClientOriginalExtension();
            $logo->storeAs('public/platform', $filename);
            $data->image = 'storage/platform/' . $filename;
        }
        $data->save();
        $notification = array(
            'messege' => 'Plat Form Created Successfully',
            'alert-type' => 'success'
        );
        return Redirect()->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)
    {
        //
    }

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

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