<?php $profile_id = $this->session->userdata('profile_id'); ?>
<div class="col-sm-10">
    <div class="row ">

        <div class="col-sm-12 ">
            <span class="col-sm-6 text-left"><h3>Manage Batch Student</h3></span>
        </div>
        <div class="col-sm-12 ">
            <span class="col-sm-6 text-left">
                <h4>Batch: <?php echo $batch_info->course_batch_name; ?>
                </h4>
            </span>
        </div>
        <div class="col-sm-12 ">
            <span class="col-sm-6 text-left">
                <h4>Course: 
                    <a href="<?php echo base_url() . $course_info->url; ?>" target="_blank">
                        <?php echo $course_info->course_name; ?>
                    </a>
                </h4>
            </span>
            <span class="col-sm-6 text-left">
                <h4>Resource person: 
                    <a href="<?php echo base_url() . $resource_person_info->url . '/profile'; ?>" target="_blank">
                        <?php echo $resource_person_info->person_first_name . ' ' . $resource_person_info->person_last_name; ?>
                    </a>
                </h4>
            </span>
        </div>
        <div class="col-sm-12"><hr></div>
        <div class="col-sm-12"><hr></div>

        <div class="col-sm-12">

            <div class="col-sm-12  text-left  "style="margin-bottom: 5px;font-weight: bold; border-bottom: 2px solid #80c12c">
                <div class="col-sm-3">Name</div>
                <div class="col-sm-2">Mobile</div>
                <div class="col-sm-3">Email</div>
                <div class="col-sm-2">Status</div>
                <div class="col-sm-2">Action</div>
            </div>
            <?php
            $assigned_student_arr = explode(',', $batch_info->student_id);
            $security_salt = '#' . rand(1, 99999);
            foreach ($course_registered_student as $v_student) {
                $e_id = $this->encrypt->encode($v_student->student_id);
                $s_id = $e_id . $security_salt;
                $de_id = $this->encrypt->encode($s_id);
                $student_e_id = str_replace("/", "%F2", $de_id);
                $student_payment_status = $student_payment_info[$v_student->student_id]->payment_status;
                ?>
                <?php if ($student_payment_status == 1 || $student_payment_status == 2 || $student_payment_status == 6 || $student_payment_status == 7) { ?>
                    <div id="sActions<?php echo $v_student->student_id; ?>">
                        <div class="col-sm-12 text-left " style="padding: 10px;background-color:#F2F9EA;border: 1px solid #ededed">
                            <div class="col-sm-3">
                                <a href="<?php echo base_url() . $v_student->url . '/profile'; ?>" target="_blank">
                                    <?php echo $v_student->person_first_name . ' ' . $v_student->person_last_name; ?>
                                </a>
                            </div>
                            <div class="col-sm-2"><?php echo $v_student->person_contact_number; ?></div>
                            <div class="col-sm-3"><?php echo $v_student->person_email; ?></div>
                            <div class="col-sm-2">
                                <?php
                                if ($student_payment_info[$v_student->student_id]) {
                                    $payment_status = array('', 'Paid', 'Partial paid', 'Unpaid', 'Drop', 'cancel', 'pending', 'Free');
                                    echo $payment_status[$student_payment_status];
                                } else {
                                    echo 'Free';
                                }
                                ?>
                            </div>
                            <div class="col-sm-2">

                                <?php if (!in_array($v_student->student_id, $assigned_student_arr)) { ?>
                                    <?php if (count($assigned_batch[$v_student->student_id]) < 2) { ?>     
                                        <a href="javascript:;" onclick="assignStudent('<?php echo base_url() . 'super-admin/assign-batch-student/' . $student_e_id . '/' . $batch_info->course_batch_id; ?>', '<?php echo $v_student->student_id; ?>')">  
                                            <i class="fa fa-check" title="Assign"></i>
                                        </a>&nbsp;&nbsp;&nbsp; 
                                        <?php
                                    }
                                    if (count($assigned_batch[$v_student->student_id]) == 1 && !in_array($v_student->student_id, $assigned_student_arr)) {
                                        foreach ($assigned_batch[$v_student->student_id] as $v_assigned_batch) {
                                            ?>
                                            <a href="javascript:;" onclick="assignStudent('<?php echo base_url() . 'super-admin/unassign-batch-student/' . $student_e_id . '/' . $v_assigned_batch->course_batch_id; ?>', '<?php echo $v_student->student_id; ?>')">  
                                                <i class="fa fa-times-circle-o" title="Unassign from <?php echo $v_assigned_batch->course_batch_name; ?>"></i>
                                            </a>&nbsp;&nbsp;&nbsp; 
                                            <?php
                                        }
                                    }
                                    ?>

                                <?php } else { ?>
                                    <a href="javascript:;" onclick="assignStudent('<?php echo base_url() . 'super-admin/unassign-batch-student/' . $student_e_id . '/' . $batch_info->course_batch_id; ?>', '<?php echo $v_student->student_id; ?>')">  
                                        <i class="fa fa-times-circle-o" title="Unassign"></i>
                                    </a>&nbsp;&nbsp;&nbsp; 
                                <?php } ?>

                            </div>
                        </div>
                        <div class="col-sm-12 form-group"></div>
                    </div>
                <?php } else if ($course_info->course_type == 2) { // free course ?>
                    <div id="sActions<?php echo $v_student->student_id; ?>">
                        <div class="col-sm-12 text-left " style="padding: 10px;background-color:#F2F9EA;border: 1px solid #ededed">
                            <div class="col-sm-3">
                                <a href="<?php echo base_url() . $v_student->url . '/profile'; ?>" target="_blank">
                                    <?php echo $v_student->person_first_name . ' ' . $v_student->person_last_name; ?>
                                </a>
                            </div>
                            <div class="col-sm-2"><?php echo $v_student->person_contact_number; ?></div>
                            <div class="col-sm-3"><?php echo $v_student->person_email; ?></div>
                            <div class="col-sm-2">
                                <?php echo 'Free';?>
                            </div>
                            <div class="col-sm-2">

                                <?php if (!in_array($v_student->student_id, $assigned_student_arr)) { ?>
                                    <?php if (count($assigned_batch[$v_student->student_id]) < 2) { ?>     
                                        <a href="javascript:;" onclick="assignStudent('<?php echo base_url() . 'super-admin/assign-batch-student/' . $student_e_id . '/' . $batch_info->course_batch_id; ?>', '<?php echo $v_student->student_id; ?>')">  
                                            <i class="fa fa-check" title="Assign"></i>
                                        </a>&nbsp;&nbsp;&nbsp; 
                                        <?php
                                    }
                                    if (count($assigned_batch[$v_student->student_id]) == 1 && !in_array($v_student->student_id, $assigned_student_arr)) {
                                        foreach ($assigned_batch[$v_student->student_id] as $v_assigned_batch) {
                                            ?>
                                            <a href="javascript:;" onclick="assignStudent('<?php echo base_url() . 'super-admin/unassign-batch-student/' . $student_e_id . '/' . $v_assigned_batch->course_batch_id; ?>', '<?php echo $v_student->student_id; ?>')">  
                                                <i class="fa fa-times-circle-o" title="Unassign from <?php echo $v_assigned_batch->course_batch_name; ?>"></i>
                                            </a>&nbsp;&nbsp;&nbsp; 
                                            <?php
                                        }
                                    }
                                    ?>

                                <?php } else { ?>
                                    <a href="javascript:;" onclick="assignStudent('<?php echo base_url() . 'super-admin/unassign-batch-student/' . $student_e_id . '/' . $batch_info->course_batch_id; ?>', '<?php echo $v_student->student_id; ?>')">  
                                        <i class="fa fa-times-circle-o" title="Unassign"></i>
                                    </a>&nbsp;&nbsp;&nbsp; 
                                <?php } ?>

                            </div>
                        </div>
                        <div class="col-sm-12 form-group"></div>
                    </div>
                    <?php
                }
            }
            ?>
        </div>

        <!--------------------Start Pagination---------------------------->
        <div class="col-sm-12">
            <?php
            $total_page = ceil($total_student / $per_page_view);
