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

class Dashboard extends CI_Controller {

	 function __construct()
     {
          parent::__construct();
          $this->load->database();
          $this->load->helper('url');
          $this->load->library('session');
          $this->load->model('dashboard_model');
          $this->load->model('customer_model');
          $this->load->model('common');
          	define( 'API_ACCESS_KEY1', 'AAAANrVnP-A:APA91bF12A4sgPNkTV9AHjD-ep7m10jqPeHyaeKo9gZVBh0SbSLcVWCd7l7NQ3dTpwxSrnNRDV4o2XldZMLv-Di_Lzx1rtjCjyM6eEO4Sypq95bwSk4McnvVBSgIhdb4CMGC3mSUZwgq');
     }
     function send_notification($token_id,$title,$body,$id,$type){
	 
	$icon=base_url('uploads/logo.png');
	
	$finalPostArray = array('registration_ids' => $token_id,
								'notification' => array('body' => $body,
														'title' => $title,
														"image"=> $icon,
														   "sound"=> "sample4_out.caf"
													),
									'aps'=>array(
										"sound"=> "sample4_out.caf"
									),				
								"data"=> array("click_action"=> "FLUTTER_NOTIFICATION_CLICK",
												"type"=>$type,
												"id"=>$id,
												"sound"=> "sample4_out.caf", 
												"soundios"=> "sample4_out.caf",
												"vibrate"=> 1)); 
		
	
		
		
		$headers = array
				(
					'Authorization: key=' . API_ACCESS_KEY1,
					'Content-Type: application/json'
				);
	#Send Reponse To FireBase Server	
			$ch = curl_init();
			curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
			curl_setopt( $ch,CURLOPT_POST, true );
			curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
			curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
			curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
			curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $finalPostArray ) );
			$result['mystatus'] = curl_exec($ch );
			curl_close( $ch );
	#Echo Result Of FireBase Server
	//$result['firbasestatus']=$fields;
	return $result;
		}

public function mytest($id="111",$msg="new data",$type="Event",$ntype="user"){
	$data = $this->customer_model->getall_customernew();;
	//exit;
		$token=array();
		$userIdArray=array();
	


		foreach($data["user"] as $l=>$j){
			 if(!empty($j['token'])  && $j['token'] !=""){
			 	  $userIdArray[$l] = false;
			array_push($token, $j['token'])	; 	
			 }
			 $ceta=array();
			 $ceta['reciverId']=$l;
			 $ceta['inserteddate']=time()*1000;
			 $ceta['senderId']="admin";
			 $ceta['type']="event";
			 $ceta['eventId']=$id;
			 $ceta['msg']="Admin has been added new event.";
			 $insertedid=rand().time();
			 $this->common->adddatamodel("Notification",$insertedid,$ceta);

	
		}
//		echo "<pre>";
//		print_r($data);
//		print_r($token);

		$notification=$this->send_notification($token,"New Event",$msg,$id,$type);
 //print_r($notification);
//echo "</pre>";
}

