<?php
include "header.php";

if($post_url == "manage-news" || empty($post_url)):
?>

<div id="content" class="main-content">

	<div class="layout-px-spacing">
		<div class="row layout-top-spacing" id="cancel-row">
  
			<div class="col-xl-12 col-lg-12 col-sm-12 layout-spacing m-auto">
			
			<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 ">
				<h1 class="title-4"> News </h1>
				<div class="btn-toolbar mb-2 mb-md-0">
					<div class="btn-group mr-2">
					
						<a href="?preg=post-news" class="btn btn-icon bg-primary text-white">
						<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-plus-circle"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="16"></line><line x1="8" y1="12" x2="16" y2="12"></line></svg>
						&nbsp; Add News </a>
						
					</div>
				</div>
			</div>
	
			
			<div class="row">
				<div class="col-xl-12 col-lg-12 col-sm-12 m-auto">
							
				<div class="widget-content widget-content-area br-6">	
					
					<div class="table-responsive mb-4 mt-4">
						<table id="events_tbrecs" class="table table-stripped caption-top" style="width:100%">
							
							<thead>
								<tr>
									<th width="40%">Title</th>
									<th width="15%" class="text-center vertical-align">Post By</th>
									<th width="15%" class="text-center vertical-align">Post Date</th>
									<th width="15%" class="text-center vertical-align">Date</th>
									<th width="15%" class="text-center vertical-align"  data-sortable="false">Action</th>
								</tr>
							</thead>
						</table>
					</div>
					
						
				</div>
				</div>	
				</div>
		
			</div>

		</div>

	</div>

</div>
  
<script type="text/javascript" language="javascript" >

