<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\SessionModel;
use App\Models\UserModel;


class Sessions extends BaseController {

	private $model, $usermodel, $custom_config;

	function __construct() {
		parent::__construct();
		$this->model = new SessionModel();
		$this->usermodel = new UserModel();
		$this->custom_config = new \Config\Custom();
	}

	public function add_form($id = null) {
		helper(['form']);
		$this->data['id'] = $id;
		$this->data['title'] = "Create Event";
		if($id) {
		    $this->data['title'] = "Edit Event";
			$this->data['data'] = $this->model->find($id);
			$this->data['data']['agents'] = explode(",", $this->data['data']['agents']);
			$this->data['data']['admins'] = explode(",", $this->data['data']['admins']);
			$this->data['data']['checkers'] = explode(",", $this->data['data']['checkers']);
			$this->data['data']['functions'] = explode(",", $this->data['data']['functions']);
		}
		$this->data['admins'] = $this->usermodel->where('role', 1)->get()->getResultArray();
		$this->data['agents'] = $this->usermodel->where('role', 2)->get()->getResultArray();
		$this->data['checkers'] = $this->usermodel->where('role', 3)->get()->getResultArray();
		
		$this->data['template'] = "session/add";
		$this->data['event_functions'] = $this->custom_config->event_functions;
		$this->data['css_file'] = "add-session.css";
		echo view('layout', $this->data);
	}

	public function save() {
		 //include helper form
        helper(['form']);
        $data = [
            'title'     => $this->request->getVar('title'),
            'venue'     => $this->request->getVar('venue'),
            'description'    => $this->request->getVar('description'),
            'start'    => $this->request->getVar('start'),
            'end'    => $this->request->getVar('end'),
            'functions' => implode(",", $this->request->getVar('functions'))
        ];
        $data['admins'] = $this->request->getVar('admins');
        $data['agents'] = $this->request->getVar('agents');
        $data['checkers'] = $this->request->getVar('checkers');
        if(is_array($data['admins']))
        	$data['admins'] = implode(",", $data['admins']);
        	
        if(is_array($data['agents']))
        	$data['agents'] = implode(",", $data['agents']);
        	
        if(is_array($data['checkers']))
        	$data['checkers'] = implode(",", $data['checkers']);
        
        $id = $this->request->getVar('id');
        $data['admin_id'] = session('id');

        if(isset($id))
        	$this->model->update($id, $data);
      	else
      		$this->model->save($data);

      	$this->session->setFlashdata('msg', 'Event added/updated!');
        return redirect()->to('session/list');
	}	

	public function remove($session_id) {
		$this->model->delete($session_id);
		$this->seatingmodel->where('session_id', $session_id)->delete();
        $this->stagemodel->where('session_id', $session_id)->delete();
        return redirect()->to('session/list');
	}
	public function list() {

		$this->data['title'] = "Events";
		$this->data['template'] = "session/list";
		$this->data['css_file'] = "session.css";

		$this->data['user_id'] = session('id');
		$list = array();
		$sessions = $this->model->get()->getResultArray();
		foreach($sessions as $session):
		    $session['admins'] = explode(",", $session['admins']);
		    $session['agents'] = explode(",", $session['agents']);
		    $session['checkers'] = explode(",", $session['checkers']);
		    
            if($this->data['is_superadmin']):
                $list[] = $session;
		    elseif($this->data['is_admin']):
		        if( in_array($this->data['user_id'], $session['admins']) ):   $list[] = $session; endif;
		    elseif($this->data['is_agent']):
		        if( in_array($this->data['user_id'], $session['agents']) ):   $list[] = $session; endif;
		    elseif($this->data['is_checker']):
		        if( in_array($this->data['user_id'], $session['checkers']) ): $list[] = $session; endif;
		    endif;
		endforeach;
		
		if($this->data['is_checker'] & count($list) == 1) {
		    return redirect()->to('session/checkin/spot/' . $list[0]['id']);
		    exit;
		}
		    
		$this->data['list'] = $list;
        echo view('layout', $this->data);
	}
}