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

namespace App\Controller;

use App\Controller\AppController;

class VendorController extends AppController
{
    var $helpers = array( 'Html' );
    public function initialize()
    {
        parent::initialize();

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

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

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

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

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

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

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

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