<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Innovativecomoanies;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class InnovativecomoanieController extends Controller
{
    function __construct()
    {
        $this->middleware('role_or_permission:home company access|home company create|home company delete', ['only' => ['index']]);
        $this->middleware('role_or_permission:home company create', ['only' => ['create','store']]);
        $this->middleware('role_or_permission:home company delete', ['only' => ['delete']]);
    }



    function index()
    {
        $datas =  Innovativecomoanies::all();
        return view('admin.home.innovativecomoanie.innovativecomoanie', compact('datas'));
    }

    function store(Request $request)
    {
        $request->validate([
            'image' => ['required']
        ]);
        $filename = fileupload($request->file('image'), 'homepage-files');

        Innovativecomoanies::create([
            'file_name' => $filename
        ]);

        $notification = array(
            'messege' => 'Create successfully',
            'alert-type' => 'success'
        );
        return back()->with($notification);
    }

    function create()
    {
        return view('admin.home.innovativecomoanie.create');
    }
    function delete($id)
    {
        $data = Innovativecomoanies::findOrFail($id);
        if (File::exists($data->file_name)) {
            File::delete($data->file_name);
        }

        $data->delete();

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