<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\GuestModel;
use App\Models\GroupModel;


class Groups extends BaseController {

	private $model, $guestmodel, $checkinmodel;

	function __construct() {
		parent::__construct();
		$this->model = new GroupModel();
		$this->guestmodel = new GuestModel();
	}

	public function add_form($id = null) {
		helper(['form']);
		$this->data['id'] = $id;
		if($id) {
			$this->data['data'] = $this->model->find($id);
		}
		$this->data['title'] = "Create Group";
		$this->data['template'] = "group/add";
		$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'),
            'color'     => $this->request->getVar('color'),
            
        ];
        $nGuests = $this->request->getVar('nGuests');
        if($nGuests)
        	$data['nGuests'] = $nGuests;
        
        
        $id = $this->request->getVar('id');
        if($id != null)
        	$this->model->update($id, $data);
      	else
      		$this->model->save($data);

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

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

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

		$this->data['id'] = session('id');
		
		$this->data['list'] = $this->model->get()->getResultArray();
        echo view('layout', $this->data);
	}
}