<?php
// src/Controller/ArticlesController.php

namespace App\Controller;

use App\Controller\AppController;

class VehicleController extends AppController
{

    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('Paginator');
        $this->loadComponent('Flash'); // Include the FlashComponent
    }

    public function index()
    {
        $vehicles = $this->Paginator->paginate($this->Vehicle->find());
        $this->set(compact('vehicles'));
    }

    public function view($id)
    {
        $vehicle = $this->Vehicle->findById($id)->firstOrFail();
        $this->set(compact('vehicle'));
    }

    public function add()
    {
        $vehicle = $this->Vehicle->newEntity();
        if ($this->request->is('post')) {
            $vehicle = $this->Vehicle->patchEntity($vehicle, $this->request->getData());

            // Hardcoding the user_id is temporary, and will be removed later
            // when we build authentication out.
            $vehicle->user_id = 1;

            if ($this->Vehicle->save($vehicle)) {
                $this->Flash->success(__(' vehicle has been saved.'));
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('Unable to add vehicle.'));
        }
       
        $this->set('vehicle', $vehicle);
    }
    // Add the following method.

public function edit($id)
{
    $vehicle = $this->Vehicle->findById($id)->firstOrFail();
    if ($this->request->is(['post', 'put'])) {
        $this->Vehicle->patchEntity($vehicle, $this->request->getData());
        if ($this->Vehicle->save($vehicle)) {
            $this->Flash->success(__('vehicle has been updated.'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('Unable to update vehicle.'));
    }

    $this->set('vehicle', $vehicle);
}
public function delete($id)
{
    $this->request->allowMethod(['post', 'delete']);
    $vehicle = $this->Vehicle->findById($id)->firstOrFail();
    if ($this->Vehicle->delete($vehicle)) {
        $this->Flash->success(__('The {0} has been deleted.', $vehicle->vendorname));
        return $this->redirect(['action' => 'index']);
    }
}
}
?>