<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
use Google\Cloud\Firestore\FirestoreClient;

class Dashboard_model extends CI_Model
{
    public function getdashboard_info()
    {
        $data['tot_vehicles']      = $this->db->select('v_id')->from('vehicles')->get()->num_rows();
        $data['tot_drivers']       = $this->db->select('d_id')->from('drivers')->get()->num_rows();
        $data['tot_customers']     = $this->db->select('c_id')->from('customers')->get()->num_rows();
        $data['tot_today_trips']   = $this->db->select('t_id')->from('trips')->where('t_start_date', date('Y-m-d'))->get()->num_rows();
        $data['tot_today_income']  = $this->db->select_sum('ie_amount')->from('incomeexpense')->where('ie_date', date('Y-m-d'))->where('ie_type', 'income')->get()->row()->ie_amount;
        $data['tot_today_expense'] = $this->db->select_sum('ie_amount')->from('incomeexpense')->where('ie_date', date('Y-m-d'))->where('ie_type', 'expense')->get()->row()->ie_amount;
        return $data;
    }


    public function add_event($data)
	{
        $firebase = $this->firebase->init();
        $storage = $firebase->createStorage();
        $defaultBucket = $storage->getBucket();



        if ($_FILES['image']['tmp_name'] != '') {
            $fileName = "events" . rand() . time() . $_FILES['image']['name'];
            $uploadedFile = $defaultBucket->upload(
              file_get_contents($_FILES['image']['tmp_name']),
              [
                'name' => $fileName,
              ]
            );

            $data['image'] = $uploadedFile->info()['mediaLink'];
          }
        $data['date'] = time()*1000;


		$x = $this->config->item('firebase_app_key');
		$firestore = new FirestoreClient([
			'projectId' => $this->config->item('firebase_project_id'),
			'keyFile' => json_decode(file_get_contents($x), true)
		]);

		$firestore->collection('Restaurant')->add($data);
		return true;
	}
	public function add_country($data)
	{
        $firebase = $this->firebase->init();
        $storage = $firebase->createStorage();
        $defaultBucket = $storage->getBucket();



        if ($_FILES['image']['tmp_name'] != '') {
            $fileName = "country" . rand() . time() . $_FILES['image']['name'];
            $uploadedFile = $defaultBucket->upload(
              file_get_contents($_FILES['image']['tmp_name']),
              [
                'name' => $fileName,
              ]
            );

            $data['image'] = $uploadedFile->info()['mediaLink'];
          }
        $data['date'] = time()*1000;


		$x = $this->config->item('firebase_app_key');
		$firestore = new FirestoreClient([
			'projectId' => $this->config->item('firebase_project_id'),
			'keyFile' => json_decode(file_get_contents($x), true)
		]);

		$firestore->collection('Country')->add($data);
		return true;
	}
  public function add_speciality($data)
	{

		$firebase = $this->firebase->init();
        $storage = $firebase->createStorage();
        $defaultBucket = $storage->getBucket();

		$x = $this->config->item('firebase_app_key');
		$firestore = new FirestoreClient([
			'projectId' => $this->config->item('firebase_project_id'),
			'keyFile' => json_decode(file_get_contents($x), true)
		]);



        if ($_FILES['image']['tmp_name'] != '') {
            $fileName = "events" . rand() . time() . $_FILES['image']['name'];
            $uploadedFile = $defaultBucket->upload(
              file_get_contents($_FILES['image']['tmp_name']),
              [
                'name' => $fileName,
              ]
            );

            $data['image'] = $uploadedFile->info()['mediaLink'];
          }
		  if ($_FILES['image1']['tmp_name'] != '') {
            $fileName = "events" . rand() . time() . $_FILES['image1']['name'];
            $uploadedFile = $defaultBucket->upload(
              file_get_contents($_FILES['image1']['tmp_name']),
              [
                'name' => $fileName,
              ]
            );

            $data['image1'] = $uploadedFile->info()['mediaLink'];
          }else{
			$data['image1'] ="";
		  }
		  if ($_FILES['image2']['tmp_name'] != '') {
            $fileName = "events" . rand() . time() . $_FILES['image2']['name'];
            $uploadedFile = $defaultBucket->upload(
              file_get_contents($_FILES['image2']['tmp_name']),
              [
                'name' => $fileName,
              ]
            );

            $data['image2'] = $uploadedFile->info()['mediaLink'];
          }else{
			$data['image2'] ="";
		  }
        $data['date'] = time()*1000;


		$firestore->collection('Dishes')->add($data);
		return true;
	}


public function add_category($data)
	{

		$x = $this->config->item('firebase_app_key');
		$firestore = new FirestoreClient([
			'projectId' => $this->config->item('firebase_project_id'),
			'keyFile' => json_decode(file_get_contents($x), true)
		]);

		$firestore->collection('Categories')->add($data);
		return true;
	}


   
    public function get_events()
	{

		
		$x = $this->config->item('firebase_app_key');
		$db = new FirestoreClient([
			'projectId' => $this->config->item('firebase_project_id'),
			'keyFile' => json_decode(file_get_contents($x), true)
		]);

		$eventsRef = $db->collection('Restaurant');
		$documents = $eventsRef->documents();
		$events = array();
		foreach ($documents as $document) {
			if ($document->exists()) {
				$events[$document->id()] = $document->data();
			}
		}
		return $events;
	}

  public function get_specialities()
	{

		
		$x = $this->config->item('firebase_app_key');
		$db = new FirestoreClient([
			'projectId' => $this->config->item('firebase_project_id'),
			'keyFile' => json_decode(file_get_contents($x), true)
		]);

		$eventsRef = $db->collection('Dishes');
		$documents = $eventsRef->documents();
		$specialities = array();
		foreach ($documents as $document) {
			if ($document->exists()) {
				$specialities[$document->id()] = $document->data();
			}
		}
		return $specialities;
	}

  public function get_category()
	{

		
		$x = $this->config->item('firebase_app_key');
		$db = new FirestoreClient([
			'projectId' => $this->config->item('firebase_project_id'),
			'keyFile' => json_decode(file_get_contents($x), true)
		]);

		$eventsRef = $db->collection('Restaurant');
		$documents = $eventsRef->documents();
		$specialities = array();
		foreach ($documents as $document) {
			if ($document->exists()) {
				$specialities[$document->id()] = $document->data();
			}
		}
		return $specialities;
	}

	public function get_country()
	{

		
		$x = $this->config->item('firebase_app_key');
		$db = new FirestoreClient([
			'projectId' => $this->config->item('firebase_project_id'),
			'keyFile' => json_decode(file_get_contents($x), true)
		]);

		$eventsRef = $db->collection('Country');
		$documents = $eventsRef->documents();
		$specialities = array();
		foreach ($documents as $document) {
			if ($document->exists()) {
				$specialities[$document->id()] = $document->data();
			}
		}
		return $specialities;
	}

    
}