public function index(){

	$data['appversion'] = $this->common->readdatadocument("Version","IT7O6P1WTXwrJt6jSJpc");
	$data['reminder'] = $this->common->readdatadocument("reminderlogs","stats");
	$data['countrystate'] = $this->common->readdatacollection("analytics","countries","country_stats");
	// echo "<pre>";
	// print_r($data['countrystate']);
	// echo "</pre>";
	// 	exit;
	$this->template->template_render('dispute',$data);
	 

}


	public function indexold()
	{
		$jana=array();
		$timestamps=array();
		$users = $this->common->readdatafromcollectionwhereclause("Users");
		foreach ($users as $key => $user) {
			$timestamps[$key]    = $user['userDate'];
		} 
		array_multisort($timestamps, SORT_ASC, $users);
		$labels = array();
		

		 $labels[0]=  date('F', strtotime($labels[0]." + 6 month"));
	  
		for($i =1; $i<=6; $i++) {
		 $timestamp = strtotime($labels[$i-1].' + 1 month');
		 $labels[$i] = date('F', $timestamp);
	   }
	   $labelStr = '';
	   foreach($labels as $label) {
		 $labelStr .= '"'.$label.'", ';
	   }
	   
	   $data['labelStr'] = $labelStr;
	   //print_r($data);
	   $dataStr = '';
	   for($i =0; $i<count($labels); $i++) {
		 $num = 0;
		 foreach($timestamps as $timestamp) {
   
   
		   if($labels[$i] == date('F', $timestamp/1000)) {
		  //   echo "yes";
			 $num += 1;
		   }
		 }
		 $dataStr .= $num.',';
	   }
	   $datarecord = explode(",",$dataStr);
	  // var_dump($labels);
$j=array();
for($i=0;$i<7;$i++){
//	echo $labels[$i]."===".$datarecord[$i];
//	echo "<br>";
$papa=array();
$papa['label']=$labels[$i];
 $papa['y']=(int)$datarecord[$i];
 $papa['x']=$i;
 array_push($j,$papa);

}



	   
		$guestcount = count($users);
			//$jana
          $gusestuser=[];
          $gusestuser['label']="Users";
          $gusestuser['y']=$guestcount;
          array_push($jana, $gusestuser);
          $guestcount = $this->common->readdatafromcollectionwhereclause("Reminders");
		 

      $gusestuser=[];
          $gusestuser['label']="Reminders";
          $gusestuser['y']=count($guestcount);
          array_push($jana, $gusestuser);
		$archived=0;
		foreach($guestcount as $gu=>$pr){
			if($pr['isArchive']==true){
				$archived=$archived+1;
			}
		}
		$gusestuser=[];
		$gusestuser['label']="Archived";
		$gusestuser['y']=$archived;
		array_push($jana, $gusestuser);


//exit;

// echo "<pre>";
// print_r($data);
// //print_r($jana);
// echo "</pre>";

$guestcount = $this->common->readdatafromcollectionwhereclause("Version");

//  echo "<pre>";
//  print_r($guestcount);
// // //print_r($jana);
//  echo "</pre>";
 $android=0;
 $ios=0;
foreach($guestcount as $g=>$v){
$ios=$v['ios'];
$android=$v['android'];
	
}


 $gusestuser=[];
 $gusestuser['label']="Android Version";
 $gusestuser['y']=$android;
 array_push($jana, $gusestuser);
 $gusestuser=[];
 $gusestuser['label']="Ios Version";
 $gusestuser['y']=$ios;
 array_push($jana, $gusestuser);


 $androiduser=0;
 $iosuser=0;
 $guestcount = $this->common->readdatafromcollectionwhereclause("UserList");
foreach($guestcount as $g=>$y){
if($y['userType']=="android"){
	$androiduser=$androiduser+1;
}else{
	$iosuser=$iosuser+1;
}

}

$gusestuser=[];
$gusestuser['label']="Andoid Downloads";
$gusestuser['y']=$androiduser+12;
array_push($jana, $gusestuser);
$gusestuser=[];
$gusestuser['label']="Ios Downloads";
$gusestuser['y']=$iosuser+21;
array_push($jana, $gusestuser);




$data['result']= json_encode($jana);
$data['result3']= json_encode($j);
$data['result2']= ($jana);

          $this->template->template_render('dashboard',$data);
	}

public function lettsdashboard1(){
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => 'https://us-central1-letts-of-london.cloudfunctions.net/widgets/getuserlist',
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => '',
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 0,
	  CURLOPT_FOLLOWLOCATION => true,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => 'GET',
	
	));
	
	$response = curl_exec($curl);
	
	curl_close($curl);
	$res=json_decode($response,true);
	// echo "<pre>";
	// var_dump($res);
	// echo "</pre>";	
	$jana=array();
	$gusestuser=[];
	$gusestuser['label']="Users";
	$gusestuser['y']=count($res);
	array_push($jana, $gusestuser);

	$timestamps=array();
	$users = $res;
	foreach ($users as $key => $user) {
		
		$timestamps[$key]    = $user['userDate'];
	} 
	array_multisort($timestamps, SORT_ASC, $users);
		
	$labels = array();
	

	 $labels[0]=  date('F', strtotime($labels[0]." + 6 month"));
  
	for($i =1; $i<=6; $i++) {
	 $timestamp = strtotime($labels[$i-1].' + 1 month');
	 $labels[$i] = date('F', $timestamp);
   }

   $labelStr = '';
   foreach($labels as $label) {
	 $labelStr .= '"'.$label.'", ';
   }
   
   $data['labelStr'] = $labelStr;
   //print_r($data);
   $dataStr = '';
   for($i =0; $i<count($labels); $i++) {
	 $num = 0;
	 foreach($timestamps as $timestamp) {


	   if($labels[$i] == date('F', $timestamp/1000)) {
	  //   echo "yes";
		 $num += 1;
	   }
	 }
	 $dataStr .= $num.',';
   }
    
   $datarecord = explode(",",$dataStr);
  // var_dump($labels);