//            echo $total_student;
//            echo $per_page_view;
//            echo $total_page;
//            if ($total_page > 1) {
            ?>
            <!--                <div class="col-sm-2 form-group" style="margin-top: 22px;">
                                <select onchange="getPerPageValue(this.value)"
                                        class="form-control">
                                    <option value="10">10</option>
                                    <option value="25">25</option>
                                    <option value="50">50</option>
                                    <option value="100">100</option>
                                    <option value="500">500</option>
                                </select>
                            </div>-->
            <?php
//            }
            ?>

            <div class="col-sm-12 form-group">
                <?php
                $security_salt = '#' . rand(1, 99999);
                $s_id = $batch_info->course_batch_id . $security_salt;
                $e_id = $this->encrypt->encode($s_id);
                $e_id = str_replace("/", "%F2", $e_id);

                if ($total_page > 1) {
                    ?>
                    <nav class="course-pagination">
                        <ul class="pagination">
                            <?php
                            if ($page > 2) {
                                ?>
                                <li>
                                    <a <?php echo 'href=' . base_url() . 'super-admin-user/'; ?>
                                        aria-label="Previous">
                                        <span aria-hidden="true" title="First Page">&laquo;&laquo;</span>
                                    </a>
                                </li>
                                <li>
                                    <a <?php echo 'href=' . base_url() . 'super-admin/course-registered-student/' . $course_info->url . '/' . ($page - 1) . '/' . $e_id; ?> 
                                        aria-label="Previous">
                                        <span aria-hidden="true" title="Previous Page">&laquo;</span>
                                    </a>
                                </li>
                            <?php } elseif ($page > 1) { ?>
                                <li>
                                    <a <?php echo 'href=' . base_url() . 'super-admin/course-registered-student/' . $course_info->url . '/' . ($page - 1) . '/' . $e_id; ?>  
                                        aria-label="Previous">
                                        <span aria-hidden="true" title="Previous Page">&laquo;</span>
                                    </a>
                                </li>
                                <?php
                            }
                            $count_page = 0;
                            if ($total_page < 10) {
                                for ($i = $page; $total_page >= $i; $i++) {
                                    ?>
                                    <li>
                                        <a <?php
                        if ($page == $i) {
                            echo 'style="background-color:#80c12c; color:#fff;"';
                        }
                                    ?>
                                        <?php echo 'href=' . base_url() . 'super-admin/course-registered-student/' . $course_info->url . '/' . $i . '/' . $e_id; ?> >
                                            <?php echo $i ?>
                                        </a>
                                    </li>
                                    <?php
                                }
                            } else if ($page + 10 <= $total_page) {
                                $count_page = $page + 10;
                                for ($i = $page; $count_page > $i; $i++) {
                                    ?>
                                    <li>
                                        <a <?php
                        if ($page == $i) {
                            echo 'style="background-color:#80c12c; color:#fff;"';
                        }
                                    ?>
                                        <?php echo 'href=' . base_url() . 'super-admin/course-registered-student/' . $course_info->url . '/' . $i . '/' . $e_id; ?> >
                                            <?php echo $i ?>
                                        </a>
                                    </li>
                                    <?php
                                }
                            } else {
                                for ($i = $total_page - 10; $total_page > $i; $i++) {
                                    ?>
                                    <li>
                                        <a <?php
                        if ($page == $i + 1) {
                            echo 'style="background-color:#80c12c; color:#fff;"';
                        }
                                    ?>
                                        <?php echo 'href=' . base_url() . 'super-admin/course-registered-student/' . $course_info->url . '/' . ($i + 1) . '/' . $e_id; ?> > 
                                            <?php echo ($i + 1) ?>
                                        </a>
                                    </li>
                                    <?php
                                }
                            }

                            if ($total_page > $page + 10) {
                                ?>
                                <li>
                                    <a <?php echo 'href=' . base_url() . 'super-admin/course-registered-student/' . $course_info->url . '/' . ($page + 1) . '/' . $e_id; ?>  aria-label="Next">
                                        <span aria-hidden="true" title="Next Page">&raquo;</span>
                                    </a>
                                </li>
                                <li>
                                    <a <?php echo 'href=' . base_url() . 'super-admin/course-registered-student/' . $course_info->url . '/' . $total_page . '/' . $e_id; ?>  
                                        aria-label="Next">
                                        <span aria-hidden="true" title="Last Page">&raquo;&raquo;</span>
                                    </a>
                                </li>
                            <?php } ?>
                        </ul>
                    </nav>
                <?php } ?>
            </div>
        </div>
    </div>
</div>

<script>
    function create_xmlhttp() {
        var xmlhttp = false;
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
            xmlhttp = new XMLHttpRequest();
        }
        return xmlhttp;
    }

    function assignStudent(url, ID) {
        xmlhttp = create_xmlhttp();
        serverPage = url;
        xmlhttp.open("GET", serverPage);
        xmlhttp.onreadystatechange = function ()
        {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
                document.getElementById("sActions" + ID).innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.send(null);
    }
</script>