<?php
namespace App\Controller\Admin01;

/**
 * PlanAddons Controller
 *
 * @property \App\Model\Table\PlanAddonsTable $PlanAddons
 *
 * @method \App\Model\Entity\PlanAddon[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class PlanAddonsController extends AppController
{

    /**
     * Index method
     *
     * @return \Cake\Http\Response|void
     */
    public function index()
    {
        $this->paginate = [
            'contain' => ['Plans', 'Addons']
        ];
        $planAddons = $this->paginate($this->PlanAddons);

        $this->set(compact('planAddons'));
    }

    /**
     * View method
     *
     * @param string|null $id Plan Addon id.
     * @return \Cake\Http\Response|void
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $planAddon = $this->PlanAddons->get($id, [
            'contain' => ['Plans', 'Addons', 'SaleAddons']
        ]);

        $this->set('planAddon', $planAddon);
    }

    /**
     * Add method
     *
     * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $planAddon = $this->PlanAddons->newEntity();
        if ($this->request->is('post')) {
            $planAddon = $this->PlanAddons->patchEntity($planAddon, $this->request->getData());
            if ($this->PlanAddons->save($planAddon)) {
                $this->Flash->admin01_success(__('The plan addon has been saved.'));
            }else{
                $this->Flash->admin01_error(__('The plan addon could not be saved. Please, try again.'));
            }
        }
        return $this->redirect($this->referer());
    }

    /**
     * Edit method
     *
     * @param string|null $id Plan Addon id.
     * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $planAddon = $this->PlanAddons->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $planAddon = $this->PlanAddons->patchEntity($planAddon, $this->request->getData());
            if ($this->PlanAddons->save($planAddon)) {
                $this->Flash->admin01_success(__('The plan addon has been saved.'));

            }else{
                $this->Flash->admin01_error(__('The plan addon could not be saved. Please, try again.'));
            }
            
        }
        return $this->redirect($this->referer());
    }

    /**
     * Delete method
     *
     * @param string|null $id Plan Addon id.
     * @return \Cake\Http\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $planAddon = $this->PlanAddons->get($id);
        if ($this->PlanAddons->delete($planAddon)) {
            $this->Flash->admin01_success(__('The plan addon has been deleted.'));
        } else {
            $this->Flash->admin01_error(__('The plan addon could not be deleted. Please, try again.'));
        }

        return $this->redirect($this->referer());
    }
}
