<?php 
	require_once('Connections/conn_ama.php');

	if(!isset($_SESSION)){
        session_start();
		} 
		if(isset($_POST['ah'])){
				$colname= $_POST['ah'];
			//echo $colname;
		}
	//Retrieve News Articles
		$query_rsNews = "SELECT newsID, ah, headline, gist, content, dateAdded  FROM v_newsarticles  WHERE ah = '".$colname."'";
		$rsNews = mysqli_query($conn_ama, $query_rsNews ) or die(mysqli_error($conn_ama));
		$row_rsNews = mysqli_fetch_assoc($rsNews);
		$totalRows_rsNews = mysqli_num_rows($rsNews);

	//Retrieve Headline(main) Image
		$query_rsNewsTopImage = "SELECT imageID, ah, `image`, thumbnail FROM articleimages WHERE ah = '".$colname."' ORDER BY imageID ASC LIMIT 1";
		$rsNewsTopImage = mysqli_query($conn_ama, $query_rsNewsTopImage ) or die(mysqli_error($conn_ama));
		$row_rsNewsTopImage = mysqli_fetch_assoc($rsNewsTopImage);
		$totalRows_rsNewsTopImage = mysqli_num_rows($rsNewsTopImage);

		//Retrieve Other News Images
		$query_rsOtherNewsImages = "SELECT imageID, ah, `image`, thumbnail FROM articleimages WHERE ah != '".$row_rsNewsTopImage['imageID']."' ORDER BY imageID ASC LIMIT 1";
		$rsOtherNewsImages = mysqli_query($conn_ama, $query_rsOtherNewsImages ) or die(mysqli_error($conn_ama));
		$row_rsOtherNewsImages = mysqli_fetch_assoc($rsOtherNewsImages);
		$totalRows_rsOtherNewsImages = mysqli_num_rows($rsOtherNewsImages);
 ?>

<!-- <div class="col-md-9 data-area"> -->

	<div class="data-area-wrap">
		<div class="form-wrapper">
			<form id="new-news-update" method="post">
			  	<fieldset>
			  		<legend>Edit News</legend>
			  		<div class="form-group">
					    <label for="headline">Headline</label>
					    <input type="text" name="headline" class="form-control" value="<?php echo $row_rsNews['headline']; ?>" id="headline" placeholder="Headline" >
				  	</div>

				  	<div class="form-group">
					    <label for="gist">Gist</label>
					    <input type="text" name="gist" class="form-control" value="<?php echo $row_rsNews['gist']; ?>" id="gist" placeholder="Gist">
				  	</div>

				  	<div class="form-group">
						<label for="elm1">News Content</label>
						<textarea name="content" class="tinymce"  id="newsupload"><?php echo $row_rsNews['content']; ?></textarea>
					  	<!-- <textarea name="content" class="tinymce" cols="" rows="" id="emlm1">
								<?php// echo $row_rsNews['content']; ?>
							</textarea> -->
				  	</div>
					</fieldset>
					<fieldset>
						<legend>Images</legend>
							<div class="form-row">
								<div class="col-4">
									<div class="imag-display">
										<h4>Current Headline Image</h4>
										<img class="img-fluid" src="_/components/php/articles-images/news/thumbs/<?php echo $row_rsNewsTopImage['thumbnail']; ?>" alt="">
										<?php echo $row_rsNewsTopImage['image']. " ".$row_rsNewsTopImage['imageID']; ?>
									</div>
								</div>
								<div class="col-4">
									<div class="form-group">
										<label for="fileUpload">Replace News Image</label>
										<input type="file" id="fileUpload" class="form-control" name="image[]">
											
									</div>
								</div>
								<div class="col-4">
										<div class="img-display">
											<h4>Replacement Image</h4>
											<div id="image-holder"></div>
										</div>
										
								</div>
							</div>
						<!-- <div class="form-row">
							<div class="col-12"><h4>Other Images</h4></div>
							<?php do { ?>
								<div class="col-4">
									<div class="imag-display">
										<img class="img-fluid" src="_/components/php/articles-images/news/thumbs/<?php echo $row_rsOtherNewsImages['thumbnail']; ?>" alt="">
										<?php echo $row_rsOtherNewsImages['image']." ".$row_rsOtherNewsImages['imageID']; ?>
									</div>
								</div>
							<?php } while($row_rsOtherNewsImages = mysqli_fetch_assoc($rsOtherNewsImages)) ?>
						</div> -->
					</fieldset>
				<hr>
				<button type="submit" id="news-update" class="btn btn-success">Submit</button>
				<input type="hidden" name="MM_imageID" value="<?php echo $row_rsNewsTopImage['imageID']; ?>" />
				<input type="hidden" name="MM_newsID" value="<?php echo $row_rsNews['newsID']; ?>" />
			  <input type="hidden" name="MM_update" value="save-news-update" />
			  <div class="error_msg"></div>
			</form>
		</div><!-- End .form-wrapper-->
	</div><!-- End .data-area-wrap-->	
<!-- </div> End .data-area -->

<!-- TinyMCE -->
        
<script type="text/javascript" src="_/js/tinymce/jquery.tinymce.min.js"></script> 
<script type="text/javascript" src="_/js/tinymce/tinymce.min.js"></script>
                        
<!-- TinyMCE -->
        
            
	<script type="text/javascript">
		$(function(){
			tinymce.init({
			selector: 'textarea#newsupload',
			height: 500,
			content_css: [
			'//fonts.googleapis.com/css?family=Lato:300,300i,400,400i',
			'//www.tiny.cloud/css/codepen.min.css'
			]
		});
		});
	</script>
<!-- /TinyMCE -->
<script>
	$(document).on('click','.delete-btn',function(e){
		e.preventDefault();
				
		//alert("form");
		$(this).parent('div').fadeOut();
				
	});

	$("#fileUpload").on('change', function () {
		//alert("askfljsd");
		//Get count of selected files
		var countFiles = $(this)[0].files.length;

		var imgPath = $(this)[0].value;
		var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
		var image_holder = $("#image-holder");
		image_holder.empty();

		if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
			if (typeof (FileReader) != "undefined") {

				//loop for each file selected for uploaded.
				for (var i = 0; i < countFiles; i++) {

					var reader = new FileReader();
					reader.onload = function (e) {
						$("<img />", {
							"src": e.target.result,
								"class": "thumb-image img-fluid"
						}).appendTo(image_holder);
					}

					image_holder.show();
					reader.readAsDataURL($(this)[0].files[i]);
				}

			} else {
				alert("This browser does not support FileReader.");
			}
		} else {
			alert("Pls select only images");
		}
	});	
</script> 
<!-- /TinyMCE -->