//exit; 
  $j=array();
for($i=0;$i<7;$i++){
//	echo $labels[$i]."===".$datarecord[$i];
//	echo "<br>";
$papa=array();
$papa['label']=$labels[$i];
$papa['y']=(int)$datarecord[$i];
$papa['x']=$i;
array_push($j,$papa);

}








	$curl = curl_init();
	
	curl_setopt_array($curl, array(
	  CURLOPT_URL => 'https://us-central1-letts-of-london.cloudfunctions.net/widgets/getreminderlist',
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => '',
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 0,
	  CURLOPT_FOLLOWLOCATION => true,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => 'GET',
	
	));
	
	$response = curl_exec($curl);
	
	curl_close($curl);
	$res=json_decode($response,true);
	$gusestuser=[];
	$gusestuser['label']="Reminders";
	$gusestuser['y']=count($res);
	array_push($jana, $gusestuser);
  $archived=0;
  foreach($res as $gu=>$pr){
	  if($pr['isArchive']==true){
		  $archived=$archived+1;
	  }
  }
  $gusestuser=[];
  $gusestuser['label']="Archived";
  $gusestuser['y']=$archived;
  array_push($jana, $gusestuser);

  $curl = curl_init();
  curl_setopt_array($curl, array(
	CURLOPT_URL => 'https://us-central1-letts-of-london.cloudfunctions.net/widgets/getVersion',
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_ENCODING => '',
	CURLOPT_MAXREDIRS => 10,
	CURLOPT_TIMEOUT => 0,
	CURLOPT_FOLLOWLOCATION => true,
	CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	CURLOPT_CUSTOMREQUEST => 'GET',
  
  ));
  
  $response = curl_exec($curl);
  
  curl_close($curl);
  $res=json_decode($response,true);

// echo "<pre>";
// print_r($res);
// echo "</pre>";

$android=0;
 $ios=0;
foreach($res as $v){
$ios=$v['ios'];
$android=$v['android'];
	
}


 $gusestuser=[];
 $gusestuser['label']="Android Version";
 $gusestuser['y']=$android;
 array_push($jana, $gusestuser);

 $gusestuser=[];
 $gusestuser['label']="Ios Version";
 $gusestuser['y']=$ios;
 array_push($jana, $gusestuser);



// echo "<pre>";
// print_r($data);
// //print_r($jana);
// echo "</pre>";
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://us-central1-letts-of-london.cloudfunctions.net/widgets/getdownloadlist',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',

));

$response = curl_exec($curl);

curl_close($curl);
$res=json_decode($response,true);

//echo "<pre>";
//print_r($res);
//echo "</pre>";
$androiduser=0;
$iosuser=0;
foreach($res as $g=>$y){
	if($y['userType']=="android"){
		$androiduser=$androiduser+1;
	}else{
		$iosuser=$iosuser+1;
	}
	
	}
	

	$gusestuser=[];
	$gusestuser['label']="Andoid Downloads";
	$gusestuser['y']=$androiduser+20;
	array_push($jana, $gusestuser);
	$gusestuser=[];
	$gusestuser['label']="Ios Downloads";
	$gusestuser['y']=$iosuser+16;
	array_push($jana, $gusestuser);


	$data['result']= json_encode($jana);

	$data['result3']= json_encode($j);

