<?php
$currency_symbol = $this->customlib->getSchoolCurrencyFormat();
?>
<div class="content-wrapper" style="min-height: 946px;">   
    <section class="content-header">
        <h1>
            <i class="fa fa-money"></i> Assign Team Members</h1>
									 		 <a href="<?php  echo base_url(); ?>search_student_sport" class="col-md pull-right">View Student Sport</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>	
						 		 <a href="<?php  echo base_url(); ?>teammember" class="col-md pull-right">Assign Student Sport to Team</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>											 
								 <a href="<?php  echo base_url(); ?>teams" class="col-md pull-right">Create Team</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
								 <a href="<?php  echo base_url(); ?>sport" class="col-md pull-right">Add Sport</a> 
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
								 <a href="<?php  echo base_url(); ?>categories" class="col-md pull-right">Create Categories</a>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><i class="fa fa-search"></i> <?php echo $this->lang->line('select_criteria'); ?></h3>
                    </div>
                    <div class="box-body">
                        <div class="row">
                            <div class="col-md-12 col-sm-12">
                                <div class="row">
                                    <form role="form" action="<?php echo site_url('teammember/search') ?>" method="post" class="">
                                        <?php echo $this->customlib->getCSRF(); ?>

                                        <div class="col-sm-6">
                                            <div class="form-group">
                                                <label>Learner Level</label><small class="req">  *</small>
                                                <select autofocus="" id="class_id" name="class_id" class="form-control" >
                                                    <option value=""><?php echo $this->lang->line('select'); ?></option>
                                                    <?php
                                                    foreach ($classlist as $class) {
                                                        ?>
                                                        <option value="<?php echo $class['id'] ?>" <?php if (set_value('class_id') == $class['id']) echo "selected=selected" ?>><?php echo $class['class'] ?></option>
                                                        <?php
                                                    }
                                                    ?>
                                                </select>
                                                <span class="text-danger"><?php echo form_error('class_id'); ?></span>
                                            </div> 
                                        </div>
                                        <div class="col-sm-6">
                                            <div class="form-group">
                                                <label>Learner Class</label>
                                                <select  id="section_id" name="section_id" class="form-control" >
                                                    <option value=""><?php echo $this->lang->line('select'); ?></option>
                                                </select>
                                                <span class="text-danger"><?php echo form_error('section_id'); ?></span>
                                            </div> 
                                        </div>
                                        <div class="col-sm-12">  
                                            <div class="form-group">
                                                <button type="submit" name="search" value="search_filter" class="btn btn-primary pull-right btn-sm checkbox-toggle"><i class="fa fa-search"></i> <?php echo $this->lang->line('search'); ?></button>
                                            </div>
                                        </div>
                                    </form>
                                </div>  
                            </div>
                        </div>
                    </div>
                </div>
                <?php
                if (isset($resultlist)) {
                    ?>
                    <div class="box box-info">
                        <div class="box-header ptbnull">
                            <h3 class="box-title titlefix"><i class="fa fa-users"></i> Learner <?php echo $this->lang->line('list'); ?>
                                </i> <?php echo form_error('student'); ?></h3>
                            <div class="box-tools pull-right">
                            </div>
                        </div>
                        <div class="box-body table-responsive">

                            <div class="download_label">Learner <?php echo $this->lang->line('list'); ?></div>
                            <table class="table table-striped table-bordered table-hover example">
                                <thead>

                                    <tr>
                                        <th>Learner Level</th>
                                        <th>Learner Class</th>
                                        <th><?php echo $this->lang->line('admission_no'); ?></th>
                                        <th>Learner <?php echo $this->lang->line('name'); ?></th>
                                        <th class="text-right">Assign Sport</th>
                                        <th class="text-right"><?php echo $this->lang->line('action'); ?></th>

                                    </tr>
                                </thead>            
                                <tbody>    
                                    <?php
                                    $count = 1;
                                    foreach ($resultlist as $student) {
                                        ?>
                                        <tr>
                                            <td><?php echo $student['class']; ?></td>
                                            <td><?php echo $student['section']; ?></td>
                                            <td><?php echo $student['admission_no']; ?></td>
                                            <td><?php echo $student['firstname'] . " " . $student['lastname']; ?></td>
											<?php 

                                                $this->db->select('*');
                                                $this->db->from('student_sport');
                                                $query = $this->db->get();
                                                $result = $query->row();
												
												$assigned_sport = $result->active;
												$student_id = $result->student_id;
												
											if($assigned_sport == 1 and $student_id == $student['admission_no']){
												
												 echo " <td class='text-right'><i style='color:blue;'>Learner already has a sport, check in Student Sport Menu.</i></td><td></td>";
                                            ?>
									<?php }else { ?> 
													<form id="form1" action="<?php echo base_url() ?>sport/student_sport"  method="POST" accept-charset="utf-8">
														<td class="pull-right">
														 <input  name="student_id" placeholder="" type="hidden" class="form-control"  value="<?php echo $student['admission_no']; ?>" />
															<div class="form-group">
																	<select id="sportcode" name="sportcode" placeholder="" type="text" class="form-control" >
																		<option value="select"><?php echo $this->lang->line('select') ?></option>
																		<?php 
																			foreach ($sport->result() as $name) {
																			?>
																				<option value="<?php echo $name->sportcode; ?>" >  <?php echo "".$name->name." > ".$name->age_group." > ".$name->gender; ?></option>     	 
																		<?php }
																		?>
																	</select>
																<span class="text-danger"><?php echo form_error('sportcode'); ?></span>
															</div>	
														</td>                                            
														<td>
														<div class="form-group">
																<button type="submit" class="btn btn-info pull-right"><?php echo $this->lang->line('save'); ?></button>
														</div>	
														</td>
														</form>									
									<?php } ?>
                                        </tr>
                                        <?php

                                    }
                                    $count++;
                                    ?>
                                </tbody></table>
                        </div>
                    </div>
                    <?php
                }
                ?>
            </div>

        </div> 

    </section>
</div>

<script type="text/javascript">
    function getSectionByClass(class_id, section_id) {
        if (class_id != "" && section_id != "") {
            $('#section_id').html("");
            var base_url = '<?php echo base_url() ?>';
            var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
            $.ajax({
                type: "GET",
                url: base_url + "sections/getByClass",
                data: {'class_id': class_id},
                dataType: "json",
                success: function (data) {
                    $.each(data, function (i, obj)
                    {
                        var sel = "";
                        if (section_id == obj.section_id) {
                            sel = "selected";
                        }
                        div_data += "<option value=" + obj.section_id + " " + sel + ">" + obj.section + "</option>";
                    });
                    $('#section_id').append(div_data);
                }
            });
        }
    }

    $(document).ready(function () {
        var class_id = $('#class_id').val();
        var section_id = '<?php echo set_value('section_id') ?>';
        getSectionByClass(class_id, section_id);
        $(document).on('change', '#class_id', function (e) {
            $('#section_id').html("");
            var class_id = $(this).val();
            var base_url = '<?php echo base_url() ?>';
            var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
            $.ajax({
                type: "GET",
                url: base_url + "sections/getByClass",
                data: {'class_id': class_id},
                dataType: "json",
                success: function (data) {
                    $.each(data, function (i, obj)
                    {
                        div_data += "<option value=" + obj.section_id + ">" + obj.section + "</option>";
                    });
                    $('#section_id').append(div_data);
                }
            });
        });
    });
</script>