<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Yajra\DataTables\DataTables;

class PersonalAccountController extends Controller
{

    public function index(Request $request)
    {
        if ($request->ajax()) {
            $data = User::query()
                ->selectRaw('id, standing_id, first_name, last_name, email, account_status')
                ->where('user_type', 'personal'); // Select necessary columns

            return Datatables::of($data)
                ->addIndexColumn()
                ->addColumn('action', function ($row) {
                    // Pass the user ID to the 'admin.users.show' route
                    $btn = '<a href="' . route('admin.users.personal.show', $row->id) . '" class="edit btn btn-primary btn-sm">View</a>';
                    return $btn;
                })
                ->rawColumns(['action'])
                ->make(true);
        }

        return view('admin.personal-accounts.index');
    }



    public function show($id)
    {
        $user = User::with([
            'vehicles',
            'transactions',
            'trading_years',
            'profile_setting',
            // If you have an id documents relationship, you can include it here as well
        ])->findOrFail($id);

        // Return to the detail view with the user and their related data
        return view('admin.personal-accounts.show', compact('user'));
    }
}
