<?php

namespace App\Http\Controllers;

use App\Models\BusinessOrIndustry;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;

class BusinessOrIndustriesController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $menuId = $request->query('menu_id');
        $user = Auth::user();
        $userId = $user->id;

        // Ambil role_id yang dimiliki user
        $roleIds = DB::table('user_role')
            ->where('user_id', $userId)
            ->pluck('role_id');

        // Ambil data akses berdasarkan role_id dan menu_id
        $roleAccess = DB::table('role_menu')
            ->whereIn('role_id', $roleIds)
            ->where('menu_id', $menuId)
            ->first();

        // Pastikan roleAccess tidak null sebelum mengakses properti
        $creator = $roleAccess->allow_create ?? 0;
        $updater = $roleAccess->allow_update ?? 0;
        $destroyer = $roleAccess->allow_destroy ?? 0;
        return view('business-industries.index', compact('creator', 'updater', 'destroyer'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view("business-industries.create");
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $data = BusinessOrIndustry::findOrFail($id);
        return view('business-industries.edit', compact('data'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
