<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator;

class CustomTableController extends Controller
{
    public function index(Request $request)
    {
        $data = User::query();
        $search = $request->input('search');
        if ($search) {
            $data->where('name', 'like', "%$search%");
        }
        $sortField = $request->input('sort_field');
        $sortOrder = $request->input('sort_order', 'asc');
        if ($sortField) {
            $data->orderBy($sortField, $sortOrder);
        }
        $perPage = $request->input('per_page', 10);
        $paginatedData = $data->paginate($perPage);
        $columns = [
            ['title' => 'ID', 'dataIndex' => 'id'],
            ['title' => 'Name', 'dataIndex' => 'name'],
        ];
        $actions = [
            ['type' => 'edit', 'name' => 'Edit', 'path' => '/edit', 'access' => true],
            ['type' => 'delete', 'name' => 'Delete', 'path' => '#', 'access' => true],
        ];

        return view('custom-table', compact('paginatedData', 'columns', 'actions'));
    }

    public function destroy($id)
    {
        User::find($id)->delete();
        return redirect()->back()->with('success', 'User deleted');
    }
}