<?php
    $running_year       = $this->crud->getInfo('running_year');
    $info               = base64_decode($data);
    $ex                 = explode('-', $info);
    $sub                = $this->db->get_where('subject', array('subject_id' => $ex[2]))->result_array();
    $nota_c             = $this->academic->getInfo('minium_mark');
    foreach ($sub as $row):
    $students   =   $this->db->get_where('enroll' , array('class_id' => $ex[0], 'section_id' => $ex[1] , 'year' => $running_year))->result_array();
?>
    <div class="content-w">
        <div class="conty">
            <?php include 'fancy.php'; ?>
            <div class="header-spacer"></div>
            <?php include 'subject_header.php';?>
            <div class="container-fluid">
                <div class="content-box">
                    <div class="back mb-3">
                        <a class="fs-16" href="<?php echo base_url();?>teacher/subject_dashboard/<?php echo $data;?>/"><i class="picons-thin-icon-thin-0131_arrow_back_undo"></i> <span><?php echo getEduAppGTLang('return_to_dashboard');?></span></a>
                    </div>
                    <div class="row">
                        <main class="col col-xl-12 order-xl-2 col-lg-12 order-lg-1 col-md-12 col-sm-12 col-12">
                            <div class="ui-block">
                                <article class="hentry post thumb-full-width">
                                    <div class="post__author author vcard inline-items">
                                        <img src="<?php echo base_url(); ?>public/uploads/<?php echo $this->crud->getInfo('logo'); ?>" class="no-border">
                                        <div class="author-date">
                                            <a class="h6 post__author-name fn" href="javascript:void(0);"><?php echo getEduAppGTLang('upload_marks'); ?> <small>(<?php echo $this->db->get_where('exam', array('exam_id' => $exam_id))->row()->name; ?>)</small>.</a>
                                        </div>
                                        <div class="pull-right">
                                            <a href="javascript:void(0);" data-toggle="modal" data-target="#crearcapacidad"><button type="button" class="btn btn-rounded btn-warning"><?php echo getEduAppGTLang('create_activity'); ?></button></a>
                                        </div>
                                    </div>
                                    <div class="edu-posts cta-with-media">
                                        <div id='cssmenu'>
                                            <ul>
                                            <?php
                                                $examss = $this->db->get('exam')->result_array();
                                                foreach ($examss as $exam):
                                            ?>
                                                <li class='<?php if ($exam['exam_id'] == $exam_id) echo "act"; ?>'><a href="<?php echo base_url(); ?>teacher/upload_marks/<?php echo $data . '/' . $exam['exam_id'] . '/' . $order; ?>/"><i class="os-icon picons-thin-icon-thin-0023_calendar_month_day_planner_events"></i><?php echo $exam['name']; ?></a></li>
                                            <?php endforeach; ?>
                                            </ul>
                                        </div>
                                        <div class="table-responsive">
                                            <?php echo form_open(base_url() . 'teacher/updatePoints/' . $data . '/' . $exam_id . '/' . $order . '/'); ?>
                                            <table class="studentInfo">
                                                <tbody>
                                                    <tr class="mainRow">
                                                        <td class="text-center col-sticky nums bg-white" width="85px">
                                                            <span class="full-width text-black d-inline"><?php echo getEduAppGTLang('roll'); ?></span>
                                                            <?php $studentsOrder = 0;
                                                            if ($order == 1) $studentsOrder = 2;
                                                            elseif ($order == 2) $studentsOrder = 1; ?>
                                                            <a href="<?php echo base_url(); ?>teacher/upload_marks/<?php echo $data; ?>/<?php echo $exam_id; ?>/<?php echo $studentsOrder; ?>/" class="btn btn-primary btn-sm bto"><?php if ($order == 1) echo 'A-Z'; elseif ($order == 2) echo 'Z-A'; ?></a>
                                                        </td>
                                                        <td class="col-sticky studs bg-white text-black stdi">
                                                            <span class="full-width d-inline"><?php echo getEduAppGTLang('student') ?></span>
                                                            <input type="text" class="searchInput" id="filter" placeholder="<?php echo getEduAppGTLang('search_student') ?>...">
                                                        </td>
                                                        <?php $markActivities = $this->db->order_by('mark_activity_id', 'ASC')->get_where('mark_activity', array('subject_id' => $row['subject_id'], 'exam_id' => $exam_id, 'class_id' => $ex[0], 'section_id' => $ex[1], 'year' => $running_year))->result_array(); ?>
                                                        <?php foreach ($markActivities as $cap) : ?>
                                                        <td class="text-center pd5px">
                                                            <span class="full-width d-inline"><?php echo $cap['name']; ?></span>
                                                            <a class="text-white" onclick="showAjaxModal('<?php echo base_url(); ?>modal/popup/modal_capacities/<?php echo $cap['mark_activity_id']; ?>/<?php echo $data . '/' . $exam_id . '/' . $order . '/'; ?>');" href="javascript:void(0);"><svg class="align-sub" xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m14.304 4.844l2.852 2.852M7 7H4a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h11a1 1 0 0 0 1-1v-4.5m2.409-9.91a2.017 2.017 0 0 1 0 2.853l-6.844 6.844L8 14l.713-3.565l6.844-6.844a2.015 2.015 0 0 1 2.852 0Z"/></svg></a>
                                                            <a class="text-white" href="<?php echo base_url().'teacher/manage_marks/delete_capacity/'.$data.'/'.$exam_id.'/'.$order.'/'.$cap['mark_activity_id'].'/';?>" onclick="return confirm('<?php echo getEduAppGTLang('confirm_delete');?>');"><svg class="align-sub" xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14 11v6m-4-6v6M6 7v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V7M4 7h16M7 7l2-4h6l2 4"/></svg></a>
                                                        </td>
                                                        <?php endforeach; ?>
                                                        <td class="text-center pd5px">
                                                            <?php echo getEduAppGTLang('average'); ?>
                                                        </td>
                                                        <td class="text-center pd5px">
                                                            <?php echo getEduAppGTLang('comment'); ?>
                                                        </td>
                                                    </tr>
                                                    <tbody id="results">
                                                        <?php 
                                                            $studs = $this->mark->get_enroll_students($row['subject_id'], $ex[0], $ex[1], $running_year, $order);
                                                            foreach($studs as $rows): 
                                                        ?>
                                                        <tr class="altRow">
                                                            <td class="text-center col-sticky nums bg-white"><?php echo $rows['roll'];?></td>
                                                            <td class="col-sticky studs bg-white">
                                                                <div class="studentContainer">
                                                                    <a href="<?php echo base_url();?>teacher/student_portal/<?php echo $rows['student_id'];?>"><img alt="" src="<?php echo $this->crud->get_image_url('student', $rows['student_id']); ?>" width="25px" class="std-img"> <?php echo $this->crud->get_name('student', $rows['student_id']); ?></a>
                                                                </div>
                                                            </td>
                                                        <?php 
                                                            $total = 0;
                                                            $markActivities = $this->db->order_by('mark_activity_id', 'ASC')->get_where('mark_activity', array('subject_id' => $row['subject_id'], 'exam_id' => $exam_id, 'class_id' => $ex[0], 'section_id' => $ex[1], 'year' => $running_year))->result_array();
                                                            foreach ($markActivities as $cap):
                                                        ?>
                                                            <td align="center">
                                                            <?php 
                                                                $notas = $this->db->order_by('point_id', 'ASC')->get_where('point', array('mark_activity_id' => $cap['mark_activity_id'], 'student_id' => $rows['student_id']));
                                                                $nota_cap = $notas->result_array();
                                                                foreach ($nota_cap as $nota):
                                                            ?>
                                                                    <?php $total+= $nota['nota'];?>
                                                                    <input type="number" value="<?php echo $nota['nota'] ?>" onkeyup="calcAverage(this)" min="0" name="mark_<?php echo $rows['student_id'] . '_' . $cap['mark_activity_id']; ?>" class="markInput" placeholder="0">
                                                                <?php endforeach;?>
                                                            </td>
                                                            <?php endforeach; ?>
                                                            <td class="text-center">
                                                                <span data-id="<?php echo $rows['student_id'];?>" id="avg_<?php echo $rows['student_id']; ?>"><?php if(count($markActivities) > 0) echo number_format($total/count($markActivities),2,".",","); else echo '0.00';?></span> 
                                                            </td>
                                                            <td>
                                                                <input type="hidden" id="final_avg_<?php echo $rows['student_id']; ?>" name="final_avg_<?php echo $rows['student_id']; ?>" value="<?php if(count($markActivities) > 0) echo number_format($total/count($markActivities),2,".",","); else echo '0.00';?>">
                                                                <input type="hidden" id="mark_obtained_<?php echo $rows['student_id']; ?>" name="mark_obtained_<?php echo $rows['student_id']; ?>" value="<?php echo $total;?>">
                                                                <?php 
                                                                    $comment = $this->db->order_by('mark_id', 'ASC')->get_where('mark', array('exam_id' => $exam_id, 'student_id' => $rows['student_id'], 'class_id' => $ex[0], 'section_id' => $ex[1], 'subject_id' => $ex[2], 'year' => $running_year))->result_array();
                                                                    foreach ($comment as $com):
                                                                ?>
                                                                    <input type="text" class="commentInput" name="comment_<?php echo $rows['student_id']; ?>" value="<?php echo $com['comment']; ?>" placeholder="<?php echo getEduAppGTLang('comment'); ?>...">
                                                                <?php endforeach; ?>
                                                            </td>
                                                        </tr>
                                                        <?php endforeach;?>
                                                    </tbody>
                                                </tbody>
                                            </table>
                                            <div class="form-buttons-w text-center">
                                                <button class="btn btn-success btn-rounded" type="submit"><?php echo getEduAppGTLang('update'); ?></button>
                                            </div>
                                            <?php echo form_close(); ?>
                                        </div>
                                    </div>
                                </article>
                            </div>
                        </main>
                    </div>
                </div>
                <a class="back-to-top" href="javascript:void(0);">
                    <img src="<?php echo base_url(); ?>public/style/olapp/svg-icons/back-to-top.svg" alt="arrow" class="back-icon">
                </a>
            </div>
        </div>
        <div class="modal fade" id="crearcapacidad" tabindex="-1" role="dialog" aria-labelledby="fav-page-popup" aria-hidden="true">
            <div class="modal-dialog window-popup fav-page-popup" role="document">
                <div class="modal-content">
                    <a href="javascript:void(0);" class="close icon-close" data-dismiss="modal" aria-label="Close"></a>
                    <div class="modal-header">
                        <h6 class="title"><?php echo getEduAppGTLang('create_new_activity'); ?></h6>
                    </div>
                    <div class="modal-body">
                        <?php echo form_open(base_url() . 'teacher/manage_marks/new_activity/' . $data . '/' . $exam_id . '/' . $order . '/', array('enctype' => 'multipart/form-data')); ?>
                        <div class="row">
                            <div class="col col-lg-12 col-md-12 col-sm-12 col-12">
                                <div class="form-group label-floating">
                                    <label class="control-label"><?php echo getEduAppGTLang('name'); ?></label>
                                    <input class="form-control" name="name" type="text" required>
                                </div>
                            </div>
                            <div class="col col-lg-12 col-md-12 col-sm-12 col-12">
                                <button class="btn btn-success btn-lg full-width" type="submit"><?php echo getEduAppGTLang('save'); ?></button>
                            </div>
                        </div>
                    </div>
                    <?php echo form_close(); ?>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        var minMark = <?php echo $nota_c; ?>;
    </script>
    <script src="<?php echo base_url();?>public/style/cms/js/marks.js"></script>
<?php endforeach; ?>