<?php
include "header.php";

if($post_url == "manage-calendar" || empty($post_url)):

$fet_cln = gtALL("acadcalendar",""," ac_id DESC");
?>

<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"> Manage Calendars </h1>
				<div class="btn-toolbar mb-2 mb-md-0">
					<div class="btn-group mr-2">
					
						<a href="?preg=post-calendar" 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 New </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="recordFetched" class="table table-stripped caption-top" style="width:100%">
							
							<thead>
								<tr>
									<th width="55%">Post</th>
									<th width="15%" class="text-center vertical-align">File</th>
									<th width="15%" class="text-center vertical-align"  data-sortable="false">Action</th>
								</tr>
							</thead>
							<tbody>
							<?php
							if(!empty($fet_cln)){
								foreach($fet_cln as $fv){
									
									$prd_id = $fv['ac_id'];	
									$prd_file = (!empty($fv['ac_file']) && file_exists('../img/shield-calendar/'.$fv['ac_file'])) ? '../img/shield-calendar/'.$fv['ac_file'] : 'javascript:void(0);';
	
							?>
								<tr  class="tritem">
									<td>
										<div class="smlinf">
											<div class="prdinfo">
												<div class="image d-flex align-item-center">
													<a href="javascript:void(0);">
														<img src="../img/logo-badge.png" alt="shield calendar" class="img-fluid" />
													</a>
												</div>
												
												<div class="content py-0">
													<h5 class="name">
														<a href="javascript:void(0);" class="text-primary font-weight-bold"><?= utf8_encode($fv['ac_year']); ?></a>
													</h5>
													<span class="email text-dark font-weight-bold"><?= utf8_encode($fv['ac_remarks']) ?></span>
												</div>
											</div>
										</div>
									</td>
									
									<td>
										<div class="table-data-feature text-center">
											<a href="<?= $prd_file; ?>" target="_blank" class="bg-info text-white bs-tooltip border-0 btn rounded-circle"  title="View">
												<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-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline>
												<line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>
											</a>
										</div>
									</td>
									
									<td>
										<div class="table-data-feature text-center">
										 
											<a href="?preg=edit-calendar&prd=<?= $prd_id; ?>" class="bg-primary text-white bs-tooltip border-0 btn rounded-circle" title="Edit">
												
													<svg xmlns="http://www.w3.org/2000/svg" width="18" height="24" viewBox="0 0 24 25" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" class="feather feather-edit-3">
												<path d="M12 20h9"></path><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path></svg>
											</a>
											
											<button class="bg-danger recshw text-white bs-tooltip border-0 btn rounded-circle" id="del_<?= $prd_id; ?>" onClick="takeoff('<?= $prd_id; ?>','ypqmkd')"  title="Delete">
											   <svg xmlns="http://www.w3.org/2000/svg" width="18" height="24" viewBox="0 0 24 25" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" class="feather feather-trash-2 table-cancel">
												<polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line>
												</svg>
											</button>
										</div>
									</td>
								</tr>
							<?php
							} }
							?>
							</tbody>		
						</table>
					</div>
					
				</div>	
				</div>
		
			</div>

		</div>

	</div>

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

$prd_ft = "";
if(!empty($prd))
{
	$chk_prd_rc = $db_con->prepare("SELECT * FROM acadcalendar WHERE ac_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 ='calendar?preg=manage-calendar'</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"> Add Calendar</h1>
				<div class="btn-toolbar mb-2 mb-md-0">
					<div class="btn-group mr-2">
					
						<a href="?preg=manage-calendar" 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 Posts</a>
						
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xl-8 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-6">
							        <div class="form-group">
        								<label class="text-dark font-weight-bold">Academic Year <code>*</code>  </label>
        								<input type="text" name="posttitle" class="form-control" required placeholder="2023/2024 Term 1" value="<?= (!empty($prd_ft)) ? $prd_ft['ac_year']:''; ?>"/>
        								<span id="" class="error_here"></span>
        							</div>
							    </div>
								
								<div class="col-md-6">
									<div class="form-group">
										<label class="text-dark font-weight-bold">Calendar File <?= (!empty($prd_ft)) ? '':'<code>*</code>'; ?>   </label>
										<input type="file"  name="pics_a"  id="pic_a" class="form-control for_image" <?= (!empty($prd_ft)) ? '':'required'; ?> accept="application/pdf" />
										<span id="img_error" class="error_here"></span>
									</div>
								</div>
							</div>
							
							<?= (!empty($prd_ft)) ? '<input type="hidden" name="pstrec" value="'.$prd_ft['ac_id'].'" />' : ''; ?>
							
							<input type="hidden" id="setbk" value="<?= $post_url; ?>" name="post_info" />
							
							<div class="form-group">
							<label class="text-dark font-weight-bold">Brief Remarks<code>*</code> </label>
								<textarea name="briefremark" class="form-control" id="brief" required maxlength="200"><?= (!empty($prd_ft)) ? $prd_ft['ac_remarks']:''; ?></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"  id="button_tbpst">Submit</button>
							</div>
						</div>
					</form>
					
				</div>
				</div>	
			</div>
		
			</div>

		</div>

	</div>

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

$(document).ready(function(){

$("#mkpost_form").on('submit',function(e) {
		e.preventDefault();	 
		
		var rev = $('#mkpost_form');
		
			$.ajax({
				url: 'actions/post_calendar.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-calendar"){
							rev.trigger('reset');	
						}
						else if(data.errorform != "")
						{
							$("#pic_"+data.errorform).siblings("span").text(data.errorreason);
							
						}
					}
			});
    });	
})
</script>


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