<?php include('inc/header.php');
$clientObj->select($org_id); 
if(isset($_POST['batch_submit']))
{
	
	
	 
	$startdate=sanitize($_POST['start_date']);
	$end_date=sanitize($_POST['end_date']);
	$title=sanitize($_POST['event']);
	$checkevent=$addeventObj->selectsameevent($startdate,$end_date,$title);
	if($checkevent==-1)
	{
	if($startdate<=$end_date)
	{
	$common=sanitize($_POST['common']);	
	if($common==1)
	{
	$addeventObj->start_date=sanitize($_POST['start_date']);
    $addeventObj->end_date=sanitize($_POST['end_date']);
	$addeventObj->title=sanitize($_POST['event']);
	$addeventObj->description=sanitize($_POST['desc']);
	$addeventObj->holiday=sanitize($_POST['holiday']);
	$addeventObj->common=$common;
	$addeventObj->department='-1';
	$addeventObj->batch=-1;
	$addeventObj->event_key=sanitize($_POST['eventkey']);
	$addeventObj->org_id=$org_id;
	$addeventObj->insert();	
	}
	foreach ($_POST['batch'] as $selectedOption)
	{
	
	if(!$selectedOption=='')
	{
	$common=0;
	}
	else
	{ 
	$common=sanitize($_POST['common']); 
	}
    $addeventObj->start_date=sanitize($_POST['start_date']);
    $addeventObj->end_date=sanitize($_POST['end_date']);
	$addeventObj->title=sanitize($_POST['event']);
	$addeventObj->description=sanitize($_POST['desc']);
	$addeventObj->holiday=sanitize($_POST['holiday']);
	$addeventObj->common=$common;
	$addeventObj->department='0';
	$addeventObj->batch=$selectedOption;
	$addeventObj->event_key=sanitize($_POST['eventkey']);
	$addeventObj->org_id=$org_id;
	$addeventObj->insert();
	}
	foreach ($_POST['dept'] as $selectedOption1)
	{
		if(!$selectedOption1=='')
	{
		$common=0;
	}else{ $common=sanitize($_POST['common']); }
	$addeventObj->start_date=sanitize($_POST['start_date']);
    $addeventObj->end_date=sanitize($_POST['end_date']);
	$addeventObj->title=sanitize($_POST['event']);
	$addeventObj->description=sanitize($_POST['desc']);
	$addeventObj->holiday=sanitize($_POST['holiday']);
	$addeventObj->common=$common;
	$addeventObj->department=$selectedOption1;
	$addeventObj->batch='0';
	$addeventObj->event_key=sanitize($_POST['eventkey']);
	$addeventObj->org_id=$org_id;
	$addeventObj->insert();
	}
	$msg=1;
	}
	else
	{
		$dateerror=1;
	}
	}
	else
	{
		$sameeventerror=1;
	}
}
/* Delete  */ 
if(isset($_POST['delsubmit']))
{
	$id=sanitize($_POST['del_id']);
	$addeventObj->select($id);
	$addeventObj->delete($id);
	
}
/* Delete  */