$(document).ready(function(){

	var userdataTable = $('#events_tbrecs').DataTable({
		"processing": true,
		"serverSide": true,
		"order": [],
		"ajax":{
			url:"actions/fetch_news.php",
			type:"POST"
		},
		"oLanguage": {
                "oPaginate": { "sPrevious": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-left"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>', "sNext": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>' },
                "sInfo": "Showing _START_ to _END_ of _TOTAL_ News",
                "sSearch": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>',
                "sSearchPlaceholder": "Search title...",
               "sLengthMenu": "Results :  _MENU_",
				"sInfoFiltered": "",
            },
		
		"stripeClasses": [],
		"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
		"pageLength": 10,
		drawCallback: function () { $('.dataTables_paginate > .pagination').addClass(' pagination-style-13 pagination-bordered mb-5'); }
	
	});
	
	
	
	   $(document).on('click', '.postfeature', function(){
		
		var id = this.id;   var split_id = id.split("_"); var text1 = split_id[0]; var text2 = split_id[1];
		
	swal({
      title: 'Are you sure?', text: "This Post to be added to Featured News", type: 'warning', showCancelButton: true, confirmButtonText: 'Yes', padding: '2em' }).then(function(result) {
      if (result.value) {
	$.ajax({ type:'POST', url:'actions/create_news.php',data:{ 'addfetures':1, 'featurePost':text2, },
	dataType:"json", success:function(data){ donejob(data.runstt,data.errorreason); if(data.resv != ""){ $("#cd_"+text2).addClass(data.resv); }
    } }); }  });   });
	
})

</script>	
	
<?php
elseif($post_url == "post-news" || $post_url == "edit-news"):

unset($_SESSION['prdss']);

$prd_ft = "";
if(!empty($prd))
{
	$chk_prd_rc = $db_con->prepare("SELECT * FROM newspost WHERE nw_id = '$prd' ");
	$chk_prd_rc->execute();
	$prd_ft = $chk_prd_rc->fetch();	 
	$prd_count = $chk_prd_rc->rowCount();
		
	if($prd_count == 0)
	{
		echo "<script>window.location.href ='news?preg=manage-news'</script>";
	}
}
?>
<link rel="stylesheet" href="assets/css/dropzone.css">

 <script src="https://cdn.tiny.cloud/1/1mb1rf3spybvgrr1u39vhym6nvfqlmkb7qgm9ym9fixbr3n6/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>
 
 <script>
    tinymce.init({
      selector: 'textarea#pstdescription', menubar: false, plugins: 'link image code autolink lists media table ',
	  toolbar: 'undo redo | h1 h2 h3 | blockquote | underline strikethrough superscript subscript | bold italic casechange | alignleft aligncenter alignright alignjustify | numlist checklist outdent indent | link image table code',
	  images_upload_url: 'postloadimage.php', toolbar_mode: 'floating', tinycomments_mode: 'embedded', tinycomments_author: 'Author name', height : 500,
   });
  </script>
  
<div id="content" class="main-content">

	<div class="layout-px-spacing">
		<div class="row layout-top-spacing" id="cancel-row">
  
			<div class="col-xl-12 col-lg-12 col-sm-12 layout-spacing m-auto">
			
			<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 ">
				<h1 class="title-4"> Post News</h1>
				<div class="btn-toolbar mb-2 mb-md-0">
					<div class="btn-group mr-2">
					
						<a href="?preg=manage-news" class="btn btn-icon bg-primary text-white">
						<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-copy"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>
						&nbsp; All News</a>
						
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xl-10 col-lg-10 col-sm-12 m-auto">
				
				<div class="widget-content widget-content-area br-6">	
					
					<form class="text-left" method="post" action="" id="mkpost_form" autocomplete="" enctype="multipart/form-data">
					
							
						<div class="widget-content widget-content-area shadow-none">
							
							<div class="row">
							    <div class="col-md-12 form-group nw-input">
									<label class="text-dark font-weight-bold">Title <code>*</code>  </label>
									<input type="text" name="posttitle" class="form-control" required value="<?= (!empty($prd_ft)) ? $prd_ft['nwtitle']:''; ?>"/>
									<span id="" class="error_here"></span>
							    </div>
							</div>
							
							
							<?= (!empty($prd_ft)) ? '<input type="hidden" name="pstrec" value="'.$prd_ft['nw_id'].'" />' : ''; ?>
						
							<div class="row">
							    
								<div class="col-md-4 form-group nw-input">
									<label class="text-dark font-weight-bold">Upload Image <code>*</code></label>
									<input type="file"  name="pics_a"  id="pic_a" class="form-control for_image" accept="image/jpeg, image/png, image/jpg" <?= (!empty($prd_ft)) ? '':'required'; ?> />
									<span id="img_error" class="error_here"></span>
								</div>
								
								<div class="col-md-4 form-group nw-input">
									<label class="text-dark font-weight-bold">Post Date <code>*</code>  </label>
									<input type="date" name="pstdate" class="form-control" required value="<?= (!empty($prd_ft)) ? date('Y-m-d', strtotime($prd_ft['nwdate'])) :  date('Y-m-d'); ?>"/>
									<span id="" class="error_here"></span>
								</div>
								
								<div class="col-md-4 form-group nw-input">
									<label class="text-dark font-weight-bold">Post By <code>*</code>  </label>
									<input type="text" name="pspost" class="form-control" required value="<?= (!empty($prd_ft)) ? $prd_ft['nwby']:''; ?>" placeholder="who composed this news?" />
									<span id="" class="error_here"></span>
								</div>
							</div>
							
							<input type="hidden" id="setbk" value="<?= $post_url; ?>" name="post_info" />
							
							<div class="form-group nw-input">
								<label class="text-dark font-weight-bold">Preview Post <code>*</code> </label>
								<textarea name="postbrief" class="form-control" id="brief" required maxlength="150"><?= (!empty($prd_ft)) ? $prd_ft['newsbrief']:''; ?></textarea>
								<span id="" class="error_here"></span>
							</div>
							
							<div class="form-group nw-input">
								<label class="text-dark font-weight-bold">Description</label>
								<textarea name="postdescription" class="form-control" id="pstdescription" ><?= (!empty($prd_ft)) ? $prd_ft['post_news']:''; ?></textarea>
								<span id="" class="error_here"></span>
							</div>
							
							
									
											
						<div class="row form-group mt-3">
							<div class="col-md-12 text-center">
								<button type="submit" class="btn btn-primary"  style="position:absolute; z-index:-1; opacity:0.3" id="button_dosubs">Submit</button>
							</div>
						</div>
					</form>
					
					<input type="hidden" id="pcmgct" value="0" />
						
					<div class="form-submit galrfrm">	
						<h3>Gallery </h3>
						<div class="submit-section">
							<div class="row">
							
								<div class="form-group col-md-12">
									<label>Upload Pictures <span id="imgsrr" class="ms-3 text-danger"></span></label>
									<form action="../image-add.php?action=save" id="drpzn" class="dropzone dz-clickable primary-dropzone">
										<div class="dz-default dz-message">
											<i class="far fa-image"></i>
											<span>Drag & Drop To Add Post Images</span>
										</div>
									</form>
								</div>
								
							</div>
						</div>
					</div>	
					
					
					<div class="form-group col-lg-12 col-md-12 text-center">
						<button class="btn btn-theme-light-2 rounded w-50 border-primary text-primary" id="button_tbpst" type="button">Submit</button>
					</div>
				</div>
				</div>	
			</div>
		
			</div>

		</div>

	</div>

</div>
  
<script src="assets/js/dropzone.js"></script>



<script type="text/javascript" language="javascript" >

$("#button_tbpst").on('click', function(e) { e.preventDefault(); if( $("#pcmgct").val() == 0){ $("#imgsrr").text("Images required"); } $("#button_dosubs").trigger('click'); /*$( "#mkpost_form" ).submit();*/ });
Dropzone.options.drpzn = { maxFilesize: 5, acceptedFiles: 'image/*', init: function (){ this.on("complete", function (file) { var mkscf = this.getAcceptedFiles().length; $("#pcmgct").val(mkscf); if(mkscf > 0){$("#imgsrr").text(''); } }); }};

/*
$(document).ready(function(){

$("#mkpost_form").on('submit',function(e) {
		e.preventDefault();	 
		
		var rev = $('#mkpost_form');
		
			$.ajax({
				url: 'actions/create_news.php',
				type: 'POST',
				data:  new FormData(this),
				contentType: false,
				cache: false,
				processData:false,
				dataType:"json",
				beforeSend: function()
				{	
					$("#button_tbpst").html('submiting <span class="spinner-grow-sm text-white ml-2 align-self-center loader-sm"></span>');
					$("#button_tbpst").attr('disabled', 'disabled');
				},
				success: function(data)
					{
						donejob(data.runstt,data.errorreason);
						$("#button_tbpst").html('Submit');
						$("#button_tbpst").attr('disabled', false);
						
						if(data.runstt == "grant" && $("#setbk").val() == "post-news"){
							rev.trigger('reset');	
						}
						else if(data.errorform != "")
						{
							$("#pic_"+data.errorform).siblings("span").text(data.errorreason);
							
						}
					}
			});
    });	
})*/

$('document').ready(function(){
	
	$("#mkpost_form").validate({
		rules:
		{posttitle: {required: true}, <?= (!empty($prd_ft)) ? '':'pics_a: {required: true},'; ?>pstdate: {required: true},pspost: {required: true},postbrief: {required: true} },
		messages:{posttitle: {required: "Enter post title"}, <?= (!empty($prd_ft)) ? '':'pics_a: {required: "Image Required"},'; ?>pstdate: {required: "Post Date Required"},
		pspost: {required: "Enter Content User"},postbrief: {required: "Post Brief Required"} }, errorPlacement : function(error, element) {  
		$(element).closest('.nw-input').find('.error_here').html(error.html()); },highlight : function(element) {  
		$(element).closest('.nw-input').removeClass('has-success').addClass('has-error'); },unhighlight: function(element, errorClass, validClass) {
		$(element).closest('.nw-input').removeClass('has-error').addClass('has-success'); $(element).closest('.nw-input').find('.error_here').html(''); },
		submitHandler: submitForm
		});
	function submitForm(){
    var mkfrm = new FormData($('#mkpost_form')[0]);
	
	$.ajax({type : 'POST', url  : 'actions/create_news.php', data: mkfrm, processData:false, contentType: false,
	beforeSend: function() { $('#button_tbpst').attr('disabled', 'disabled'); $("#button_tbpst").html('submiting <span class="spinner-grow-sm text-white ml-2 align-self-center loader-sm"></span>'); }, 
	dataType:"json", success :  function(data) { donejob(data.runstt,data.errorreason); $('#button_tbpst').attr('disabled', false); $("#button_tbpst").html('Submit');
	if(data.runstt == "grant" && $("#setbk").val() == "post-news"){ $("#mkpost_form").trigger('reset'); $('#drpzn').removeClass("dz-started");
	$('#drpzn').html('<div class="dz-default dz-message"><i class="far fa-image"></i><span> Drag &amp; Drop To Add Post Images</span></div>');	 }
	else if(data.errorform != ""){	$("#pic_"+data.errorform).siblings("span").text(data.errorreason); }
	} }); return false; } 
		
});
</script>

<script src="assets/js/validation.min.js" type="text/javascript"></script>
<script src="assets/js/jquery.validate.min.js"></script>


</div>
<?php
endif;
include "footer.php"; 
?>