$data['result2']= ($jana);





          $this->template->template_render('dashboard',$data);

}
public function lettsdashboard(){
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => 'https://us-central1-letts-of-london.cloudfunctions.net/widgets/myanayltics',
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => '',
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 0,
	  CURLOPT_FOLLOWLOCATION => true,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => 'GET',
	
	));
	
	$response = curl_exec($curl);
	
	curl_close($curl);
	$res=json_decode($response,true);

	$data['appversion']=$res['data']['appversion'];
	$data['reminder']=$res['data']['reminder'];
	$data['countrystate']=$res['data']['countrystate'];
	$this->template->template_render('dispute',$data);
}
public function filofaxdashboard(){
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => 'https://us-central1-filofax-96375.cloudfunctions.net/widgets/myanayltics',
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => '',
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 0,
	  CURLOPT_FOLLOWLOCATION => true,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => 'GET',
	
	));
	
	$response = curl_exec($curl);
	
	curl_close($curl);
	$res=json_decode($response,true);

	$data['appversion']=$res['data']['appversion'];
	$data['reminder']=$res['data']['reminder'];
	$data['countrystate']=$res['data']['countrystate'];
	$this->template->template_render('dispute',$data);
	// echo "<pre>";
	// print_r($data);
	// echo "</pre>";
}
public function filofaxdashboard1(){
	if ($this->session->userdata('user_list_data')) {
		// Use the cached data from the session
		$res = $this->session->userdata('user_list_data');
	} else {
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => 'https://us-central1-filofax-96375.cloudfunctions.net/widgets/getuserlist',
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => '',
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 0,
	  CURLOPT_FOLLOWLOCATION => true,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => 'GET',
	
	));
	
	$response = curl_exec($curl);
	
	curl_close($curl);
	$res=json_decode($response,true);
	$this->session->set_userdata('user_list_data', $res);

	}
	// echo "<pre>";
	// var_dump($res);
	// echo "</pre>";	
	$jana=array();
	$gusestuser=[];
	$gusestuser['label']="Users";
	$gusestuser['y']=count($res);
	array_push($jana, $gusestuser);
	$timestamps=array();
	$users = $res;
	foreach ($users as $key => $user) {
		
		$timestamps[$key]    = $user['userDate'];
	} 
	array_multisort($timestamps, SORT_ASC, $users);
		
	$labels = array();
	

	 $labels[0]=  date('F', strtotime($labels[0]." + 6 month"));
  
	for($i =1; $i<=6; $i++) {
	 $timestamp = strtotime($labels[$i-1].' + 1 month');
	 $labels[$i] = date('F', $timestamp);
   }

   $labelStr = '';
   foreach($labels as $label) {
	 $labelStr .= '"'.$label.'", ';
   }
   
   $data['labelStr'] = $labelStr;
   //print_r($data);
   $dataStr = '';
   for($i =0; $i<count($labels); $i++) {
	 $num = 0;
	 foreach($timestamps as $timestamp) {


	   if($labels[$i] == date('F', $timestamp/1000)) {
	  //   echo "yes";
		 $num += 1;
	   }
	 }
	 $dataStr .= $num.',';
   }
    
   $datarecord = explode(",",$dataStr);
  // var_dump($labels);

//exit; 
  $j=array();
for($i=0;$i<7;$i++){
//	echo $labels[$i]."===".$datarecord[$i];
//	echo "<br>";
$papa=array();
$papa['label']=$labels[$i];
$papa['y']=(int)$datarecord[$i];
$papa['x']=$i;
array_push($j,$papa);

}
if ($this->session->userdata('reminder_list_data')) {
	// Use the cached data from the session
	$res = $this->session->userdata('reminder_list_data');
} else {

	$curl = curl_init();
	
	curl_setopt_array($curl, array(
	  CURLOPT_URL => 'https://us-central1-filofax-96375.cloudfunctions.net/widgets/getreminderlist',
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => '',
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 0,
	  CURLOPT_FOLLOWLOCATION => true,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => 'GET',
	
	));
	
	$response = curl_exec($curl);
	
	curl_close($curl);
	$res=json_decode($response,true);
	$this->session->set_userdata('reminder_list_data', $res);
	}

	$gusestuser=[];
	$gusestuser['label']="Reminders";
	$gusestuser['y']=count($res);
	array_push($jana, $gusestuser);
  $archived=0;
  foreach($res as $gu=>$pr){
	  if($pr['isArchive']==true){
		  $archived=$archived+1;
	  }
  }
  $gusestuser=[];
  $gusestuser['label']="Archived";
  $gusestuser['y']=$archived;
  array_push($jana, $gusestuser);

  $curl = curl_init();
  curl_setopt_array($curl, array(
	CURLOPT_URL => 'https://us-central1-filofax-96375.cloudfunctions.net/widgets/getVersion',
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_ENCODING => '',
	CURLOPT_MAXREDIRS => 10,
	CURLOPT_TIMEOUT => 0,
	CURLOPT_FOLLOWLOCATION => true,
	CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	CURLOPT_CUSTOMREQUEST => 'GET',
  
  ));
  
  $response = curl_exec($curl);
  
  curl_close($curl);
  $res=json_decode($response,true);

// echo "<pre>";
// print_r($res);
// echo "</pre>";

