<?php
include_once "../../includes/connectConfig.php";

	if($_POST)
	{	
		if(isset($_POST['addfetures'])):
		
			$featurePost = test_input($_POST['featurePost']);
			
			$gtPost = gtCount("page_selected","status = 'upcoming_event' AND delete_status = 0");
			if($gtPost == 5)
			{
				$output["runstt"] = "warant";
				$output["errorreason"] = "Limit Reached for Featured Upcoming Events";
				echo json_encode($output);
				exit;
			}
			
			$chkPstdel = getOne('page_selected'," post_id = '$featurePost' AND status = 'upcoming_event' AND delete_status = 0",'id');
			if(!empty($chkPstdel))
			{
				$output["runstt"] = "grant";
				$output["errorreason"] = "Event Already Featured.";
				echo json_encode($output);
				exit;
			}
			
			$chkPstNdel = getOne('page_selected'," post_id = '$featurePost' AND status = 'upcoming_event' AND delete_status = 1",'id');
			if(!empty($chkPstNdel))
			{
				$prd = $db_con->prepare("UPDATE page_selected SET delete_status = 0 WHERE post_id =:pstrec ");
				$prd->bindParam(':pstrec',$featurePost);
				$prd->execute();
			
				$output["runstt"] = "grant";
				$output["errorreason"] = "Event Featured Successfully";
				echo json_encode($output);
				exit;
			}
			
			$prd = $db_con->prepare("INSERT INTO page_selected(post_id,status) VALUES(:pstrec,'upcoming_event')");
			$prd->bindParam(':pstrec',$featurePost);
			$prd->execute();
			
			$output["runstt"] = "grant";
			$output["errorreason"] = "Event Featured Successfully";
			echo json_encode($output);
			exit;
		
		else:
		
			$valid_extensions = array('png','jpg','jpeg');

			$namea = $_FILES['pics_a']['name'];
			$sizea = $_FILES['pics_a']['size'];
			$extsa = strtolower(pathinfo($namea, PATHINFO_EXTENSION));

			if(!empty($namea) && !(in_array($extsa, $valid_extensions)) || $sizea > 9000000)
			{
				$output["runstt"] = "warant";
				$output["errorform"] = "a";
				$output["errorreason"] = "Check file type or size";
				echo json_encode($output);
				exit;
			}
			else
			{
			
				$folder = "../../img/shield-events/";  
				$num_rn = substr(md5(uniqid(rand())), 0, 7 );
				
				$pstrec = (isset($_POST["pstrec"])) ? test_input($_POST["pstrec"]) : '';
				$post_title = test_input($_POST["posttitle"]);
				$eventdate = test_input(date('Y-m-d H:i:a', strtotime($_POST["eventdate"].$_POST['nwstime'])));
				$eventvenue = test_input($_POST["eventvenue"]);
				$briefpost =  test_input($_POST['brief']);
				$de_scpost =  $_POST['description'];
				$p_url = php_url_slug($post_title.'-'.date('jMy', strtotime($eventdate)));
				
				$imga = "";
				if(!empty($namea))
				{
					$tmpa = $_FILES['pics_a']['tmp_name'];
					list($txta, $exta) = explode(".", $namea);
					$imga = strtolower($num_rn.'_'.rand(1000,100000).'.'.$exta);
					$filePatha = $folder.'/'.$imga;
					
					move_uploaded_file($tmpa, $filePatha);
				}
				
				
				
				/*****************************************************/
				
				$mediamode = test_input($_POST['mediamode']);
				
				if($mediamode == "File")
				{
					$valid_extensions = array('mp4','avi','mov');

					$namev = $_FILES['pics_ab']['name'];
					$sizev = $_FILES['pics_ab']['size'];
					$extsv = strtolower(pathinfo($namev, PATHINFO_EXTENSION));
			
					if(!empty($namev) && !(in_array($extsv, $valid_extensions)))
					{
						$output["errorform"] = "ab";
						$output["runstt"] = "warant";
						$output["errorreason"] = "Check file type or size";
						echo json_encode($output);
						exit;
					}
					else
					{
						$tmpv = $_FILES['pics_ab']['tmp_name'];
						list($txtv, $extv) = explode(".", $namev);
						$imgv = strtolower($num_rn.'_'.rand(1000,100000).'.'.$extv);
						$filePathv = $folder .'/'.$imgv;
						move_uploaded_file($tmpv, $filePathv);
					}
					
					$nw_img = $imgv;
					$sta_tus = "video";
				}
				elseif($mediamode == "Video")
				{
					$nw_img = test_input($_POST['post_rec']);
					$sta_tus = "url";
				}
				else{
					$nw_img = NULL;
					$sta_tus = NULL;
				}
				
				
				/*****************************************************/
			

				if($_POST['post_info'] == "post-event")
				{
					//INSERT INTO TABLE
					$prd = $db_con->prepare("INSERT INTO eventspost(eventtitle, eventimg, eventdate, eventvenue, preview_brief, post_note, posturl, mediastt, mediarec) 
																	VALUES(:post_title, :imga, :eventdate, :eventvenue, :briefpost, :pstnt, :p_url, :p_stt, :p_clp)");
					
					$prd->bindParam(':post_title',$post_title);
					$prd->bindParam(':imga',$imga);
					$prd->bindParam(':eventdate',$eventdate);
					$prd->bindParam(':eventvenue',$eventvenue);
					$prd->bindParam(':briefpost',$briefpost);
					$prd->bindParam(':pstnt',$de_scpost);
					$prd->bindParam(':p_url',$p_url);
					$prd->bindParam(':p_stt',$sta_tus);
					$prd->bindParam(':p_clp',$nw_img);
					$prd->execute();
					
					$output["runstt"] = "grant";
					$output["errorreason"] = "Event Posted Successfully";
					echo json_encode($output);
					exit;
				}
				elseif($_POST['post_info'] == "edit-event")
				{
					$imgnm = (!empty($namea) && $imga != "") ? $imga : getOne('eventspost'," evn_id = '$pstrec' ",'eventimg');
					
					//INSERT INTO TABLE
					$prd = $db_con->prepare("UPDATE eventspost SET eventtitle=:post_title, eventimg=:imga, preview_brief=:briefpost, post_note=:pstnt,
																		eventdate=:eventdate, eventvenue=:eventvenue, posturl=:p_url, mediastt=:p_stt, mediarec=:p_clp  WHERE evn_id=:pstrec");
					
					$prd->bindParam(':post_title',$post_title);
					$prd->bindParam(':imga',$imgnm);
					$prd->bindParam(':eventdate',$eventdate);
					$prd->bindParam(':eventvenue',$eventvenue);
					$prd->bindParam(':briefpost',$briefpost);
					$prd->bindParam(':pstnt',$de_scpost);
					$prd->bindParam(':p_url',$p_url);
					$prd->bindParam(':p_stt',$sta_tus);
					$prd->bindParam(':p_clp',$nw_img);
					$prd->bindParam(':pstrec',$pstrec);
					$prd->execute();
					
					
					$output["runstt"] = "grant";
					$output["errorreason"] = "Event Data Updated";
					echo json_encode($output);
					exit;
				}
				else
				{
					$output["runstt"] = "warant";
					$output["errorreason"] = "";
					echo json_encode($output);
					exit;
				}
			}
		endif;
	}
?>