<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\UserModel;
use App\Models\GuestModel;
use App\Models\CheckinModel;

class User extends BaseController {

    private $model;
    private $guest_model;

    function __construct() {
        parent::__construct();
        $this->model = new UserModel();
        $this->guest_model = new GuestModel();
        $this->checkinmodel = new CheckinModel();
    }

	public function index(){
        if( session()->get('logged_in'))
            return redirect()->to('/user/dashboard'); 
        else
            return redirect()->to('/user/login');
    }

    public function add_form($id = null) {
        helper(['form']);
        $this->data['id'] = $id;
        if($id) {
            $this->data['data'] = $this->model->find($id);
        }

        echo view('user/add', $this->data);
    }


    public function list() {
        helper(['form']);
        if($this->data['is_admin'])
            $this->model->where("role", 2);
        $this->data['users_list'] = $this->model->get()->getResultArray();

        echo view('user/list', $this->data);
    }

	public function dashboard() {
        $this->data['n_invited'] = $this->checkinmodel->get()->getNumRows();
        $this->data['n_accepted'] = $this->checkinmodel->where('status', 1)->get()->getNumRows();
        $this->data['n_declined'] = $this->checkinmodel->where('status', 2)->get()->getNumRows();
        $this->data['n_checkedin'] = $this->checkinmodel->where('checked_in', 1)->get()->getNumRows();
		echo view('user/dashboard', $this->data);
	}

    public function save() {
         //include helper form
        helper(['form']);
        $data = [
            'username'     => $this->request->getVar('username'),
            'password'    => md5($this->request->getVar('password')),
            'role'     => (int)$this->request->getVar('role'),
        ];

        if($this->data['is_admin'] && $data['role'] == 1) {
            $this->session->setFlashdata('error', 'You are not priviliged to add an admin!');
        } else {
            $this->model->save($data);    
            $this->session->setFlashdata('msg', 'User added!');
        }
        return redirect()->to('/user/list');
    }

	public function login_form() {
		helper(['form']);
		echo view('login');
	}

	public function login_auth() {

		$session = session();
        $model = new UserModel();
        $username = $this->request->getVar('username');
        $password = $this->request->getVar('password');
        $data = $model->where('username', $username)->first();
        if($data){
            $pass = $data['password'];
            $verify_pass = (md5($password) == $data['password']);
            if($verify_pass){
                $ses_data = [
                    'id'       => $data['id'],
                    'username'      => $data['username'],
                    'role'          => $data['role'],
                    'logged_in'     => TRUE
                ];
                $session->set($ses_data);
                return redirect()->to('/user/dashboard');
            }else{
                $session->setFlashdata('msg', 'Wrong password');
                return redirect()->to('/user/login');
            }
        }else{
            $session->setFlashdata('msg', 'Email not found');
            return redirect()->to('/user/login');
        }

	}

	public function logout() {
		$session = session();
		$session->destroy();
		return redirect()->to('/user/login');
	}

    public function remove($id) {
        $this->model->delete($id);
        $this->seatingmodel->where('guest_id', $id)->delete();
        $this->stagemodel->where('guest_id', $id)->delete();
        $this->session->setFlashdata('msg', 'The User removed!');
        return redirect()->to('/user/list');
    }

}