<div class="row">
	<div class="col-md-5">
		<section class="panel">
			<header class="panel-heading">
				<h4 class="panel-title"><?=translate('add_session_term')?></h4>
			</header>
			<?php echo form_open($this->uri->uri_string()); ?>
			<div class="panel-body">






			<?php if (is_superadmin_loggedin() ): ?>
				<div class="form-group mb-md">
							<label class="control-label"><?=translate('branch')?> <span class="required">*</span></label>
							<?php
								$arrayBranch = $this->app_lib->getSelectList('branch');
								echo form_dropdown("branch_id", $arrayBranch, set_value('branch_id'), "class='form-control' id='branch_id' name='branch_id'
								data-plugin-selectTwo data-width='100%'");
							?>
							<span class="error"><?php echo form_error('branch_id'); ?></span>
						</div>
					
				<?php endif; ?>




				<div class="form-group mb-md">
							<label class="control-label"><?=translate('enrollment_period')?> <span class="required">*</span></label>
							<?php
						$arrayYear = array("" => translate('select'));
						$years = $this->db->get('schoolyear')->result();
						foreach ($years as $year) {
							$arrayYear[$year->id] = $year->school_year;
						}
						echo form_dropdown("session_id", $arrayYear, set_value('session_id', $global_config['session_id']), "class='form-control' required id='session_id' select2
						data-plugin-selectTwo data-width='100%' data-minimum-results-for-search='Infinity' name='session_id' ");
						?>
							<span class="error"><?php echo form_error('schoolyear'); ?></span>
						</div>



				<div class="form-group mb-md">
					<label class="control-label"><?=translate('term_name')?> <span class="required">*</span></label>
					<input type="text" class="form-control" name="term_name" value="<?=set_value('term_name')?>" />
					<span class="error"><?=form_error('term_name')?></span>
				</div>



				<div class="form-group mb-md">
    <label class="control-label"><?=translate('start_date')?> <span class="required">*</span></label>
    <input type="date" class="form-control" name="start_date" id="start_date" value="<?=set_value('start_date')?>" />
    <span class="error"><?=form_error('start_date')?></span>
</div>

<div class="form-group mb-md">
    <label class="control-label"><?=translate('end_date')?> <span class="required">*</span></label>
    <input type="date" class="form-control" name="end_date" id="end_date" value="<?=set_value('end_date')?>" />
    <span class="error"><?=form_error('end_date')?></span>
</div>

			</div>
			<div class="panel-footer">
				<div class="row">
					<div class="col-md-12">
						<button class="btn btn-default pull-right" type="submit" name="save" value="1">
							<i class="fas fa-plus-circle"></i> <?=translate('save')?>
						</button>
					</div>	
				</div>
			</div>
			<?php echo form_close();?>
		</section>
	</div>

	<div class="col-md-7">
		<section class="panel">
			<header class="panel-heading">
				<h4 class="panel-title"><?=translate('sessions_list')?></h4>
			</header>
			<div class="panel-body">
				<div class="table-responsive">
					<table class="table table-bordered table-hover table-condensed mb-md">
						<thead>
							<tr>
								<th><?=translate('session')?></th>
								<th><?=translate('term')?></th>
								<th><?=translate('start_of_term')?></th>
								<th><?=translate('end_of_term')?></th>
								<th><?=translate('status')?></th>
								<th><?=translate('created_at')?></th>
								<th><?=translate('action')?></th>
							</tr>
						</thead>
						<tbody>

						<?php

$this->db->select('schoolterm.*, schoolyear.school_year');
$this->db->from('schoolterm');
$this->db->join('schoolyear', 'schoolyear.id = schoolterm.school_year_id', 'left'); // Change 'left' to 'inner' if needed
$result = $this->db->get()->result_array();

if (count($result)):
    foreach ($result as $row):
?>
							<tr>
								<td><?php echo $row['school_year']; ?></td>

								<td><?php echo $row['term_name']; ?></td>
								<td><?php echo $row['start_date']; ?></td>
								<td><?php echo $row['end_date']; ?></td>
								<td>
									<?php if (get_session_id() == $row['school_year_id']) :?>
									<span class="label label-primary"> <?=translate('selected_session')?></span>
									<?php endif;?>
								</td>
								<th><?php echo _d($row['created_at']);?></th>
								<td>
									<!-- update link -->
									<a class="btn btn-default btn-circle icon editModal" href="javascript:void(0);" data-id="<?=$row['id']?>" data-session="<?=$row['school_year']?>">
										<i class="fas fa-pen-nib"></i>
									</a>
									
									<!-- delete link -->
									<?php 
									if (get_session_id() != $row['id'])
										echo btn_delete('sessions/delete/' . $row['id']);
									?>
								</td>
							</tr>
						<?php endforeach; endif;?>
						</tbody>
					</table>
				</div>
			</div>
		</section>
	</div>
</div>

<div class="zoom-anim-dialog modal-block modal-block-primary mfp-hide" id="modal">
	<section class="panel">
		<?php echo form_open('sessions/edit', array('class' => 'frm-submit')); ?>
			<header class="panel-heading">
				<h4 class="panel-title">
					<i class="far fa-edit"></i> <?=translate('edit_session')?>
				</h4>
			</header>
			<div class="panel-body">
				<input type="hidden" name="schoolyear_id" id="schoolyear_id" value="" >
				<div class="form-group mb-md">
					<label class="control-label"><?=translate('sessions')?> <span class="required">*</span></label>
					<input type="text" class="form-control" value="" name="session" id="session" />
					<span class="error"></span>
				</div>
			</div>
			<footer class="panel-footer">
				<div class="row">
					<div class="col-md-12 text-right">
						<button type="submit" class="btn btn-default" data-loading-text="<i class='fas fa-spinner fa-spin'></i> Processing"><?=translate('update')?></button>
						<button class="btn btn-default modal-dismiss"><?=translate('cancel')?></button>
					</div>
				</div>
			</footer>
		<?php echo form_close();?>
	</section>
</div>

<script type="text/javascript">
	$(document).ready(function () {
		$('.editModal').on('click', function() {
			var id = $(this).data('id');
			var session = $(this).data('session'); 
			$('.error').html("");
			$('#schoolyear_id').val(id);
			$('#session').val(session);
			mfp_modal('#modal');
		});
	});


	$(document).ready(function() {
    $('#schoolyear').select2({
        width: '100%'
    });
});





document.addEventListener('DOMContentLoaded', function () {
        const startDateInput = document.getElementById('start_date');
        const endDateInput = document.getElementById('end_date');

        // When the start_date is changed
        startDateInput.addEventListener('change', function () {
            const startDate = startDateInput.value;
            if (startDate) {
                // Set the min attribute of end_date to the selected start_date
                endDateInput.min = startDate;
            }
        });

        // Optional: Validate that end_date is not less than start_date
        endDateInput.addEventListener('change', function () {
            const startDate = startDateInput.value;
            const endDate = endDateInput.value;
            if (endDate && startDate && endDate < startDate) {
                alert('End date cannot be earlier than start date.');
                endDateInput.value = ''; // Reset end_date
            }
        });
    });



</script>





