<?php
	if(isset($_POST['behavioral_upload_btn'])){
		for($j = 0; $j < (int)$_POST["total_num"]; $j++){
			$student_name = inject_checker($connection, $_POST['student_name'][$j]);
			$student_class = inject_checker($connection, $_POST['student_class'][$j]);
			$behavioral_term = inject_checker($connection, $_POST['behavioral_term'][$j]);
			$behavioral_session = inject_checker($connection, $_POST['behavioral_session'][$j]);
			$puntuality = inject_checker($connection, $_POST['puntuality'][$j]);
			$neatness = inject_checker($connection, $_POST['neatness'][$j]);
            $relationship_with_others = inject_checker($connection, $_POST['relationship_with_others'][$j]);
			$regards_for_authority = inject_checker($connection, $_POST['regards_for_authority'][$j]);
			$attitude_towards_academics = inject_checker($connection, $_POST['attitude_towards_academics'][$j]);
			$honesty = inject_checker($connection, $_POST['honesty'][$j]);
			$initiative = inject_checker($connection, $_POST['initiative'][$j]);
			$form_master_or_mistress = inject_checker($connection, $_POST['form_master_or_mistress'][$j]);
			$date = date('d/M/Y');
			
			$query = " SELECT * FROM `behavioral` WHERE `class` = '{$student_class}' AND `term` = '{$behavioral_term}' AND `session` = '{$behavioral_session}' ";
			$run_query = mysqli_query($connection, $query);
		}
		if(mysqli_num_rows($run_query) > 0){
			$msg = "<p class='text-danger'><b><span class='glyphicon glyphicon-remove'></span> This Behavioral Record Has Already Been Uploaded</b></p>";
		}else{
			$query = " INSERT INTO `behavioral`(`name`, `class`, `term`, `session`, `puntuality`, `neatness`, `relationship_with_others`, `regards_for_authority`, `attitude_towards_academics`, `honesty`, `initiative`, `form_master_or_mistress`, `date`) VALUES ";
			for($j = 0; $j < (int)$_POST["total_num"]; $j++){
			$student_name = inject_checker($connection, $_POST['student_name'][$j]);
			$student_class = inject_checker($connection, $_POST['student_class'][$j]);
			$behavioral_term = inject_checker($connection, $_POST['behavioral_term'][$j]);
			$behavioral_session = inject_checker($connection, $_POST['behavioral_session'][$j]);
			$puntuality = inject_checker($connection, $_POST['puntuality'][$j]);
			$neatness = inject_checker($connection, $_POST['neatness'][$j]);
			$relationship_with_others = inject_checker($connection, $_POST['relationship_with_others'][$j]);
			$regards_for_authority = inject_checker($connection, $_POST['regards_for_authority'][$j]);
			$attitude_towards_academics = inject_checker($connection, $_POST['attitude_towards_academics'][$j]);
			$honesty = inject_checker($connection, $_POST['honesty'][$j]);
			$initiative = inject_checker($connection, $_POST['initiative'][$j]);
			$form_master_or_mistress = inject_checker($connection, $_POST['form_master_or_mistress'][$j]);
			$date = date('d/M/Y');
				
				$query .= "('$student_name','$student_class','$behavioral_term','$behavioral_session','$puntuality','$neatness','$relationship_with_others','$regards_for_authority','$attitude_towards_academics', '$honesty', '$initiative', '$form_master_or_mistress', '$date'),";
			}
			$query = rtrim($query,",");
			$run_query = mysqli_query($connection, $query);
			
			if($run_query == true){
                $msg = "<p class='text-success'><b><span class='glyphicon glyphicon-ok'></span>Behavioral Upload Successfully</b></p>";
				
			}else{
				$msg = "<p class='text-danger'><b><span class='glyphicon glyphicon-remove'></span> Failed! Try Again</b></p>";
			}
		}
	}
?>

