<?php
namespace App\Http\Controllers\v2019;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Auth\authentication;

class EventsController extends Controller
{ 
    public function __construct(Request $request) {
        if($request->has('debug') && $request->input('debug') == true) {
			$this->debug = true;
            include_once "/home/steelmin/public_html/api/pre_function/sm_events.php";	
        }
    }

   /**
     * @param Request $request
     * @param $id
     * @return JsonResponse
     * Get events package details with tax, and currently is not used
     */
    public function getEventPackage(Request $request, $id)
    {
        include_once "/home/steelmin/public_html/api/pre_function/sm_events.php";
       // echo $_REQUEST['currencyCode'] . " -- " . $id . " -- " . $this->debug ; 
		///$_rs_array = getEventPackageDetail($ipAddress = '' , $id, '', $_REQUEST['currencyCode']  ) ;
		$_rs_array = getEventPackageDetail($ipAddress = '' , $id, '',$request->input('currencyCode') ) ;

         return new JsonResponse(array(
            'status' => [
                "code" => 1001,
                'message' => "Success"
            ],
			'data' => $_rs_array
        )) ;

       /* return new JsonResponse(array(
            "status#" => "1" ,
            "data#" => $_rs_array
        )) ;*/
    }
    
    public function getEventListing(Request $request)
    {      
		 $v = app('App\Http\Controllers\v2019\Bookmark')->getBookmarkId($request);
		 $bookmarks = $v->getData()->data;
     
		 if ($request->has('bookmarked') && $request->input('bookmarked')!='')
		  $is_bookmarked = $request->input('bookmarked');
		else 
		  $is_bookmarked = 0;
		 
			if ($request->has('filters') && $request->input('filters')!='')
		  $filter = $request->input('filters');
		else 
			$filter = array();
		
	   $registering_id='';
	   $registering_id = \App\Helper::getMemId();	
	   $result = array();

	   $resourceType = "APP";		
	   if ($request->has('resourceType'))
		   $resourceType = strtolower($request->input('resourceType'));
		   
	   if ($request->has('source'))
		   $source = strtolower($request->input('source'));	   
	  
		$result = \ApiQuery::getEventsRowSet($bookmarks,$is_bookmarked,$filter,$source,$registering_id);
		$code = 1001;
		$message = "Success";
//		print_r($result);
//	   echo json_encode($result); dd();
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}

	public function getWebEventListing(Request $request){
        $v = app('App\Http\Controllers\v2019\Bookmark')->getBookmarkId($request);
		$bookmarks = $v->getData()->data;
		
		if ($request->has('bookmarked') && $request->input('bookmarked')!='')
		  $is_bookmarked = $request->input('bookmarked');
		else 
		  $is_bookmarked = 0; 

		if($request->has('pageValue') && $request->input('pageValue')!=''){
			$pageValue = $request->input('pageValue');
			$offset = ($pageValue-1)*8;
	    }else{ 
			$pageValue = 1;$offset=0;    
	    }  

		if ($request->has('source') && $request->input('source')!='')
		  $source = $request->input('source');


		if ($request->has('filters') && $request->input('filters')!=''){
		  $filter = $request->input('filters');
			if( isset($filter['app_type']) && $filter['app_type'] == 'CM'){
				$filter['commodityID'] = array(401);//if coalmint then show only colamint related reports
			}
		  
		} else 
		  $filter = array();
		
		if ($request->has('listType') && $request->input('listType')!='')
		  $ltype = $request->input('listType');
		else 
		  $ltype = array();  

		$result = \apiQuery::getEventsWebRowSet($bookmarks,$filter,$ltype,$is_bookmarked,$offset,$pageValue, $source);

		//if(strtolower($source) == "mapview")
		//		$result = $this->result_web($result);

		$code = 1001;
		$message = "Success";
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}

	private function result_web(&$result){
		$result_with_viewmore = array();$vmore=array("parent"=>"","val"=>"","indexer"=>false);
		usort($result, array($this, "build_sorted"));
		foreach($result as $k=>$v){
			  $vmore["indexer"] = $vmore["val"] == $v["regionID"] ? true : false;
			   if($vmore["indexer"]){
				   $vmore["parent"] = $vmore["parent"] == "" ? count($result_with_viewmore)-1:$vmore["parent"];
				   $result_with_viewmore[$vmore["parent"]]["viewmore"][] = $v;
			   }else{
				   $vmore["parent"] = "";
				   $result_with_viewmore[] = $v;
			   }  
			      
			   $vmore["val"] = $v["regionID"];
		}
		return $result_with_viewmore;
	}
	
