<?php
namespace App\Controller;

use App\Utility\Common;
use Cake\I18n\I18n;
use Cake\Cache\Cache;
use Cake\I18n\FrozenTime;

/**
 * Student Controller
 *
 * @property \App\Model\Table\SessionsTable $Sessions
 * @property \App\Model\Table\StudentSessionsTable $StudentSessions
 */
class SessionController extends AppsController
{

    private  $controller = "session";
    public function initialize(): void
    {
        parent::initialize();
        $this->loadModel('Sessions');
        $this->set('controller', $this->controller);
        $this->setPermissions();
    }

    public function index()
    {
        $page_limit = 10;
        if (isset($this->request->query['page_limit'])) {
            $page_limit = $this->request->query('page_limit');
        }

        $this->paginate = [
            'limit' => $page_limit,
            'order' => [
                'id' => 'asc'
            ]
        ];
//        $condition = ['status' => 1];
        $search = '';
        if (isset($this->request->query['search'])) {
            $search = $this->request->query('search');
            $condition = '(name LIKE ' . "'%" . $search ."%' OR duration_month like '%" . $search.  "%' " .')'   ;
        }
        $sessions = $this->Sessions->find('all')->where($condition);


        $this->set('sessions', $this->paginate($sessions));
        $this->set('searchText', $search);
        $nav_arr[0] = array('action' => 'add', 'page_id' => '10112', 'icon' => '<i class="fa fa-plus-circle"></i>', 'label' => 'Add');
        $this->set('nav_arr', $nav_arr);
        $this->set('page_limit', $page_limit);
    }

    public function add()
    {
        $user_id = $this->Auth->user('id');

        if ($this->request->is('post')) {

            $Sessions = $this->Sessions->newEntity();
            $request = $this->request->getData();
            $request['status'] = 1;
            $request['created_at'] = FrozenTime::now();
            $request['updated_at'] = FrozenTime::now();
            $request['start_date'] = new FrozenTime($request['start_date']);
            $request['end_date'] =  $request['start_date']->add(new \DateInterval('P' . $request['duration_month'] . 'M'));
            $request['created_by'] = $user_id;
            $request['updated_by'] = $user_id;

            $Sessions = $this->Sessions->patchEntity($Sessions, $request);

            $errors = $Sessions->errors();

            if (empty($errors)) {
                if ($this->Sessions->save($Sessions)) {

                    $this->Flash->success(__('The session has been saved.'));
                    return $this->redirect(['action' => 'add']);
                }
            }
            $this->Flash->error(__('Unable to add the session.'));
            $this->set('errors', $errors);
        }
        $this->setNextPage();
    }

    public function edit($id=null)
    {
        $session = $this->Sessions->get($id, [
            'contain' => []
        ]);

        if ($this->request->is(['patch', 'post', 'put'])) {

            $user_id = $this->Auth->user('id');

            $request = $this->request->data;
            $request['start_date'] = new FrozenTime($request['start_date']);
            $request['end_date'] = $request['start_date']->add(new \DateInterval('P' . $request['duration_month'] . 'M'));

            $session = $this->Sessions->patchEntity($session, $request);

            $errors = $session->errors();

            $session['updated_at'] = FrozenTime::now();
            $session['updated_by'] = $user_id;

            if (empty($errors)) {
                if ($this->Sessions->save($session)) {
                    $this->Flash->success(__('The Session has been updated.'));
                    return $this->redirect(['action' => 'index']);
                } else {
                    $this->Flash->error(__('The Session could not be saved. Please, try again.'));
                }
            }

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


        $this->set('session', $session);
        $this->set('_serialize', ['session']);
        $this->setNextPage();
    }

    public function view($id=null)
    {
        $session = $this->Sessions->get($id, [
            'contain' => []
        ]);

        $this->set('session', $session);
        $this->set('_serialize', ['session']);
        $this->setNextPage();
    }

    public function delete($id=null)
    {
        $this->loadModel('StudentSessions');

        $session = $this->Sessions->get($id);

        $studentSessionCount = $this->StudentSessions->find()
            ->where(['session_id' => $id])
            ->count();

        if ($studentSessionCount > 0) {
            $this->Flash->error(__('Cannot delete session because it has associated student sessions.'));
        } else {
            if ($this->Sessions->delete($session)) {
                $this->Flash->success(__('The session has been deleted.'));
            } else {
                $this->Flash->error(__('The session could not be deleted. Please try again.'));
            }
        }

        return $this->redirect(['action' => 'index']);
    }
    private function setNextPage(){
        $nav_arr[0] = array('action' => 'index', 'page_id' => '10109', 'icon' => '<i class="fa fa-list"></i>', 'label' => 'List');
        $this->set('nav_arr', $nav_arr);
    }

}
