<?php
namespace App\Controller;

use App\Controller\AppsController;

/**
 * Logs Controller
 *
 * @property \App\Model\Table\LogsTable $Logs
 *
 * @method \App\Model\Entity\Log[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class LogsController extends AppsController
{
    /**
     * Index method
     *
     * @return \Cake\Http\Response|null
     */
    public function index()
    {
        if($this->request->is('ajax')){
            $request = $this->request->getData();
            $response = $this->ajaxRequest($request);
            echo json_encode($response);
            exit;
        }
        $page_limit = 10;
        $this->loadModel('Cmsusers');
        $this->loadModel('Logs');
        $search = [];
        $action_list = $this->Logs->ACCION_ARRAY;
        $status_list = ['1'=>'Success','2'=>'Failed'];
        $membersList = $this->Cmsusers->getAllMemberListAssocId();
        //pr($membersList);exit;
        $clientList = [];
        $search['from_date'] = date('Y-m-d',strtotime("-7 day"));
        $search['to_date'] = date('Y-m-d');
        if(!empty($this->request->query)){
            foreach($this->request->query as $key => $query){
                if(isset($query)){
                    $search[$key] = $query;
                }
            }
            
            if(!empty($search['page_limit'])){
                $page_limit = $search['page_limit'];
            }
            
            if($search['member_id'] > 0){
               $this->loadModel('Clients');
               $clientList = $this->Clients->getClientList($search['member_id'],"",false); 
            }
        }
        
        $this->paginate = [
            'limit' => $page_limit
        ];
        $logsData = $this->Logs->getAllLogs($search);
        if($logsData){
            $logs = $this->paginate($logsData);
        }
        $this->set(compact('clientList','membersList','action_list','logs','status_list','search','page_limit'));
    }

    
    private function ajaxRequest($request){
        $result = "";
         $this->loadModel('Clients');
        if($request['action'] == "GET_CLIENT_LIST_BY_MEMBER"){
            $clientList = $this->Clients->getClientList($request['member_id'],"",false);
            $html = "<option value=''>Please select</option>";
            if(!empty($clientList)){
                foreach($clientList as $client_id => $client){
                    $html .= "<option value ='".$client_id."'>".$client."</option>";
                }
                $result = $html;
            }
        }
        
        return $result;
    }

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

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

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

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The log could not be saved. Please, try again.'));
        }
        $members = $this->Logs->Members->find('list', ['limit' => 200]);
        $clients = $this->Logs->Clients->find('list', ['limit' => 200]);
        $this->set(compact('log', 'members', 'clients'));
    }

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

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The log could not be saved. Please, try again.'));
        }
        $members = $this->Logs->Members->find('list', ['limit' => 200]);
        $clients = $this->Logs->Clients->find('list', ['limit' => 200]);
        $this->set(compact('log', 'members', 'clients'));
    }

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

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