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

namespace App\Controller;

use App\Controller\AppController;

class TurnkeyController extends AppController
{

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

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

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

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

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

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

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

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

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