<div class='page-title'>
  <div>
    <h1><i class='fa fa-laptop'></i> Behavioral Analysis</h1>
    <p>Upload Excel Analysis</p>
  </div>
  <div>
    <ul class='breadcrumb'>
      <li><a href='teacher_dashboard.php'><i class='fa fa-home fa-lg'></i></a></li>
      <li><a href='#'>Students Behavioral Analysis</a></li>
      <li><a href='#'>Upload Excel Analysis </a></li>
    </ul>
  </div>
</div>

<div class="card">
    <?php
        if(isset($msg)){
            echo $msg;
        }
    ?>
    <div class = 'row'>
        <div class = 'col-xs-12 col-sm-12 col-md-12'>
            <div class = 'panel panel-primary ch'>
                <div class = 'panel-heading'>
                    <h4 class = 'txt'>Manually Save Students Behavioral Analysis</h4>
                </div><br/>
                <div class = 'panel-body'>
                    <form method='POST' action=''>
                        <div class='row'>
                            <div class = 'col-xs-12 col-sm-12 col-md-4'>
                                <div class = 'input-group'>
                                    <span class = 'input-group-addon' id = 'sizing-addon2'>CLASS</span>
                                    <select class='form-control' name='class' required>
                                        <?php
                                            // Assuming teacher_id is stored in session after login
                                            $query = "SELECT DISTINCT class FROM `teachers_classes` WHERE teacher_id = $teacher_id";
                                            $run_query = mysqli_query($connection, $query);

                                            if (mysqli_num_rows($run_query) > 0) {
                                                while ($result = mysqli_fetch_assoc($run_query)) {
                                                    $classes = $result['class'];
                                                    echo "<option value='{$classes}'>{$classes}</option>";
                                                }
                                            } else {
                                                echo "<option>No classes assigned</option>";
                                            }
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class = 'col-xs-12 col-sm-12 col-md-4'>
                                <div class = 'input-group'>
                                    <span class = 'input-group-addon' id = 'sizing-addon2'>TERM:</span>
                                    <select class='form-control' name='term' required>
                                        <?php
                                            ///////////// POST ACTION TO DISPLAY CURRENT TERM //////////////
                                            $query = " SELECT * FROM `current_season` ";
                                            $run_query = mysqli_query($connection, $query);
                                            if(mysqli_num_rows($run_query) == 1){
                                                while($result = mysqli_fetch_assoc($run_query)){
                                                    $show_current_term = $result['current_term'];
                                                    echo"
                                                        <option>{$show_current_term}</option>
                                                    ";
                                                }
                                            }
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class = 'col-xs-12 col-sm-12 col-md-4'>
                                <div class = 'input-group'>
                                    <span class = 'input-group-addon' id = 'sizing-addon2'>SESSION</span>
                                    <select class='form-control' name='session' required>
                                        <?php
                                            ///////////// POST ACTION TO DISPLAY CURRENT SESSION //////////////
                                            $query = " SELECT * FROM `current_season` ";
                                            $run_query = mysqli_query($connection, $query);
                                            if(mysqli_num_rows($run_query) == 1){
                                                while($result = mysqli_fetch_assoc($run_query)){
                                                    $show_current_session = $result['current_session'];
                                                    echo"
                                                        <option>{$show_current_session}</option>
                                                    ";
                                                }
                                            }
                                        ?>
                                    </select>
                                </div>
                            </div>
                        </div><br>
                        <div class='row'>
                            <div class = 'col-xs-12 col-sm-12 col-md-4'>
                                <button type = 'submit' name='behavioral_get_student_btn' class ='btn btn-primary btn-md'><span class = 'glyphicon glyphicon-user'></span>&nbsp; SHOW STUDENTS</button><br/> 
                            </div>
                        </div>
                    </form>
                </div>
        </div>
    </div>
