<?php

function format_interval(DateInterval $interval) {
    $result = "";
    if ($interval->y) {
        $result .= $interval->format("%y years ");
    }
    if ($interval->m) {
        $result .= $interval->format("%m months ");
    }
    if ($interval->d) {
        $result .= $interval->format("%d days ");
    }
    if ($interval->h) {
        $result .= $interval->format("%h hours ");
    }
    if ($interval->i) {
        $result .= $interval->format("%i minutes ");
    }
    if ($interval->s) {
        $result .= $interval->format("%s seconds ");
    }

    return $result;
}
?>
<div class="col-sm-8">
    <div class="row ">
        <div class="col-sm-12 ">
            <div class="col-sm-12">
                <h3>Course Schedule</h3>
                <hr>
            </div>

            <div class="col-sm-12" >
                <div class="col-sm-12 gray_ttc text-left" style="border-bottom: 2px solid #8CC73F;padding-top: 10px;padding-bottom: 10px;font-weight: bold">

                    <div class="col-xs-7 col-sm-9">Class Details</div>
                    <div class="col-xs-5 col-sm-3">Action</div>

                </div>
                <div class="col-sm-12"><br></div> 
                <div class="col-sm-12">
                    <p>
                        <?php
                        $message = $this->session->userdata('message');
                        $this->session->unset_userdata('message');
                        if ($message) {
                            echo $message;
                        }
                        ?>
                    </p>
                </div> 


                <style>
                    .gray{
                        background-color: #ccffcc;
                    }
                    .white{
                        background-color: #ffffff;
                    }
                </style>

                <?php
                $i = 1;
                $count_course_class = array(0);
                foreach ($classes[$course_info->course_id] as $v_my_class) {
                    $count_course_class[$v_my_class->course_id] = $i;
                    ?>
                    <?php if ($classroom_student_status[$v_my_class->classroom_id]->student_id != '') { ?> 
                        <div class="col-sm-12 <?php
                        if ($i % 2 == 0) {
                            echo 'gray';
                        } else {
                            echo 'white';
                        }
                        ?> " id="actions_for<?php echo $v_my_class->classroom_id; ?>" >

                            <?php
                            $url = $v_my_class->classroom_url;
                            $encrypted_url = $this->encrypt->encode($url);
                            $encrypted_url = str_replace("/", "%F2", $encrypted_url);
                            ?>
                            <div class="col-xs-7  col-sm-9 text-left" >
                                <address>
                                    <span class="row col-sm-12" >   
                                        <span class="col-sm-4" style="padding:0">Course Name</span>
                                        <span class="col-sm-1" style="padding:0">: </span>
                                        <span class="col-sm-6" style="padding:0">
                                            <a href="<?php echo base_url() . $course_info->url; ?>" >
                                                <?php echo $v_my_class->course_name; ?>
                                            </a>
                                        </span>
                                    </span>

                                    <span class="row col-sm-12">   
                                        <span class="col-sm-4" style="padding:0">Classroom Title</span>
                                        <span class="col-sm-1" style="padding:0">: </span>
                                        <span class="col-sm-6" style="padding:0"><?php echo $v_my_class->classroom_title; ?></span>
                                    </span>


                                    <?php
                                    $offset = $this->session->userdata('time_offset');
                                    $required_time = (strtotime("$v_my_class->class_start_time") + $offset);

                                    $now = date('Y-M-d H:i:s', strtotime(gmdate('Y-M-d H:i:s')) - $offset);
                                    $now = new DateTime($now, new DateTimeZone($person_info->person_time_zone));

                                    $class_start_time = date('Y-M-d H:i:s', (strtotime($v_my_class->class_start_time)));
                                    $class_start_time = new DateTime($class_start_time, new DateTimeZone($person_info->person_time_zone));
//                                    print_r($now);
//                                    print_r($class_start_time);
                                    $interval = $class_start_time->diff($now);

                                    $class_start_time = date('Y-M-d H:i:s', (strtotime($v_my_class->class_start_time) + $offset));
                                    $now = date('Y-M-d H:i:s', (strtotime(gmdate('Y-M-d H:i:s'))));
//                                    echo $class_start_time;
//                                    echo $now;
                                    $total_duration = strtotime($v_my_class->class_end_time) - strtotime($v_my_class->class_start_time);
                                    ?>

                                    <span class="row col-sm-12" >   
                                        <span class="col-sm-4" style="padding:0">Class Status</span>
                                        <span class="col-sm-1" style="padding:0">: </span>
                                        <span class="col-sm-6" style="padding:0">
                                            <?php
                                            if ($v_my_class->classroom_status == 0) {
                                                if ($class_start_time < $now) {
                                                    echo 'Running';
                                                } else {
                                                    echo 'Start In ' . format_interval($interval);
                                                }
                                            } else {
                                                $classroom_status = array('', 'Running', 'Finished', '', 'Not Held', 'Canceled', 'Archived', 'Draft', 'Not Held');
                                                echo $classroom_status[$v_my_class->classroom_status];
                                            }
                                            ?>
                                        </span>
                                    </span>

                                    <span class="row col-sm-12" >
                                        <span class="col-sm-4" style="padding:0">Presented by</span>
                                        <span class="col-sm-1" style="padding:0">: </span>
                                        <span class="col-sm-6" style="padding:0">
                                            <a href="<?php echo base_url() . $resource_person_info->url . '/profile/'; ?>"><?php echo $resource_person_info->person_first_name . ' ' . $resource_person_info->person_last_name; ?></a>
                                        </span>
                                    </span>

                                    <span class="row col-sm-12" >   
                                        <span class="col-sm-4" style="padding:0">Duration</span>
                                        <span class="col-sm-1" style="padding:0">: </span>
                                        <span class="col-sm-6" style="padding:0"><?php echo $v_my_class->class_duration; ?> Minutes</span>
                                    </span>
                                    <?php ?>
                                </address>
                            </div>

                            <div class="col-xs-5  col-sm-3 text-left" style="padding: 20px 0 0 0" >
                                <?php if ($v_my_class->student_id) { ?>

                                    <a href="<?php echo base_url() . $url ?>"><i class="fa  fa-play-circle-o" title="Join Class"></i></a>&nbsp;&nbsp;&nbsp;

                                <?php } else { ?>

                                    <?php
                                    if ($v_my_class->classroom_status == 0) { // Class Not Started = 0   
                                        if ($v_my_class->course_type == 1) {
                                            if ($course_payment_info[$v_my_class->course_id]->payment_status == 2 && $count_course_class[$v_my_class->course_id] <= ceil(($number_of_course_class[$v_my_class->course_id] / 2))) {
                                                ?>
                                                <a href="<?php echo base_url() . $url; ?>" target="blank" onclick="reloadPage();">
                                                    <i class="fa  fa-play-circle-o" title="Launch Class"></i>
                                                </a>&nbsp;&nbsp;&nbsp;
                                                <?php
                                            }
                                        } else if ($v_my_class->course_type == 2) {
                                            ?>
                                            <a href="<?php echo base_url() . $url; ?>" target="blank" onclick="reloadPage();">
                                                <i class="fa  fa-play-circle-o" title="Launch Class"></i>
                                            </a>&nbsp;&nbsp;&nbsp;

                                        <?php } ?>

                                        <?php
                                    } else if ($v_my_class->classroom_status == 1) { // Class Running = 1                
                                        if ($v_my_class->course_type == 1) {
                                            if ($course_payment_info[$v_my_class->course_id]->payment_status == 2 && $count_course_class[$v_my_class->course_id] <= ceil(($number_of_course_class[$v_my_class->course_id] / 2))) {
                                                ?>
                                                <a href="<?php echo base_url() . $url; ?>" target="blank" onclick="reloadPage();">
                                                    <i class="fa  fa-play-circle-o" title="Launch Class"></i>
                                                </a>&nbsp;&nbsp;&nbsp;
                                                <?php
                                            }
                                        } else if ($v_my_class->course_type == 2) {
                                            ?>
                                            <a href="<?php echo base_url() . $url; ?>" target="blank" onclick="reloadPage();">
                                                <i class="fa  fa-play-circle-o" title="Launch Class"></i>
                                            </a>&nbsp;&nbsp;&nbsp;

                                        <?php } ?>

                                        <?php
                                    } else if ($v_my_class->classroom_status == 2) { // Class End = 2                
                                        if ($v_my_class->course_type == 1) {
                                            if ($course_payment_info[$v_my_class->course_id]->payment_status == 2 && $count_course_class[$v_my_class->course_id] <= ceil(($number_of_course_class[$v_my_class->course_id] / 2))) {
                                                ?>
                                                <a href="<?php echo base_url() . 'download-class-video/' . $url; ?>">
                                                    <i class="fa  fa-download" title="Download Class Video"></i>
                                                </a>&nbsp;&nbsp;&nbsp;
                                                <?php
                                            }
                                        } else if ($v_my_class->course_type == 2) {
                                            ?>
                                            <a href="<?php echo base_url() . 'download-class-video/' . $url; ?>">
                                                <i class="fa  fa-download" title="Download Class Video"></i>
                                            </a>&nbsp;&nbsp;&nbsp;

                                        <?php } ?>
                                        <?php
                                    } else if ($v_my_class->classroom_status == 3) { // Class Unexpectedly END = 3 
                                        if ($v_my_class->course_type == 1) {
                                            if ($course_payment_info[$v_my_class->course_id]->payment_status == 2 && $count_course_class[$v_my_class->course_id] <= ceil(($number_of_course_class[$v_my_class->course_id] / 2))) {
                                                ?>
                                                <a href="<?php echo base_url() . $url; ?>" target="blank" onclick="reloadPage();">
                                                    <i class="fa  fa-play-circle-o" title="Launch Class"></i>
                                                </a>&nbsp;&nbsp;&nbsp;
                                                <?php
                                            }
                                        } else if ($v_my_class->course_type == 2) {
                                            ?>
                                            <a href="<?php echo base_url() . $url; ?>" target="blank" onclick="reloadPage();">
                                                <i class="fa  fa-play-circle-o" title="Launch Class"></i>
                                            </a>&nbsp;&nbsp;&nbsp;

                                        <?php } ?>
                                    <?php } else if ($v_my_class->classroom_status == 4) { // Class Running = 4                    ?>

                                        <a href="#" onclick="return checkForAction('<?php echo base_url() . 'delete-schedule-class/' . $v_my_class->classroom_id; ?>', 'Delete', <?php echo $v_my_class->classroom_id; ?>)">
                                            <i class="fa fa-trash-o" title="Delete"></i> 
                                        </a>&nbsp;&nbsp;&nbsp;

                                    <?php } else if ($v_my_class->classroom_status == 5) { // Class Canceled = 5                    ?>

                                        <a href="#" onclick="return checkForAction('<?php echo base_url() . 'delete-schedule-class/' . $v_my_class->classroom_id; ?>', 'Delete', <?php echo $v_my_class->classroom_id; ?>)">
                                            <i class="fa fa-trash-o" title="Delete"></i>
                                        </a>&nbsp;&nbsp;&nbsp;

                                    <?php } ?>

                                <?php } ?>
                            </div>


                        </div>
                        <div class="col-sm-12"><br></div> 
                        <?php
                    }
                    $i++;
                }
