<?php namespace App\Controllers;

use App\Controllers\BaseController;
use App\Libraries\Make_bread;
use App\Models\ClientsModel;
use App\Models\EmpresesModel;
use http\Url;

class Empreses extends BaseController
{

    /*
     * Clients
     */
	public function index()
	{
        $session = session();
        if(!session()->get('isLoggedIn')){
            return  redirect()->to(base_url().'/auth');
        }
        //$this->init();
        // first load the library breadcrumb
        $make_bread = new Make_bread;
        $data=[
            'heading' =>'Empreses',
        ];
        $make_bread->add('Empreses', 'empreses', TRUE);
        $breadcrumb = $make_bread->output();
        $data['breadcrumb'] =$breadcrumb;

        $db =  db_connect();
        $clients = new  ClientsModel($db);
        $currentEmpresaId = session()->get('empresa_id_user');
        $data['clients'] = $clients->getAll($currentEmpresaId);
        $empreses = new  EmpresesModel($db);
        $currentUserId = session()->get('id');
        $data['empreses'] = $empreses->getAllAdmin();
        if($currentEmpresaId != 1)
            $data['empreses'] = $empreses->getAll($currentUserId, $currentEmpresaId);
        
		return view('admin/app/empreses', $data);
	}

}