	private function build_sorted ($a, $b){
			if ($a["regionID"] == $b["regionID"]) {
				return 0;
			}
			return ($a["regionID"] < $b["regionID"]) ? -1 : 1;
	}
	
    public function getEventSchedule(Request $request)
    {      
		 //include_once "/home/steelmin/public_html/api/storage/v2019/event/eventSchedule_list.php";
// 		 $v = app('App\Http\Controllers\v2019\Bookmark')->getBookmarkId($request);
// 		 $bookmarks = $v->getData()->data;

// 		 $a = app('App\Http\Controllers\v2019\Alert')->getAlerts($request);
// 		 $alerts = $a->getData()->data;
		

	   if ($request->has('eventID') && $request->input('eventID')!='')
		  $event_id = $request->input('eventID');
		else 
		  $event_id = '';

// 	   if ($request->has('bookmarked') && $request->input('bookmarked')!='')
// 		  $is_bookmarked = $request->input('bookmarked');
// 		else 
// 			$is_bookmarked = 0;
			
// 			if ($request->has('type') && $request->input('type')!='')
// 		  $type = $request->input('type');
// 		else 
// 		  $type = '';	

// 		 $result = \apiQuery::getSchedulesRowSet($event_id,$alerts,$bookmarks,$is_bookmarked,$type);
		 
		 $result = \apiQuery::getSchedulesRowSet_new($event_id);
		 
		 
		 $code = 1001;
		 $message = "Success";
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }

    public function getEventSpeakerListing(Request $request)
    {      
        $result = array();
        // $v = app('App\Http\Controllers\v2019\Bookmark')->getBookmarkId($request);
        // $bookmarks = $v->getData()->data;
        if ($request->has('eventID') && $request->input('eventID')!='')
            $event_id = $request->input('eventID');
        else 
            $event_id = '';
        
        if ($request->has('debug') && $request->input('debug') !='')
            $debug = $request->input('debug');
        else 
            $debug = '';
        
        // if ($request->has('bookmarked') && $request->input('bookmarked')!='')
        //     $is_bookmarked = $request->input('bookmarked');
        // else 
            $is_bookmarked = 0;
		     
        //$result = \apiQuery::getSpeakersRowSet($event_id,'',$bookmarks,$is_bookmarked);
        
        $result = \apiQuery::getSpeakersRowSet_new($event_id, $debug);
        $code = 1001;
        $message = "Success";
        
        return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }

	public function getEventSpeakerDetails(Request $request)
    {      

	   $result = array();
	  
		if ($request->has('eventID') && $request->input('eventID')!='')
		  $event_id = $request->input('eventID');
		else 
		  $event_id = '';
		
		if ($request->has('speakerID') && $request->input('speakerID')!='')
		  $speaker_id = $request->input('speakerID');
		else 
		  $speaker_id = ''; 
		     
		
		   $result = \apiQuery::getSpeakersRowSet($event_id,$speaker_id);
		   $code = 1001;
		   $message = "Success";
		
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }

    public function getEventAttendeeListing(Request $request)
    {      
		 $result = array();
		 $v = app('App\Http\Controllers\v2019\Bookmark')->getBookmarkId($request);
		 $bookmarks = $v->getData()->data;
	   //include_once "/home/steelmin/public_html/api/storage/v2019/event/attendees_list.php";
		  if ($request->has('eventID') && $request->input('eventID')!='')
		   $atnd_id = $request->input('eventID');
		  else 
		   $atnd_id = '';
		$memid=0;
		$memid=$v->getData()->status->input->memId;
		//print_r ($memid);
			if ($request->has('bookmarked') && $request->input('bookmarked')!='')
			$is_bookmarked = $request->input('bookmarked');
			else 
			$is_bookmarked = 0;
		     
	
		   //$result = \apiQuery::getParticipantRowSet($atnd_id,$bookmarks,$is_bookmarked,$memid);
		   $result = \apiQuery::getParticipantRowSet_new($atnd_id,$bookmarks,$is_bookmarked,$memid);
		   
		   $code = 1001;
		   $message = "Success";

	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }

