<?php
namespace App\Controller;

use App\Controller\AppsController;

/**
 * SoftwareVersions Controller
 *
 * @property \App\Model\Table\SoftwareVersionsTable $SoftwareVersions
 *
 * @method \App\Model\Entity\SoftwareVersion[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class SoftwareVersionsController extends AppsController
{
    public function initialize() {
        parent::initialize();
        
        
        $user_permissions = $this->request->session()->read('user_permissions');
        $this->set('module_pages', $this->getPages());
        $this->set('bulk_actions', [BULK_ACTION_DELETE => "Move to trash"]);
        $this->set('current_module', $this->getModules());
        $this->set('sub_modules', $this->getSubModules());
        $this->set('page_list', $this->getPages());
        $this->set('user_permission', $user_permissions);
        $this->viewBuilder()->layout("custom_layout");
        $this->loadModel('SoftwareVersions');
    }
    /**
     * Index method
     *
     * @return \Cake\Http\Response|null
     */
    public function index()
    {
        $page_limit = 10;
        $search = [];
        $request_params = $this->request->query;
        if(!empty($request_params)){
            $search = $request_params;
            $page_limit = $search['page_limit'];
        }
       
         $this->paginate = [
                'limit' => $page_limit
                ];
        $softwareVersions = $this->paginate($this->SoftwareVersions->getAllSoftwareVersions($search));
       
        $this->set(compact('softwareVersions','search'));
    }

    /**
     * View method
     *
     * @param string|null $id Software Version id.
     * @return \Cake\Http\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $softwareVersion = $this->SoftwareVersions->get($id, [
            'contain' => []
        ]);

        $id_disable = true;
        $this->set(compact('softwareVersion','id_disable'));
    }

    /**
     * Add method
     *
     * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        if ($this->request->is('post')) {
            $request = $this->request->getData();
            //$request['release_date'] = date('Y-m-d H:i:s', strtotime($request['release_date']));
            if ($this->SoftwareVersions->insertAgreementFormat($request)) {
                $this->Flash->success(__('The software version has been saved.'));

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

    /**
     * Edit method
     *
     * @param string|null $id Software Version id.
     * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $softwareVersion = $this->SoftwareVersions->get($id, [
            'contain' => []
        ]);
        
        if ($this->request->is(['patch', 'post', 'put'])) {
            $request = $this->request->getData();
            if(isset($request['is_new']) && $request['is_new'] == 1){
                $request['is_new'] = 1;
            }else{
                $request['is_new'] = 2;
            }
            
            
            if ($this->SoftwareVersions->updateSoftwareVersion($id,$request)) {
                $this->Flash->success(__('The software version has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The software version could not be saved. Please, try again.'));
        }
        $id_disable = false;
        $this->set(compact('softwareVersion','id_disable'));
    }

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

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