<?php
namespace App\Controller\Admin01;

use Cake\ORM\TableRegistry;
/**
 * Plans Controller
 *
 * @property \App\Model\Table\PlansTable $Plans
 *
 * @method \App\Model\Entity\Plan[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class PlansController extends AppController
{

    /**
     * Index method
     *
     * @return \Cake\Http\Response|void
     */
    public function index()
    {
        $plans = $this->Plans->find();

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

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

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

    /**
     * Add method
     *
     * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $plan = $this->Plans->newEntity();
        if ($this->request->is('post')) {
            $plan = $this->Plans->patchEntity($plan, $this->request->getData());
            if ($this->Plans->save($plan)) {
                $this->Flash->admin01_success(__('The plan has been saved.'));

                return $this->redirect(['action'=>'index']);
            }
            $this->Flash->admin01_error(__('The plan could not be saved. Please, try again.'));
        }
        $this->set(compact('plan'));
    }

    /**
     * Edit method
     *
     * @param string|null $id Plan 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)
    {
        $plan = $this->Plans->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $plan = $this->Plans->patchEntity($plan, $this->request->getData());
            if ($this->Plans->save($plan)) {
                $this->Flash->admin01_success(__('The plan has been saved.'));

                return $this->redirect($this->referer());
            }
            $this->Flash->admin01_error(__('The plan could not be saved. Please, try again.'));
        }
        $this->set(compact('plan'));
    }
    
    public function addons($id = null)
    {
        $plan = $this->Plans->get($id, [
            'contain' => ['PlanAddons'=>['Addons']]
        ]);
        
        $addons = TableRegistry::get('Addons')->find('list');
        $planAddon = TableRegistry::get('PlanAddons')->newEntity();
        
        $this->set(compact('plan','addons','planAddon'));
    }
    
    public function features($id = null)
    {
        $plan = $this->Plans->get($id, [
            'contain' => ['PlanFeatures'=>['Features']]
        ]);
        
        $features = TableRegistry::get('Features')->find('list');
        $planFeature = TableRegistry::get('PlanFeatures')->newEntity();
        
        $this->set(compact('plan','features','planFeature'));
    }

    /**
     * Delete method
     *
     * @param string|null $id Plan 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']);
        $plan = $this->Plans->get($id);
        if ($this->Plans->delete($plan)) {
            $this->Flash->success(__('The plan has been deleted.'));
        } else {
            $this->Flash->error(__('The plan could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }
}
