<?php include "header.php"; 
?>

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

	<div class="layout-px-spacing">
		<div class="row layout-top-spacing" id="cancel-row">
		<?php
		if($setup == "add-post" || $setup == "edit-post")
		{
			$prd_ft = ""; $gt_ads = "";
			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-posts'</script>"; }
				
			}	
		?>
		<link href="assets/dropzone.css" rel="stylesheet" type="text/css" />
		<script src="https://cdn.tiny.cloud/1/1mb1rf3spybvgrr1u39vhym6nvfqlmkb7qgm9ym9fixbr3n6/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>
		<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"><?= ($setup == "add-post") ? 'Add' : 'Edit'; ?> Post</h1>
			
			<div class="btn-toolbar mb-2 mb-md-0">
				<div class="btn-group mr-2">
					<a href="?setup=manage-stock" class="btn btn-icon bg-warning 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-grid"><rect x="3" y="3" width="7" height="7"></rect><rect x="14" y="3" width="7" height="7"></rect><rect x="14" y="14" width="7" height="7"></rect><rect x="3" y="14" width="7" height="7"></rect></svg>
					&nbsp; Manage Posts </a>
				</div>
			</div>
		</div>
		
		<form class="text-left" method="post" action="" id="mkpost_form" autocomplete="" enctype="multipart/form-data">
		<div class="row">
			<div class="col-md-10 mx-auto">
				<div class="widget-content widget-content-area bg-light br-6 mb-3">	
					<h5 class="mb-4 font-weight-bold text-warning">Product Info</h5>
					<div class="row">
						<div class="col-md-12 form-group">
							<label class="text-dark font-weight-bold">Product Name <code>*</code>  </label>
							<input type="text" name="jewtitle" class="form-control" required value="<?= (!empty($prd_ft)) ? $prd_ft['nwtitle']:''; ?>"/>
							<span id="" class="error_here"></span>
						</div>
						<?= (!empty($prd_ft)) ? '<input type="hidden" name="pstrec" value="'.$prd_ft['nw_id'].'" />' : '' ?>
						<div class="col-md-12 form-group">
							<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-12 form-group">
							<label class="text-dark font-weight-bold">Brief Post <code>*</code> </label>
								<textarea name="dsbrief" class="form-control" id="brief" required maxlength="280"><?= (!empty($prd_ft)) ? $prd_ft['newsbrief']:''; ?></textarea>
								<span id="" class="error_here"></span>
						</div>
						
						<input type="hidden" value="<?= ($setup == "edit-post") ? 'edit' : 'create'; ?>" name="post_info" id="stpsinf" />
						<div class="col-md-12 form-group">
							<label class="text-dark font-weight-bold">Description<code>*</code> </label>
							<textarea name="jewbrief" class="form-control" id="prdscbrief"><?= (!empty($prd_ft)) ? $prd_ft['post_news']:''; ?></textarea>
							<span id="" class="error_here"></span>
						</div>
					</div>
				</div>
			</div>
				
			<div class="form-group w-100 text-center mt-3 ">
				<button class="btn bg-warning text-white rounded w-75 font-weight-bold" id="button_tbpst" type="submit"><i class="far fa-paper-plane"></i> Submit</button>
			</div>
		</div>
		</form>
	
		</div>
	<script src="assets/dropzone.js"></script>
	<script>
	$("#mkpost_form").on('submit',function(e) {
		e.preventDefault();	 
		var rev = $('#mkpost_form');
		$.ajax({ url: 'actions/create_post.php', type: 'POST', data:  new FormData(this),
				contentType: false, cache: false, processData:false, dataType:"json",
				beforeSend: function(){	
				$("#pic_a").siblings("span").text(''); $("#button_tbpst").html('<span class="spinner-grow-sm text-white ml-2 align-self-center loader-sm"></span> submiting '); $("#button_tbpst").attr('disabled', 'disabled'); },
				success: function(data){
				donejob(data.runstt,data.errorreason); $("#button_tbpst").html('<i class="far fa-paper-plane"></i> Submit'); $("#button_tbpst").attr('disabled', false);
				if(data.runstt == "grant"){	rev.trigger('reset'); }	else if(data.errorform != ""){ $("#pic_"+data.errorform).siblings("span").text(data.errorreason); }
				}
			});
    });
	
	tinymce.init({ selector: 'textarea#prdscbrief', menubar: false, plugins: 'link image code autolink lists media table ',
	toolbar: 'h3 h4 blockquote | undo redo strikethrough superscript subscript | underline bold italic | alignleft aligncenter alignright | numlist checklist outdent indent | link image table | paragraphgroup',
	images_upload_url: 'postimgindetail.php', toolbar_mode: 'floating', tinycomments_mode: 'embedded', tinycomments_author: 'Author name', height : 270, });
	</script>
		<?php
		}
		elseif(empty($setup) || $setup == "manage-posts"){
		?>
		<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">Manage Posts</h1>
			
			<div class="btn-toolbar mb-2 mb-md-0">
				<div class="btn-group mr-2">
					<a href="?setup=add-post" class="btn btn-icon bg-warning 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-square"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><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-12 m-auto">
				
				<div class="widget-content widget-content-area br-6">	
					<div class="table-responsive mb-4 mt-4">
						<table id="kmtprd_tbrecs" class="table table-stripped caption-top" style="width:100%">
							
							<thead>
								<tr>
									<th width="30%">Product</th>
									<th width="40%" class="vertical-align" data-sortable="false">Brief</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>
		
		<script type="text/javascript" language="javascript" >

		$(document).ready(function(){

		load_data();
		function load_data(setpst){
		var userdataTable = $('#kmtprd_tbrecs').DataTable({
				"processing": true,"serverSide": true,"order": [],
				"ajax":{
					url:"actions/fetch_news.php",
					type:"POST",
					data: {setpst: setpst}
				},
				"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_ Products",
						"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 Product Name...",
					   "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','.dptcn', function(){
			var id = this.id;  var split_id = id.split("_"); var txt = split_id[0]; var deprc = split_id[1]; 
			var data_dp = $("#drp_"+deprc).attr("data-dp"); $("#shwdps").html(data_dp);
			$("#kmtprd_tbrecs").DataTable().destroy();
			if(deprc != ""){ load_data(deprc); }
			else{ load_data(); }
		});
		
		})
		</script>
		<?php
		}
		?>

		</div>

	</div>

</div>

<?php include "footer.php"; ?>