<?php
namespace App\Controller;

use App\Common\Permission;
use App\Controller\AppsController;

/**
 * Education Controller
 *
 *
 * @method \App\Model\Entity\Education[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class EducationController extends AppsController
{
    
    public function initialize() {
        parent::initialize();

        if (Permission::isEmployeeAsAdmin()) {
            $this->Auth->allow(['index', 'add', 'edit', 'delete']);
        }

        $this->checkUser(); // is RTG User or not

        $this->setUserType();
        if($this->setClientControllerAccess()){
            $this->Auth->allow(['geteducationcontent']);
        }
    }
    /**
     * Index method
     *
     * @return \Cake\Http\Response|null
     */
    public function index()
    {        
        $this->loadModel('MemberEducations');
        $member_id = $this->Auth->user('id');
        if($this->request->is('post')){
            $request =  $this->request->getData();
            $request['member_id'] = $member_id;
            $result = $this->MemberEducations->saveEducationContent($request);
            if(!empty($result)){               

                //add_log
                $logData['action'] = "EDUCATIONSETUP_ADD_EDIT";
                $logData['status'] = "success";
                $logData['input_data'] = $this->request->getData();
                $logData['input_data']["id"]=$result->id;
                $this->appLog($logData);

                $this->Flash->success(__('The eduaction content has been saved successfully.'));
                $this->redirect($this->referer());
            }
        }
        
        $html = $this->MemberEducations->getEducationTempleateByMemberId($member_id);
        $content['html'] = $html;
        
        $this->set(compact('content'));
    }

    
    public function geteducationcontent(){
        $this->loadModel('MemberEducations');
        $member_id = $this->Auth->user('id');
        $html = $this->MemberEducations->getEducationTempleateByMemberId($member_id);
        $content['html'] = $html;
        $this->set(compact('content'));
    }
    /**
     * View method
     *
     * @param string|null $id Education id.
     * @return \Cake\Http\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $education = $this->Education->get($id, [
            'contain' => []
        ]);

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

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

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

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

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

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

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