<?php

namespace App\Controller;

use Cake\I18n\FrozenTime;

/**
 * Agent Controller
 *
 * @property \App\Model\Table\AgentsTable $Agents
 */
class AgentController extends AppsController
{

    private  $controller = "agent";
    public function initialize(): void
    {
        parent::initialize();
        $this->loadModel('Agents');
        $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'
            ]
        ];

        $search = '';
        if (isset($this->request->query['search'])) {
            $search = $this->request->query('search');
            $condition = '(first_name LIKE ' . "'%" . $search  . "%'" . 'OR last_name LIKE ' . "'%" . $search  .
                "%'".'OR email LIKE ' . "'%" . $search  . "%'".'OR phone_number LIKE ' . "'%" . $search  . "%'".')' ;
        }
        $agents = $this->Agents->find('all')->where($condition);


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

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

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

            $agent = $this->Agents->newEntity();
            $request = $this->request->getData();
            $request['status'] = 1;
            $request['created_at'] = FrozenTime::now();
            $request['updated_at'] = FrozenTime::now();
            $agent = $this->Agents->patchEntity($agent, $request);
            $errors = $agent->errors();
            if (empty($errors)) {
                if ($this->Agents->save($agent)) {

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

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

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

            $user_id = $this->Auth->user('id');
            $request = $this->request->data;
            $agent = $this->Agents->patchEntity($agent, $request);
            $errors = $agent->errors();
            $agent['updated_at'] = FrozenTime::now();

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

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

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

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

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

    public function delete($id = null)
    {

        $agent = $this->Agents->get($id);

        if ($this->Agents->delete($agent)) {
            $this->Flash->success(__('The agent has been deleted.'));
        } else {
            $this->Flash->error(__('The agent could not be deleted. Please try again.'));
        }

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

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

}