<?php
if (isset($_POST['behavioral_get_student_btn'])) {
    $class = $_POST['class'];
    $term = inject_checker($connection, $_POST['term']);
    $session = inject_checker($connection, $_POST['session']);

    if ($class === $select) {
        $msg = "Error: Please Select the Class for result upload";
    } elseif ($term === $select) {
        $msg = "Error: Please Select the Term for result upload";
    } else {
        $query = "SELECT * FROM `students` WHERE `class` = '{$class}'";
        $run_query = mysqli_query($connection, $query);

        if (mysqli_num_rows($run_query) > 0) {
            echo "
                <div class='row text-center'>
                    <div class='col-md-12'>
                        <h5 style='margin-top: 0;'>HIKIMAH INTERNATIONAL SCHOOLS</h5>
                        <h5>Behavioral Analysis</h5>
                    </div>
                    <div class='col-md-4'>
                        <p><b>CLASS: </b>{$class}</p>
                    </div> 
                    <div class='col-md-4'>
                        <p><b>TERM: </b>{$term}</p>
                    </div>
                    <div class='col-md-4'>
                        <p><b>SESSION: </b>{$session}</p>
                    </div>
                </div>
                <form method='POST' action'upload.php?go_upload'>
            ";

            $i = 0;
            $k = 1;

            echo "<div class='row'>"; // Start of students grid
            while ($result = mysqli_fetch_assoc($run_query)) {
                $fname = $result['firstname'];
                $oname = $result['othername'];
                $lname = $result['lastname'];
                $name = $fname . " " . $oname . " " . $lname;

                echo "
                    <div class='col-md-3 mb-4'>
                        <div class='card p-3'>
                            <h5>{$k}. {$name}</h5>
                            <input type='hidden' name='student_name[]' value='{$name}' class='form-control' readonly />
                            <input type='hidden' name='student_class[]' value='{$class}' />
                            <input type='hidden' name='behavioral_term[]' value='{$term}' />
                            <input type='hidden' name='behavioral_session[]' value='{$session}' />

                            <select name='puntuality[]' class='form-control mb-2' required>
                                <option disabled selected>Punctuality</option>
                                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                                <option>4</option>
                                <option>5</option>
                            </select>

                            <select name='neatness[]' class='form-control mb-2' required>
                                <option disabled selected>Neatness</option>
                                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                                <option>4</option>
                                <option>5</option>
                            </select>

                            <select name='relationship_with_others[]' class='form-control mb-2' required>
                                <option disabled selected>Relationship With Others</option>
                                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                                <option>4</option>
                                <option>5</option>
                            </select>

                            <select name='regards_for_authority[]' class='form-control mb-2' required>
                                <option disabled selected>Regards for Authority</option>
                                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                                <option>4</option>
                                <option>5</option>
                            </select>

                            <select name='attitude_towards_academics[]' class='form-control mb-2' required>
                                <option disabled selected>Attitude Towards Academics</option>
                                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                                <option>4</option>
                                <option>5</option>
                            </select>

                            <select name='honesty[]' class='form-control mb-2' required>
                                <option disabled selected>Honesty</option>
                                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                                <option>4</option>
                                <option>5</option>
                            </select>

                            <select name='initiative[]' class='form-control mb-2' required>
                                <option disabled selected>Initiative</option>
                                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                                <option>4</option>
                                <option>5</option>
                            </select>

                            <select name='form_master_or_mistress[]' class='form-control' required>
                                <option disabled selected>Form Master/Mistress</option>
                                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                                <option>4</option>
                                <option>5</option>
                            </select>
                        </div>
                    </div>
                ";
                $i++;
                $k++;
            }
            echo "</div>"; // End of students grid

            echo "
                <div class='row mt-4'>
                    <div class='col-md-3'>
                        <input type='hidden' name='total_num' value='{$i}' />
                        <input type='submit' name='behavioral_upload_btn' value='Upload' class='btn btn-primary' />
                    </div>
                </div>
                </form>
            ";
        } else {
            echo "<p class='text-danger'><b>No records found !!!</b></p>";
        }
    }
}
?>