//                }
                ?>

                <script>

                    function checkForAction(url, action, ID) { // check for Active Inactives
                        //                        alert();
                        swal({
                            title: "",
                            text: "Are you sure you want to " + action + " this Teacher?",
                            type: "warning",
                            showCancelButton: true,
                            confirmButtonColor: '#DD6B55',
                            confirmButtonText: 'Yes',
                            cancelButtonText: "No",
                            closeOnConfirm: true,
                            closeOnCancel: true
                        },
                        function (isConfirm) {
                            if (isConfirm) {
                                //                                document.location.href = url;
                                serverPage = url;
                                xmlhttp.open("GET", serverPage);
                                //                                alert(serverPage);
                                xmlhttp.onreadystatechange = function ()
                                {
                                    //                                    alert(xmlhttp.readyState);
                                    //                                    alert(xmlhttp.status);
                                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                                    {
                                        //                                        alert(xmlhttp.responseText);
                                        document.getElementById("actions_for" + ID).innerHTML = xmlhttp.responseText;
                                        document.getElementById("message_for" + ID).innerHTML = "<div class='alert alert-info' style='height:20px; padding:0px;margine:0px;' ><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>Class Successfully " + action + "</div>";

                                    }
                                }
                                xmlhttp.send(null);
                            }
                        });
                    }

                    function cancelClass(classroomId) {
                        message = 'do you want to Cancel Class!!';
                        chk = confirm(message);
                        if (chk) {
                            serverPage = '<?php echo base_url() . 'cancel-schedule-class/'; ?>' + classroomId;
                            xmlhttp.open("GET", serverPage);
                            xmlhttp.onreadystatechange = function ()
                            {
                                if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
                                {                                     //                                document.getElementById('res').innerHTML = xmlhttp.responseText;
                                }
                            };
                            xmlhttp.send(null);
                            location.reload();
                        }
                    }

                    function reScheduleClass(classroomId) {
                        message = 'do you want to Cancel Class!!';
                        chk = confirm(message);
                        if (chk) {
                            serverPage = '<?php echo base_url() . 'reschedule-schedule-class/'; ?>' + classroomId;
                            xmlhttp.open("GET", serverPage);
                            xmlhttp.onreadystatechange = function ()
                            {
                                if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
                                {
                                    //                                document.getElementById('res').innerHTML = xmlhttp.responseText;
                                }
                            };
                            xmlhttp.send(null);
                            location.reload();
                        }
                    }
                </script>

                <!--START TalhaTraining Course -->

                <!--START TalhaTraining Upcoming Course -->
                <div class="col-sm-12"><br></div>
                <div class="col-sm-12">
                    <h3>
                        You can register any of TalhaTraining Upcoming Course bellow:
                    </h3>
                    <hr><br>
                </div>
                <div class="col-sm-12 " style="padding: 0">

                    <?php
                    $i = 0;
                    foreach ($tt_upcomming_course as $v_tt_upcomming_course) {
                        if (++$i > 5) {
                            break;
                        }
                        ?>
                        <div class="row gray" >
                            <div class="col-sm-5 text-left" ><a href="#" class="red"><?php echo $v_tt_upcomming_course->course_name; ?></a><br>
                                By- <span class="green"><a href="#"><?php echo $v_tt_upcomming_course->person_first_name . ' ' . $v_tt_upcomming_course->person_last_name; ?></a></span>
                            </div>
                            <div class="col-sm-2" style="padding: 0"><div class="rating-read-only" ></div></div>
                            <div class="col-sm-3" style="padding:0px;">
                                <span class="red">
                                    <?php
                                    if ($v_tt_upcomming_course->course_level == 0) {
                                        echo 'Basic';
                                    } else if ($v_tt_upcomming_course->course_level == 1) {
                                        echo 'Basic';
                                    } else if ($v_tt_upcomming_course->course_level == 2) {
                                        echo 'intermediate';
                                    } else if ($v_tt_upcomming_course->course_level == 3) {
                                        echo 'advanced';
                                    }
                                    ?>
                                </span>
                                <?php echo '&nbsp; BDT ' . $v_tt_upcomming_course->course_price; ?>
                            </div>
                            <div class="col-sm-2" style="padding-left:0;">
                                <a class="btn btn-success" data-toggle="tooltip" title="Register Now!" data-placement="top"><i class="fa fa-sign-in"></i></a> 
                                <a class="btn btn-danger" data-toggle="tooltip" title="Add to wishlist!" data-placement="top"><i class="fa fa-heart"></i></a>
                            </div>
                        </div>

                        <div><br></div>
                        <script>
                            $.fn.raty.defaults.path = 'images';
                            $('.rating').raty();
                            $('.rating-read-only').raty({readOnly: true, score: <?php echo $v_tt_upcomming_course->course_rating; ?>});
                        </script>



                    <?php } ?>
                    <div><br></div>
                    <div class="text-right">
                        <a href="<?php echo base_url() . 'course'; ?>" >More ...</a>
                    </div>
                </div>
                <script>
                    $.fn.raty.defaults.path = 'images';
                    $('.rating').raty();
                    $('.rating-read-only').raty({readOnly: true, score: 4});
                </script>

                <!--END TalhaTraining Upcoming Course  -->

                <!--TalhaTraining Video Course -->
                <div class="col-sm-12">
                    <h3>
                        You can register any of TalhaTraining Video course bellow:
                    </h3>
                    <hr><br>

                </div>

                <div class="col-sm-12 " style="padding: 0">
                    <?php foreach ($tt_video_course as $v_tt_video_course) { ?>
                        <div class="row gray" >
                            <div class="col-sm-5 text-left" ><a href="#" class="red"><?php echo $v_tt_video_course->course_name; ?></a><br>
                                By- <span class="green"><a href="#"><?php echo $v_tt_video_course->person_first_name; ?></a></span>
                            </div>
                            <div class="col-sm-2" style="padding: 0"><div class="rating-read-only" ></div></div>
                            <div class="col-sm-3"><span class="red">
                                    <?php
                                    if ($v_tt_upcomming_course->course_level == 0) {
                                        echo 'Basic';
                                    } else if ($v_tt_upcomming_course->course_level == 1) {
                                        echo 'Basic';
                                    } else if ($v_tt_upcomming_course->course_level == 2) {
                                        echo 'intermediate';
                                    } else if ($v_tt_upcomming_course->course_level == 3) {
                                        echo 'advanced';
                                    }
                                    ?>
                                </span>
                                <?php echo '&nbsp; BDT ' . $v_tt_upcomming_course->course_price; ?>
                            </div>
                            <div class="col-sm-2" style="padding-left:0;">
                                <a class="btn btn-success" data-toggle="tooltip" title="Register Now!" data-placement="top"><i class="fa fa-sign-in"></i></a> 
                                <a class="btn btn-danger" data-toggle="tooltip" title="Add to wishlist!" data-placement="top"><i class="fa fa-heart"></i></a>
                            </div>
                        </div>
                        <div><br></div>
                    <?php } ?>


                    <div><br></div>
                    <div class="text-right"><a href="<?php echo base_url() . 'course'; ?>" >More ...</a></div>
                    <div><br><br></div>
                </div> 
                <!--END TalhaTraining upcoming Video course -->


                <!--END TalhaTraining Course -->

                <!-- Start Pagination -->

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


            </div>


        </div><!-- /.row gray -->


    </div>
</div>

<script>

    function reloadPage() {
//        console.log("HELLO");
        setTimeout(function () {
            location.reload();
        }, 5000);
//        console.log("TalhaTraining");

    }

</script>
