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

	if($_POST)
	{	
			/*
			$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-announcements/";  
				$num_rn = substr(md5(uniqid(rand())), 0, 7 );
				
				$pstrec = (isset($_POST["pstrec"])) ? test_input($_POST["pstrec"]) : '';
				$post_title = test_input($_POST["posttitle"]);
				$annou_cedate = test_input(date('Y-m-d', strtotime($_POST["annoucedate"])));
				$expir_ydate = test_input(date('Y-m-d', strtotime($_POST["expirydate"])));
				$anounc_ebrief =  test_input($_POST['anouncebrief']);
				//$p_url = php_url_slug($post_title.'-'.date('jMy', strtotime($annou_cedate)));
				
				$imga = NULL;
				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);
				}
			

				if($_POST['post_info'] == "post-announcement")
				{
					//INSERT INTO TABLE
					$prd = $db_con->prepare("INSERT INTO announcedpost(anp_title, anp_img, anp_date, anp_expiry, anp_info)
																	VALUES(:post_title, :imga, :an_st, :an_ex, :briefpost)");
					
					$prd->bindParam(':post_title',$post_title);
					$prd->bindParam(':imga',$imga);
					$prd->bindParam(':an_st',$annou_cedate);
					$prd->bindParam(':an_ex',$expir_ydate);
					$prd->bindParam(':briefpost',$anounc_ebrief);
					$prd->execute();
					
					$output["runstt"] = "grant";
					$output["errorreason"] = "Announcement Posted Successfully";
					echo json_encode($output);
					exit;
				}
				elseif($_POST['post_info'] == "edit-announcement")
				{
					//$imgnm = (!empty($namea) && $imga != "") ? $imga : getOne('announcedpost'," ap_id = '$pstrec' ",'anp_img');
					
					//INSERT INTO TABLE
					$prd = $db_con->prepare("UPDATE announcedpost SET anp_title=:post_title, anp_img=:imga, anp_date=:an_st, anp_expiry=:an_ex, anp_info=:briefpost WHERE ap_id=:pstrec");
					
					$prd->bindParam(':post_title',$post_title);
					$prd->bindParam(':imga',$imga);
					$prd->bindParam(':an_st',$annou_cedate);
					$prd->bindParam(':an_ex',$expir_ydate);
					$prd->bindParam(':briefpost',$anounc_ebrief);
					$prd->bindParam(':pstrec',$pstrec);
					$prd->execute();
					
					
					$output["runstt"] = "grant";
					$output["errorreason"] = "Post Updated";
					echo json_encode($output);
					exit;
				}
				else
				{
					$output["runstt"] = "warant";
					$output["errorreason"] = "";
					echo json_encode($output);
					exit;
				}
			
	}
?>