?>
<!--Delete Form-->               
<form action="#" name="delete_eveform" id="delete_eveform" method="POST">
<input type="hidden" name="del_id" id="del_id" value="" />
<input type="hidden" name="delsubmit" value="<?php echo time(); ?>" />
</form>
<!--Delete Form-->
<!-- mobile navigation -->
			<nav id="mobile_navigation"></nav>
			
			<section id="breadcrumbs">
				<div class="container">
					<ul>
						<li><a href="#">Web school </a></li>
                        <li><a href="#">General Settings </a></li>
						<li><span>Event Management</span></li>						
					</ul>
				</div>
			</section>
			<section class="container clearfix main_section">
				<div id="main_content_outer" class="clearfix">
					<div id="main_content">
						<div class="alert alert-info"><strong>Event Management</strong></div>
                         <?php 
						if($setting_error==1)
						{
						?>
                        <div class="warning">Hello ! Please <a href="general_setting.php" />Click Here</a> Complete your settings page. It is very important to fill up your general settings before you start.</div>
                        <?php
						}?>
						<?php 
						if($dateerror==1)
						{
						?>
                        <div class="error">Batch End date should be greater than start date.</div>
                        <?php
						}?>
                         <?php 
						if($sameeventerror==1)
						{
						?>
                        <div class="error">You cannot enter the same event twice.</div>
                        <?php
						}?> 
                        <?php 
						if($msg==1)
						{
						?>
                        <div class="success">Event has been created successfully</div>
                        <?php
						}?> 
                         
						<!-- main content -->
        
                        <form action="#" method="POST" id="addevent" name="addevent" enctype="multipart/form-data">
                        <?php $courselist=$courseObj->selectorgall(); 
							if(count($courselist)>0)
							{?>
                             <?php $empdepartmentlist=$emp_departmentObj->selectorgall(); 
							if(count($empdepartmentlist)>0)
							{?>
						<div class="row">
							<div class="col-sm-6">
								<div class="panel panel-default">
									<!--<div class="panel-heading">
										<h4 class="panel-title">Simple Validation</h4>
									</div>-->
									<div class="panel-body">
										
                                           
                                            <div class="form_sep">
												<label for="Timezone" class="req">Event Name</label>
												<input type="text"  name="event" id="event" class="form-control" data-required="true">
                                                
											</div>
											<div class="form_sep">
												<label for="reg_input_name" class="req">Description</label>
												<textarea id="desc" name="desc" class="form-control" data-required="true" ></textarea>
											</div>
                                         
                                            
										    <div class="form_sep">
												<label class="req">Start Date</label>
												<div class="input-group date startdate" data-date-format="yyyy-mm-dd">
													<input  name="start_date" id="start_date" class="form-control input_validate" type="text" data-required="true" data-type="dateIso" value="">
													<span class="input-group-addon"><i class="icon-calendar"></i></span>
												</div>
											</div>
                                            
                                            <div class="form_sep">
												<label class="req">End Date</label>
												<div class="input-group date enddate" data-date-format="yyyy-mm-dd">
													<input  name="end_date" id="end_date" class="form-control input_validate" type="text" data-required="true" data-type="dateIso" value="">
													<span class="input-group-addon"><i class="icon-calendar"></i></span>
												</div>
											</div>
									</div>
								</div>
							</div>
                            <div class="col-sm-6">
								<div class="panel panel-default">
									<!--<div class="panel-heading">
										<h4 class="panel-title">Simple Validation</h4>
									</div>-->
									<div class="panel-body">
										
                                           
                                            <div class="form_sep">
												<label for="Timezone" class="">Holiday ? </label>
												<input  name="holiday" id="holiday" checked="checked" type="checkbox"  value="1">
                                                
											</div>
											<div class="form_sep">
												<label for="reg_input_name" class="">Common to all</label>
												<input  name="common" id="common"   type="checkbox"  value="1">
											</div>
                                         <!--Hide course and dept-->
                                         <div class="hidecoursedept">
                                            
										    <div class="form_sep">
												<label class="">Select Batch</label>
                                                
												<select name='batch[]'  id="s2_basic" multiple class="form-control"  >
                                                <?php 
												$courselist=$courseObj->selectorgall();
												foreach($courselist as $clist)
												{
													$course=$clist->id;
													$course_name=$clist->course_name;
													$batchlist=$batchObj->selectbatch($course); ?>
													<optgroup label="<?php echo $course_name; ?>">
                                                    <?php foreach($batchlist as $blist)
												{?>
														<option value="<?php echo $blist->id; ?>"><?php echo $blist->batch; ?></option>
														<?php } ?>
													</optgroup>
												
                                                <?php } ?>
                                                </select>
											</div>
                                            
                                            <div class="form_sep">
												<label class="">Select Department (Press Ctrl to select more than one department)</label>
												
                                                <select id="s2_ext_value" name="dept[]" class="form-control" multiple>
                                               
                                                <?php foreach($empdepartmentlist as $list)
												{	?>
                                                <option value="<?php echo $list->id; ?>"><?php echo $list->department; ?></option>
                                                 <?php } ?>
                                                </select>
											</div>
                                            </div>
									</div>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="col-sm-5">
							<div class="form_sep"><input type="hidden" name="eventkey" id="eventkey" value="<?php $addeventObj->selecteventkey();   echo $addeventObj->id; ?>" />
                            <input type="hidden" name="test" id="test" value="" />
							<button class="btn btn-success" type="submit" name="batch_submit" id="batch_submit" >Save</button>
							</div>
                            </div>
                             <?php 
						   }
						   else
						   {
						  ?>
							  <div class="warning">You have to add department details before adding event.<a href="emp_department.php">Click Here</a> to add department.</div>  
						   <?php } ?>
                           <?php 
						   }
						   else
						   {
						  ?>
							  <div class="warning">You have to add course details before adding event.<a href="course.php">Click Here</a> to add course.</div>  
						   <?php } ?>
                            
						</div>
						</form>
                        <div style="margin-top:20px;">
						<div class="row">
							<div class="col-sm-12"> 
							<div class="panel panel-default">
                           
							<?php $eventlist=$addeventObj->selectorgall(); 
							if(count($eventlist)>0)
							{?>
                            <div class="panel-heading">
										<h4 class="panel-title"> Event List </h4>
									</div>
							<div class="panel_controls">
										<div class="row">
											<div class="col-sm-4">
												<label for="table_search">Search:</label>
												<input type="text" id="table_search" class="form-control">
											</div>
											<div class="col-sm-3 col-xs-6">
												
											</div>
											<div class="col-sm-3 col-xs-6">
												
											</div>
										</div>
									</div>
                            <table id="resp_table" class="table toggle-square" data-filter="#table_search" data-page-size="40">
										<thead>
											<tr>
												<th data-toggle="true">Event</th>
                                                <th data-hide="phone,tablet">Description</th>
												<th data-hide="phone,tablet">Holiday</th>
                                                <th data-hide="phone,tablet">Common to all</th>
												<th data-hide="phone,tablet"> Start Date</th>
                                                <th data-hide="phone,tablet"> End Date</th>
                                                
                                                <th > Delete</th>
											</tr>
										</thead>
										<tbody>
                                        <?php foreach($eventlist as $list)
										{?>
											<tr>
                                            <td><?php   echo $list->title; ?></td>
                                            <td><?php   echo $list->description; ?></td>
                                            <td><?php  if($list->holiday==1){ echo 'YES';} else { echo 'NO'; } ?></td>
                                            <td><?php if($list->common==1){ echo 'YES';} else { echo 'NO'; }  ?></td>
                                            <td><?php echo date('d-M-Y',strtotime($list->start_date)); ?></td>
                                            <td><?php echo date('d-M-Y',strtotime($list->end_date)); ?></td>
                                            
                                            <td><a href="#"><img src="img/error.png" class="deleteevent" name="del" id="del" del_id="<?php echo $list->id; ?>" title="Remove " /></a></td>
                                            </tr>
                                        <?php } ?>
											
										</tbody>
										<tfoot class="hide-if-no-paging">
											<tr>
												<td colspan="6" class="text-center">
													<ul class="pagination pagination-sm"></ul>
												</td>
											</tr>
										</tfoot>
									</table>
                            <?php } ?>
							</div>
                            </div>
						</div>
						</div>
							
						</div>
					</div>
				
			</section>
			<div id="footer_space"></div>
		</div>
	<?php include('inc/footer.php'); ?>