<?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> Learner Sport Details</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/sport_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 Sport Details <?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>Sport Details</th>
                                        <th>Team Details</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 
															$student_id = $student['admission_no'];
											
															$this->db->select('*');
															$this->db->from('student_sport');
															$this->db->where('student_id',$student_id);
															$query0 = $this->db->get();
															$result0 = $query0->row();
															
															$sportcode = $result0->sportcode;	

															$this->db->select('*');
															$this->db->from('sport');
															$this->db->where('sportcode',$sportcode);
															$query1 = $this->db->get();
															$sport_result = $query1->row();	
																
															$sportdet = $sport_result->name." > ".$sport_result->age_group." > ".$sport_result->gender." > ".$sport_result->description;	
															$sportdetvalidation = $sport_result->name."".$sport_result->age_group."".$sport_result->gender."".$sport_result->description;	

															$teamname = $sport_result->team;
															
															$this->db->select('*');
															$this->db->from('teams');
															$this->db->where('team_name',$teamname);
															$query2 = $this->db->get();
															$team_result = $query2->row();	

															$teamdet = "Team Name: ".$team_result->team_name." > Coach Name: ".$team_result->coachname." > ".$team_result->description;
															$teamdetvalidation = "".$team_result->team_name."".$team_result->coachname."".$team_result->description;
												?>
													<td><?php 
													if(!empty($sportdetvalidation)){
																echo $sportdet; 
														}else{ echo "<i style='color:red;'>Not yet assigned</i>";}?></td>
													<td>
													<?php 
													if(!empty($teamdetvalidation)){
																echo $teamdet; 
														}else{ echo "<i style='color:red;'>Not yet assigned</i>";}?>													
													</td>
										</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>