<?php

namespace App\Http\Controllers;

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

class CustomersController extends Controller
{
    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('customers.index', compact('creator', 'updater', 'destroyer'));
    }
    public function create()
    {
        return view('customers.create');
    }
    public function edit(string $id)
    {
        $data = Customer::findOrFail($id);
        return view('customers.edit', compact('data'));
    }
    public function upload(){
        return view('customers.upload');
    }
}
