<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Categories extends CI_Controller
{

  function __construct()
  {
    parent::__construct();
    // $this->load->database();
    $this->load->model('common');
    $this->load->helper(array('form', 'url', 'string'));
    $this->load->library('form_validation');
    $this->load->library('session');
  }
  public function addcategory()
  {
      //     $data["users"] = $this->common->readdatafromcollectionbetween("Bookings");
      $this->template->template_render('category_add');
  }
  public function index()
  {

      $data['users'] = $this->common->readdatafromcollectionwhereclause('Category');
      // print_r($data);
      // exit;

      $this->template->template_render('categories_management', $data);
  }
    //
    public function editcategory($id)
    {
        // 
        $data["category"] = $this->common->readdatadocument("Category", $id);
        $data['catid'] = $id;
        $this->template->template_render('categories_edit', $data);
    }
  public function deletecategory($id)
  {

      $response = $this->common->deletedatamodel("Category", $id);
      if ($response) {
          $this->session->set_flashdata('successmessage', ' Category has been Removed successfully..');
      } else {
          $this->session->set_flashdata('warningmessage', 'Error in delete  Category..');
      }
      redirect(base_url() . 'categories/categories/');
  }
  public function addcategorydata()

  {
      $data = $this->input->post();
      $firebase = $this->firebase->init();
      $storage = $firebase->createStorage();
      $defaultBucket = $storage->getBucket();
      if ($_FILES['image']['tmp_name'] != '') {
        $uploadedFile = $defaultBucket->upload(
          file_get_contents($_FILES['image']['tmp_name']),
          [
            'name' => "UserImage/user" . rand() . time() . $_FILES['image']['name'],
          ]
        );
        $data['image'] = $uploadedFile->info()['mediaLink'];
      
      }
     
      $id = time();
      $response = $this->common->adddatamodel("Category", $id, $data);
      //    $this->template->template_render('category_add');
      if ($response) {
          $this->session->set_flashdata('successmessage', 'New Category added successfully..');
      } else {
          $this->session->set_flashdata('warningmessage', 'Error in creating new Category..');
      }
      redirect(base_url() . 'categories/addcategory');
  }
//	$firebase = $this->firebase->init();






}