    public function getEventSponsor(Request $request)
    {      
	   $result = array();
	   //include_once "/home/steelmin/public_html/api/storage/v2019/event/sponsor_list.php";
		if ($request->has('eventID') && $request->input('eventID')!='')
		  $event_id = $request->input('eventID');
		else 
		    $event_id = '';
		     
		   //commented by tarun on 10-july-2024 to get data from json instead of querying the tables
		   //$result = \apiQuery::getSponsorOrExibRowSet($event_id);
		   
		   $result = \apiQuery::getSponsorOrExibRowSet_new($event_id, 'sponsor');
		   $code = 1001;
		   $message = "Success";

	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> isset($result["sponsors"])?$result["sponsors"]:[]));
    }

    public function getEventExhibitor(Request $request)
    {      
	   $result = array();
	   //include_once "/home/steelmin/public_html/api/storage/v2019/event/exhibitor_list.php";
		if ($request->has('eventID') && $request->input('eventID')!='')
		  $event_id = $request->input('eventID');
		else 
		  $event_id = '';
		  
            //commented by tarun on 10-july-2024 to get data from json instead of querying the tables
		   //$result = \apiQuery::getSponsorOrExibRowSet($event_id);
		   
		   $result = \apiQuery::getSponsorOrExibRowSet_new($event_id, 'exhibitor');
		   
		   $code = 1001;
		   $message = "Success";

	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> isset($result["exhibitors"])?$result["exhibitors"]:[]));
		}

     public function getEventBookmarkTabs(Request $request)
     {      
		 $result = array();
	   //include_once "/home/steelmin/public_html/api/storage/v2019/event/get_bookmarked.php";
		 if ($request->has('section') && $request->input('section')!='')
		  $section = $request->input('section');
		 else 
			$section = '';
			
		switch($section){
			 case "event":
			      return $this->getEventListing($request);
			 break;

			 case "schedule":
            return $this->getEventSchedule($request);
			 break;

			 case "speaker":
            return $this->getEventSpeakerListing($request);
			 break;

			 case "delegate":
			      return $this->getEventAttendeeListing($request);
			 break;

			 default: 
       
		 }	

    }

    public function getEventAccommodation(Request $request)
    {      
	   $result = array();

		if ($request->has('eventID') && $request->input('eventID')!='')
		  $event_id = $request->input('eventID');
		else 
		  $event_id = '';
		  
		//$result = \apiQuery::getAccomodationRowSet($event_id);
		$result = \apiQuery::getAccomodationRowSet_new($event_id);
		$code = 1001;
		$message = "Success";

	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }

    public function getEventQRdetails(Request $request)
    {      
	   $result = array();
       $qiid = $request->input("qrCodeId");
			$result = \apiQuery::getQrDetails($qiid);
		   $code = 1001;
		   $message = "Success";
	     return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
		}
		
    public function saveScannedContact(Request $request)
    {      
	   $result = array();

		  if ($request->has('qr') && $request->input('qr')!='')
		  $qr = $request->input('qr');
		else 
		  $qr = '';

		  
        $file = fopen("/home/steelmin/public_html/api/pre_function/test.txt", "a");
        fwrite($file, "\n");
        fwrite($file, "\n");
        fwrite($file, json_encode($request));
        fwrite($file, "\n");
        fwrite($file, $user_id);
        fwrite($file, "\ndone1" );
        fwrite($file, "\n");
        fclose($file);
            
            
		 $memId = \App\Helper::getMemId();
		 foreach($qr as $k=>$v){
			 $result[] = \apiQuery::saveQrDetails($memId,$v);
		 }
	   $code = 1001;
	   $message = "Success"; 
	   //return new JsonResponse(array("code" => $code, "message" => $message));
	    return new JsonResponse(array("status"=>array("code" => $code, "message" => $message),"data"=>$result));
    }

    public function getScannedContacts(Request $request)
    {      
		
	  /* $result = array();
	   include_once "/home/steelmin/public_html/api/storage/v2019/event/get_scanned_contacts.php";
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
           // $auth = new authentication();
           $token = $request->input('accessToken');
           if(empty($token)){
				///not loggedin/visitor///
				$code = 2013;
				$message = "User not found.";
			}else{
				///full data///
				$code = 1001;
				$message = "Success";
			}
        }else{
			///not loggedin/visitor///
			
			$code = 1001;
			$message = "Success";
		}
		 return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));*/
		$user_id = 0;
        $event_id = 0;
        $member_ids = array();
        $contacts = array();
        $user_id = \App\Helper::getMemId();
	
        /*if($request->has('accessToken')) {
            $token = $request->input('accessToken');
            $auth = new authentication();
            $user_id = $auth->authToken($token,$this->appType);
            if(!$user_id) {
                return new JsonResponse(array("status#" => '1001', "message#" => "Unauthorized action. Token access denied"), 401);
            }
        } else {
            return new JsonResponse(array("status#" => '1002', "message#" => "Unauthorized action. Token access denied"), 400);
		}*/

        if($request->has('eventID')) {
            $event_id = $request->input('eventID');
        }

        //if($event_id) {
				
            $sql = "SELECT m.`id`, se.`event_name`,se.id AS `event_id`,  m.`job_title` as designation, m.`company` as company_name, m.`company_profile`, m.`country`,m.profile_pic AS imageUrl";
            $sql .= ", CASE";
            $sql .= " WHEN m.billing_name = '' THEN m.name";
            $sql .= " ELSE m.billing_name";
            $sql .= " END as name";
            $sql .= ", CASE";
            $sql .= " WHEN m.`send_email_to` = 'R' THEN m.`email`";
            $sql .= " ELSE m.`addtnl_email`";
            $sql .= " END as email";
            $sql .= ", CASE";
            $sql .= " WHEN m.`send_sms_to` = 'R' THEN m.`phoneno`";
            $sql .= " ELSE m.`alternate_mob_no`";
            $sql .= " END as phoneno";
            $sql .= ", c.`country_code` as flag";
						$sql .= ", m.`isd_code` ";
						//$sql .= ", (select `event_name` from tbl_sm_events_events where id = $event_id) as eventName ";
						$sql .= ", m.`linkedin` ";
            $sql .= ", (SELECT sm.`pRemarks` FROM `tbl_sm_events_QRcode_contacts` sm WHERE sm.memid = " . $user_id . " AND sm.`contact_id` = m.`id`  LIMIT 1) as remark";
            $sql .= ", (SELECT sm.`created_at` FROM `tbl_sm_events_QRcode_contacts` sm WHERE sm.memid = " . $user_id . " AND sm.`contact_id` = m.`id`  LIMIT 1) as `timestamp`";

            $sql .= " FROM `tbl_members` m";
            $sql .= " LEFT JOIN `tbl_state_city` sc ON(m.`country` = sc.`country_id` AND m.`state` = sc.`state_id` AND m.`city` = sc.`city_id`)";
            $sql .= " LEFT JOIN `tbl_country` c ON(m.`country` = c.`id`)";
            $sql .= " LEFT JOIN `tbl_sm_events_QRcode_contacts` sm ON sm.`contact_id` = m.`id`";
            $sql .= " LEFT JOIN `tbl_sm_events_events` se ON sm.`event_id` = se.`id`";
            $sql .= " WHERE sm.memid = $user_id";
           // $sql .= " WHERE m.`id` IN (SELECT sm.`contact_id` FROM `tbl_sm_events_QRcode_contacts` sm WHERE sm.`memid` = " . $user_id .")";

            //$sql .= " GROUP BY m.`id`";
			//$sql .= " ORDER BY sm.`id` DESC";
			if($user_id == "99998")
			echo $sql;
			
            $results = DB::select($sql);
            $contacts = array();
					
            if($results) {
                foreach($results as $result) {

                    if(strlen($result['location']) <= 3) {
                        $location = strtoupper($result['location']);
                    } else {
                        $location = ucwords(strtolower($result['location']));
                    }
					if( $result['country'] == 113 || $result['country'] == 0 )
						$ph = "+91" . $result['phoneno'];
					else
						$ph = $result['isd_code'] . $result['phoneno'];
                    
					 $remarks = trim($result['remark']);
					if($this->isBase64Encoded($remarks)){
						$remarks = utf8_decode(base64_decode($remarks));
					}
						
					/* if ( base64_encode(base64_decode($remarks)) === ){
						$remarks =  base64_decode($remarks) . ' is valid :: '.$remarks;
					} else {
						$remarks .= base64_encode(base64_decode($remarks));
					} */
					if($result['event_id'] == "73" || $result['event_id'] == "76")
						$result['event_name'] = "Iron Ore & Coal Conference 2022";
					$contacts[] = array(
                        'contactID'             =>  intval($result['id']),
                        'name'              =>  removeMSWordattributes(html_entity_decode($result['name'],ENT_QUOTES)),
                        'company'      =>  removeMSWordattributes(html_entity_decode($result['company_name'],ENT_QUOTES)),
                        //'company_profile'   =>  $result['company_profile'],
                        //'location'          =>  $location,
                        'phoneno'           =>  $ph,
                        'emailId'             =>  removeMSWordattributes(html_entity_decode($result['email'],ENT_QUOTES)),
						'imageUrl' => 		"https://www.steelmint.com/images/clients/".$result['imageUrl'],			 
						'metAtEvent'           =>  $result['event_name']!=null?$result['event_name']:"",
						'eventID' => $result['event_id'],
						'linkedIn'           =>  $result['linkedin'],
                        'designation'       =>  $result['designation'],
						'remarks'          => removeMSWordattributes(html_entity_decode(base64_decode($remarks), ENT_QUOTES)),	
						'timestamp'			=>	strtotime($result['timestamp']),
						'flag' => "https://www.steelmint.com/steeladmin/flags/".strtolower($result['flag']).".png"
                         //'pRemarks'          =>  html_entity_decode($result['remark'] , ENT_QUOTES)
										);
										
                }
            }

        //}

				$code = 1001;
				$message = "Success";				
				return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $contacts));
	}
	public function isBase64Encoded($str) 
	{
		try
		{
			$decoded = base64_decode($str, true);

			if(base64_encode(base64_decode($str, true)) === $data) {
				return true;
			}
			else {
				return false;
			}
		}
		catch(Exception $e)
		{
			// If exception is caught, then it is not a base64 encoded string
			return false;
		}

	}
	
	// public function getEventsCount(Request $request){
	// 	$result = array();
	// 	$resourceType = "APP";		
	// 	if ($request->has('resourceType'))
	// 		$resourceType = strtolower($request->input('resourceType'));
		
	//     if ($request->has('filters') && $request->input('filters')!=''){
	// 	  $filter = $request->input('filters');
	// 		if( isset($filter['app_type']) && $filter['app_type'] == 'CM'){
	// 			$filter['commodityID'] = array(401);//if coalmint then show only colamint related reports
	// 		}		  
	// 	}else 
	// 	  $filter = array();
	// 	if($resourceType=='web'){
	// 		$code = 1001;
	// 		$message = "Success";
	// 		//include_once "/home/steelmin/public_html/api/storage/v2019/web/web_report_count_post.php";
	// 		$result = \ApiQuery::eventsCatCount($filter);			
	// 	} elseif($resourceType=='app') {
	// 		///
	// 	} else{
	// 		$code = 2001;
	// 		$message = "invalid input";
	// 	}
	//    return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	// }
	public function getEventsCount(Request $request){
		$result = array();
		$resourceType = "APP";		
		$code = 200;
		$status = true;
		$message = "Success";
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
	    if ($request->has('filters') && $request->input('filters')!=''){
		  $filter = $request->input('filters');
			if( isset($filter['app_type']) && $filter['app_type'] == 'CM'){
				$filter['commodityID'] = array(401);//if coalmint then show only colamint related reports
			}		  
		}else 
		  $filter = array();
		if($resourceType=='web'){
			$code = 200;
			$status = true;
			$message = "Success";
			//include_once "/home/steelmin/public_html/api/storage/v2019/web/web_report_count_post.php";
			$result = \ApiQuery::eventsCatCount($filter);			
		} elseif($resourceType=='app') {
			$code = 200;
			$status = true;
			$message = "Success";
			$result = \ApiQuery::eventsCatCount($filter);
		} else{
			$code = 400;
			$status = false;
			$message = "invalid input";
		}
	   return new JsonResponse(array("code" => $code, "status" => $status, "message" => $message, "status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}

	public function genQR(Request $request){
		//$codeContents = '[{"memid": "83757","eventid": "20","name": "MR. DIPEN SINGH GAUTAM","email": "DS.gautam@gmail.com","phoneno": "9806139099","company_name": "Gautam Services"}]';
		$memid = $request->input("memId");
		$sql = '';
		$sql = "SELECT m.`id` as contactID, m.`company` as company_name";
            $sql .= ", CASE";
            $sql .= " WHEN m.billing_name = '' THEN m.name";
            $sql .= " ELSE m.billing_name";
            $sql .= " END as name";
            $sql .= ", CASE";
            $sql .= " WHEN m.`send_email_to` = 'R' THEN m.`email`";
            $sql .= " ELSE m.`addtnl_email`";
            $sql .= " END as email";
            $sql .= ", CASE";
            $sql .= " WHEN m.`send_email_to` = 'R' THEN m.`phoneno`";
            $sql .= " ELSE m.`alternate_mob_no`";
			$sql .= " END as phoneno";
		$sql .= " FROM `tbl_members` m where id={$memid}";
		$results = DB::select($sql);
		$results[0]["eventid"] = $request->input("eventID");
		$str = json_encode($results);
		$arr = \apiQuery::qrGen($str);
		$code = 1001;
		$message = "Success";
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $arr));
	}

	public function storeEventRegistry(Request $request)
    {
        $member_ids = '';$event_ids ='';
        $dates = '';
        $existingContactIds = array();

        if($request->has('event_ids') && !empty($request->input('event_ids'))) {
						$event_ids = rtrim(preg_replace('/\s+/', '', $request->input('event_ids')), ",");
        } else {
            return new JsonResponse(array("status#" => '1003', "message#" => "Event Id(s) required!"), 400);
        }

        if($request->has('member_ids') && !empty($request->input('member_ids'))) {
            $member_ids = rtrim(preg_replace('/\s+/', '', $request->input('member_ids')), ",");
        } else {
            return new JsonResponse(array("status#" => '1004', "message#" => "Member_Id(s) required!"), 400);
        }

        if($request->has('dates')) {
            $dates = rtrim($request->input('dates'));
				}

            $insertRows = array();
			
            $member_ids = explode(",",$member_ids);
						$event_ids = explode(",",$event_ids);
						$date = explode(",",$dates);
						$createdAt = date('Y-m-d H:i:s');
						
						foreach($event_ids as $k=>$v){
            //$sql = "SELECT `event_id`,`contact_id` FROM `tbl_event_present` WHERE `event_id`='$v' AND `contact_id`='{$member_ids[$k]}'";
						//$existingContactIdResult = DB::select($sql);
						//$numRows = count($existingContactIdResult);
						//	if($numRows <= 0) {
									$insertRows[] = "('" . $v . "', '" . $member_ids[$k] . "', '" . $date[$k] . "', now())";
						//	}
					}
					if($insertRows) {
						$sql = "INSERT INTO `tbl_event_present` (`event_id`, `contact_id`, `app_sent_date`, `created_at`) VALUES ";
						$sql .= implode(", ", $insertRows);
						DB::insert($sql);
				  }


        return new JsonResponse(array(
            "status#"               => "1" ,
            "message#"              => "successfully saved"
        ));
    }
    
     public function getEventSpeakersListing(Request $request)
    {      
	   $result = array();	  
		if($request->has('eventID') && $request->input('eventID')!='')
		  $event_id = $request->input('eventID');
		else{
            $code = 2001;
            $message = "Invalid event id.";
            return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> []));
		}
		$speaker_id='';
		if ($request->has('speaker_id') && $request->input('speaker_id')!='')
		  $speaker_id = $request->input('speaker_id');    
		
		if($request->has('getAll') && $request->input('getAll')=='Y'){
            $sel = ",job_title as designation,company,profile_pic";
            
            $order = " ORDER BY FIELD(ID,$speaker_id)";
		}
        $sql = "select m.id,
            CASE
                WHEN m.billing_name <> '' THEN TRIM(m.billing_name)
                WHEN m.name <> '' THEN TRIM(m.name)
                ELSE ''
            END AS name  $sel           
        FROM `tbl_members` m where 1";
        if(empty($speaker_id)){
            $sql .= " and id in (select registering_id from final_status where eventid=$event_id and participant_type='speaker') order by  m.`name`";
        } else {
            $sql .= " and id in (".$speaker_id.") $order";
        }
       
        
        $results = DB::select($sql);		   
        $code = 1001;
        $message = "Success";
        

	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=>$results ));
    }
    
    
     public function getEventPOCsListing(Request $request)
    {      
	   $result = array();	  
		if($request->has('eventID') && $request->input('eventID')!='')
		  $event_id = $request->input('eventID');
		else{
            $code = 2001;
            $message = "Invalid event id.";
            return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> []));
		}
		$poc_id='';
		if ($request->has('poc_id') && $request->input('poc_id')!='')
		  $poc_id = $request->input('poc_id');    
		
		
        $sql = "select id,
           rm_name as name
        FROM `tbl_rel_manager` where status='Active' ";
        if(!empty($poc_id)){
            $sql .= " and id=".$poc_id;
        }    
        $sql .= " order by rm_name ";
        $results = DB::select($sql);		   
        $code = 1001;
        $message = "Success";
        

	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=>$results ));
    }

}    
    
?>