$android=0;
 $ios=0;
foreach($res as $v){
$ios=$v['ios'];
$android=$v['android'];
	
}


 $gusestuser=[];
 $gusestuser['label']="Android Version";
 $gusestuser['y']=$android;
 array_push($jana, $gusestuser);

 $gusestuser=[];
 $gusestuser['label']="Ios Version";
 $gusestuser['y']=$ios;
 array_push($jana, $gusestuser);



// echo "<pre>";
// print_r($data);
// //print_r($jana);
// echo "</pre>";
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://us-central1-filofax-96375.cloudfunctions.net/widgets/getdownloadlist',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',

));

$response = curl_exec($curl);

curl_close($curl);
$res=json_decode($response,true);

//echo "<pre>";
//print_r($res);
//echo "</pre>";
$androiduser=0;
$iosuser=0;
foreach($res as $g=>$y){
	if($y['userType']=="android"){
		$androiduser=$androiduser+1;
	}else{
		$iosuser=$iosuser+1;
	}
	
	}
	

	$gusestuser=[];
	$gusestuser['label']="Andoid Downloads";
	$gusestuser['y']=$androiduser+268;
	array_push($jana, $gusestuser);
	$gusestuser=[];
	$gusestuser['label']="Ios Downloads";
	$gusestuser['y']=$iosuser+567;
	array_push($jana, $gusestuser);
	$data['result']= json_encode($jana);
	$data['result3']= json_encode($j);
