<section class="panel">
	<div class="tabs-custom">
		<ul class="nav nav-tabs">
			<li>
				<a href="<?=base_url('exam/grade')?>"><i class="fas fa-list-ul"></i> <?=translate('grade_list')?></a>
			</li>
			<li class="active">
				<a href="#edit" data-toggle="tab" ><i class="far fa-edit"></i> <?=translate('edit_grade')?></a>
			</li>
		</ul>
		<div class="tab-content">
			<div class="tab-pane active" id="edit">
				<?php echo form_open($this->uri->uri_string(), array('class' => 'form-horizontal form-bordered frm-submit'));?>
				<input type="hidden" name="grade_id" value="<?=$grade['id']?>" />
				<?php if (is_superadmin_loggedin()): ?>
				<div class="form-group">
					<label class="col-md-3 control-label"><?=translate('branch')?> <span class="required">*</span></label>
					<div class="col-md-6">
						<?php
							$arrayBranch = $this->app_lib->getSelectList('branch');
							echo form_dropdown("branch_id", $arrayBranch, $grade['branch_id'], "class='form-control'
							data-plugin-selectTwo data-width='100%' data-minimum-results-for-search='Infinity'");
						?>
						<span class="error"></span>
					</div>
				</div>
				<?php endif; ?>



				<?php 
				$branch_id = $this->data['grade']['branch_id'] ?? get_loggedin_branch_id(); 
?>

				<div class="form-group">
    <label class="col-md-3 control-label">
        <?= translate('marksheet') . " " . translate('template'); ?> <span class="required">*</span>
    </label>
    <div class="col-md-6">
        <?php
            // Fetch marksheet templates
            $templates = $this->app_lib->getMarksheet('marksheet_template', $branch_id);
            
            // Prepend a default 'Select Template' option
            $template_options = ['' => 'Select Template'] + $templates;

            echo form_dropdown(
                "template_id",
                $template_options,
                set_value('template_id', $grade['marksheet_id']),
                "class='form-control' id='templateID' name='template_id' data-plugin-selectTwo data-width='100%'"
            );
        ?>
        <span class="error"><?php echo form_error('template_id'); ?></span>
    </div>
</div>

				<div class="form-group">
					<label class="col-md-3 control-label"><?=translate('name')?> <span class="required">*</span></label>
					<div class="col-md-6">
						<input type="text" class="form-control" name="name" value="<?=$grade['name']?>" />
						<span class="error"></span>
					</div>
				</div>





				<!-- <?php
    // Ensure branch_id is set
    $branch_id = isset($grade['branch_id']) ? $grade['branch_id'] : '';
?>

<div class="form-group">
    <label class="col-md-3 control-label"><?=translate('group_type')?> <span class="required">*</span></label>
    <div class="col-md-6">
        <?php
            $arrayClass = $this->app_lib->getClass($branch_id); // Fetch classes based on branch

            // Retrieve selected class IDs for editing
            $selectedClasses = isset($grade['classes_id']) ? explode(',', $grade['classes_id']) : [];

            echo form_dropdown(
                "classes_id[]", // Multiple selection
                $arrayClass, 
                set_value('classes_id', $selectedClasses), // Ensure selected classes are preloaded
                "class='form-control' id='class_id' multiple='multiple' data-plugin-selectTwo data-width='100%'"
            );
        ?>
        <span class="error"></span>
    </div>
</div> -->




				<div class="form-group">
					<label class="col-md-3 control-label"><?=translate('grade_point')?> <span class="required">*</span></label>
					<div class="col-md-6">
						<input type="text" class="form-control" name="grade_point" value="<?=$grade['grade_point']?>" />
						<span class="error"></span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-3 control-label"><?=translate('min_percentage')?> <span class="required">*</span></label>
					<div class="col-md-6">
						<input type="text" class="form-control" name="lower_mark" value="<?=$grade['lower_mark']?>" />
						<span class="error"></span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-3 control-label"><?=translate('max_percentage')?> <span class="required">*</span></label>
					<div class="col-md-6">
						<input type="text" class="form-control" name="upper_mark" value="<?=$grade['upper_mark']?>" />
						<span class="error"></span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-3 control-label"><?=translate('remarks')?></label>
					<div class="col-md-6 mb-md">
						<input type="text" class="form-control" name="remark" value="<?=$grade['remark']?>" />
					</div>
				</div>
				<footer class="panel-footer">
					<div class="row">
						<div class="col-md-offset-3 col-md-2">
							<button type="submit" class="btn btn-default btn-block" data-loading-text="<i class='fas fa-spinner fa-spin'></i> Processing">
								<i class="fas fa-plus-circle"></i> <?=translate('update')?>
							</button>
						</div>
					</div>
				</footer>
				<?php echo form_close();?>
			</div>
		</div>
	</div>
</section>

<script>
$('#branch_id').change(function() {
    let branch_id = $(this).val();
    console.log('Selected branch_id:', branch_id); // Debugging

    $.ajax({
        url: "<?= base_url('exam/get_classes_by_branch'); ?>",
        type: "POST",
        data: { branch_id: branch_id },
        dataType: "json",
        success: function(response) {
            console.log('AJAX Response:', response); // Debugging

            let classSelect = $('#class_id');
            classSelect.empty(); // Clear previous options

            if (response.status === 'success') {
                $.each(response.data, function(index, classObj) {
                    classSelect.append(new Option(classObj.name, classObj.id));
                });
            } else {
                alert(response.message); // Show error message
            }
        },
        error: function(xhr, status, error) {
            console.error('AJAX Error:', xhr.responseText);
        }
    });
});
</script>	