$data['result2']= ($jana);



          $this->template->template_render('dashboard',$data);

}



	

	public function events()
	{
          $data["events"] = $this->dashboard_model->get_events();
          $this->template->template_render('events',$data);
	}

	public function specialities()
	{
          $data["specialities"] = $this->dashboard_model->get_specialities();
          $this->template->template_render('specialities',$data);
	}
		public function category()
	{
          $data["specialities"] = $this->dashboard_model->get_category();
          $this->template->template_render('category',$data);
	}
	
  public function addevent()
	{

		$this->template->template_render('event_add');
	}

	public function addspeciality()
	{
				$data["category"] = $this->dashboard_model->get_category();
		// echo "<pre>";
		// print_r($data);
		// echo "</pre>";
		$this->template->template_render('specialities_add',$data);
	}
	public function addcategory()
	{
		$this->template->template_render('category_add');
	}

  public function insertevent()
	{
		$data = $this->input->post();

		$response = $this->dashboard_model->add_event($data);
		if ($response) {
			$id=time();
		$this->mytest($id,"New Event has been added by Admin",'event',"user");	

			$this->session->set_flashdata('successmessage', 'New event added successfully');
			redirect(base_url().'dashboard/events');
		} else {
			$errormsg = 'Error! Event not added, please check event data.';
			$this->session->set_flashdata('warningmessage', $errormsg);
			redirect(base_url().'dashboard/addevent');
		}
	}

	public function insertCategory()
	{
		$data = $this->input->post();
		 $checklist=array("Cosmetic","Studio","Supplier","Tattoo Artist");
                                if(in_array(output($data['name']),$checklist,TRUE)){
		$errormsg = 'Error! category already exists.';
			$this->session->set_flashdata('warningmessage', $errormsg);
			redirect(base_url().'dashboard/category');

                                }else{


		$response = $this->dashboard_model->add_category($data);
		if ($response) {
			$this->session->set_flashdata('successmessage', 'New category added successfully');
			redirect(base_url().'dashboard/category');
		} else {
			$errormsg = 'Error! category not added, please check data.';
			$this->session->set_flashdata('warningmessage', $errormsg);
			redirect(base_url().'dashboard/category');
		}
	}
	}
	public function insertspeciality()
	{
		$data = $this->input->post();
		// print_r($data);
		// exit;


		$response = $this->dashboard_model->add_speciality($data);

		if ($response) {
			$this->session->set_flashdata('successmessage', 'New speciality added successfully');
			redirect(base_url().'dashboard/specialities');
		} else {
			$errormsg = 'Error! Speciality not added, please check data.';
			$this->session->set_flashdata('warningmessage', $errormsg);
			redirect(base_url().'dashboard/specialities');
		}
	}

  public function updateevent()
	{
		$data = $this->input->post();

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

        $data = $this->input->post();


        if ($_FILES['image']['tmp_name'] != '') {
            $fileName = "events" . rand() . time() . $_FILES['image']['name'];
            $uploadedFile = $defaultBucket->upload(
              file_get_contents($_FILES['image']['tmp_name']),
              [
                'name' => $fileName,
				'predefinedAcl' => 'publicRead'
              ]
            );
            // $data['image'] = 'https://firebasestorage.googleapis.com/v0/b/tattoo-f01b9.appspot.com/o/'.$fileName.'?alt=media';
                        $data['image'] = $uploadedFile->info()['mediaLink'];
          }
        $data['date'] = strtotime($data['date']);

		$response = $this->common->updatedatamodel("Events", $data["id"], $data);
    
		if ($response) {
			$this->session->set_flashdata('successmessage', 'Event updated added successfully');
			redirect(base_url().'dashboard/events');
		} else {
			$errormsg = 'Error! Event not updated, please check event data.';
			$this->session->set_flashdata('warningmessage', $errormsg);
			redirect(base_url().'dashboard/addevent'.$data['id']);
		}
	}

	public function updateCategory()
	{
		$data = $this->input->post();
 $checklist=array("Cosmetic","Studio","Supplier","Tattoo Artist");
                                if(in_array(output($data['name']),$checklist,TRUE)){
		$errormsg = 'Error! category already exists.';
			$this->session->set_flashdata('warningmessage', $errormsg);
			redirect(base_url().'dashboard/category');

                                }else{		

		$response = $this->common->updatedatamodel("Categories", $data["id"], $data);
    
		if ($response) {
			$this->session->set_flashdata('successmessage', 'category updated successfully');
			redirect(base_url().'dashboard/category');
		} else {
			$errormsg = 'Error! category not updated, please check data.';
			$this->session->set_flashdata('warningmessage', $errormsg);
			redirect(base_url().'dashboard/category'.$data['id']);
		}
	}
}
	public function updatespeciality()
	{
		$data = $this->input->post();
		

		$response = $this->common->updatedatamodel("Specialities", $data["id"], $data);
    
		if ($response) {
			$this->session->set_flashdata('successmessage', 'Speciality updated successfully');
			redirect(base_url().'dashboard/specialities');
		} else {
			$errormsg = 'Error! Speciality not updated, please check data.';
			$this->session->set_flashdata('warningmessage', $errormsg);
			redirect(base_url().'dashboard/addspeciality'.$data['id']);
		}
	}

  public function editevent()
	{
		$eid = $this->uri->segment(3);
		$data['event'] = $this->common->readdatadocument("Events",$eid);
		$data['event']['id'] = $eid;
		$this->template->template_render('event_add', $data);
	}

	public function editspeciality()
	{
		$id = $this->uri->segment(3);
		$data['speciality'] = $this->common->readdatadocument("Specialities",$id);
		$data['speciality']['id'] = $id;
		$data["category"] = $this->dashboard_model->get_category();
		$this->template->template_render('specialities_add', $data);
	}
		public function editcategory()
	{
		$id = $this->uri->segment(3);
		$data['Category'] = $this->common->readdatadocument("Categories",$id);
		$data['Category']['id'] = $id;
		$this->template->template_render('category_add', $data);
	}

	public function deleteevent($eid){
		$response = $this->common->deletedatamodel("Events",$eid);

		if ($response) {
			$this->session->set_flashdata('successmessage', 'Event deleted successfully');
			redirect(base_url().'dashboard/events');
		} else {
			$errormsg = 'Error! Event cannot be deleted.';
			$this->session->set_flashdata('warningmessage', $errormsg);
			redirect(base_url().'dashboard/events');
		}
	}

	public function deletespeciality($id){
		$response = $this->common->deletedatamodel("Specialities",$id);

		if ($response) {
			$this->session->set_flashdata('successmessage', 'Speciality deleted successfully');
			redirect(base_url().'dashboard/specialities');
		} else {
			$errormsg = 'Error! Speciality cannot be deleted.';
			$this->session->set_flashdata('warningmessage', $errormsg);
			redirect(base_url().'dashboard/specialities');
		}
	}
		public function deletecategory($id){
		$response = $this->common->deletedatamodel("Categories",$id);

		if ($response) {
			$this->session->set_flashdata('successmessage', 'Speciality deleted successfully');
			redirect(base_url().'dashboard/category');
		} else {
			$errormsg = 'Error! Speciality cannot be deleted.';
			$this->session->set_flashdata('warningmessage', $errormsg);
			redirect(base_url().'dashboard/category');
		}
	}
     
}