<div class="col-sm-10">
    <div class="row">
        <div class="col-sm-12"></div>
    </div>
    <div class="row ">
        <div class="col-sm-12 " style="padding: 0">
            <div class="form-group col-sm-12">
                <div class="col-sm-3"><h3 class="margin-zero"></h3></div>
                <div class="col-sm-6">
                    <form action="#" method="post">
                        <div class="input-group">
                            <input type="text" class="form-control"  placeholder="Search here..." aria-describedby="basic-addon1"
                                   name="search_value" id="search_value" oninput="search(this.value);">
                            <span class="input-group-addon" id="basic-addon1"><i class="fa fa-search"></i></span>
                        </div>
                    </form>
                </div>
                <hr>
            </div>

            <div class="col-sm-12" id="topic_data">
                <div class="col-sm-12 gray_ttc text-center" style="border-bottom: 2px solid #8CC73F;padding-top: 10px;padding-bottom: 10px;font-weight: bold">
                    <div class="col-sm-4" style="padding:10px 0">Topic Name</div>
                    <div class="col-sm-3" style="padding:10px 0">By</div>
                    <div class="col-sm-5" style="padding:10px 0">Action</div>
                </div>
                <div class="col-sm-12"><br></div> 
                <?php
                if ($all_topic) {
                    foreach ($all_topic as $v_topic) {
                        $topic_id = $v_topic->topic_id;
                        $status = $v_topic->topic_status;
                        $url = $v_topic->url;
                        $topic_creator_role = $topic_creator[$v_topic->topic_id]->person_role;
                        $topic_viewer_role = $this->session->userdata('person_role');
                        ?>
                        <div id="actions<?php echo $topic_id; ?>">
                            <div class="col-sm-12 gray text-center" >
                                <div class="col-sm-4" style="padding:10px 0">
                                    <?php echo $v_topic->topic_name; ?>
                                </div>
                                <div class="col-sm-3" style="padding:10px 0">
                                    <?php
                                    if ($topic_creator_role == 6) {
                                        echo 'TalhaTraining';
                                    } else if ($topic_creator_role == 1) {
                                        echo 'TalhaTraining';
                                    } else if($topic_creator_role == 8){?>
                                     <a href="<?php echo base_url() . $topic_creator[$topic_id]->url . '/profile'; ?>" target="_blank">
                                            <?php echo $topic_creator[$topic_id]->person_first_name . ' ' . $topic_creator[$topic_id]->person_last_name;
                                            ?>
                                    </a>   
                                    <?php }else if ($topic_creator_role == 3) {
                                        ?>
                                        <a href="<?php echo base_url() . $topic_creator[$topic_id]->url . '/profile'; ?>" target="_blank">
                                            <?php echo $topic_creator[$topic_id]->person_first_name . ' ' . $topic_creator[$topic_id]->person_last_name;
                                            ?>
                                        </a>
                                        <?php
                                    } else if ($topic_creator_role == 4) {
                                        ?>
                                        <a href="<?php echo base_url() . $topic_creator[$topic_id]->url . '/profile'; ?>" target="_blank">
                                            <?php //echo $topic_creator[$topic_id]->organization_name; ?>
                                            <?php echo $topic_creator[$topic_id]->person_first_name . ' ' . $topic_creator[$topic_id]->person_last_name; ?>
                                        </a>
                                        <?php
                                    }
                                    ?>
                                </div>

                                <div class="col-sm-4" style="padding:10px 0">

                                    <a href="<?php echo base_url() . 'super-admin/edit-topic/' . $url; ?>">
                                        <i class="fa fa-edit" title="Edit topic"></i>
                                    </a>&nbsp;&nbsp;

                                    <?php
                                    switch ($topic_viewer_role) {
                                        case 1:
                                            ?>
                                            <?php
                                            switch ($status) {
                                                case 0:
                                                    ?>
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/1'; ?>', 'Publish', '<?php echo $topic_id; ?>')">
                                                        <i class="fa  fa-user-plus" title="Publish"></i>
                                                    </a>&nbsp;&nbsp;

                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/3'; ?>', 'Pending', '<?php echo $topic_id; ?>')">
                                                        <i class="fa fa-refresh fa-spin" title="Pending topic"></i>
                                                    </a>&nbsp;&nbsp;

                                                    <?php
                                                    break;
                                                case 1:
                                                    if (!$topic_quizes[$topic_id]) {
                                                        ?>
                                                        <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/0'; ?>', 'Unpublish', '<?php echo $topic_id; ?>')">
                                                            <i class="fa  fa-user-times" title="Unpublish"></i>
                                                        </a>&nbsp;&nbsp;

                                                        <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/3'; ?>', 'Pending', '<?php echo $topic_id; ?>')">
                                                            <i class="fa fa-refresh fa-spin" title="Pending topic"></i>
                                                        </a>&nbsp;&nbsp;
                                                    <?php } ?>
                                                    <?php
                                                    break;
                                                case 2:
                                                    ?>
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/1'; ?>', 'Publish', '<?php echo $topic_id; ?>')">
                                                        <i class="fa  fa-user-plus" title="Publish"></i>
                                                    </a>&nbsp;&nbsp;
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/0'; ?>', 'Unpublish', '<?php echo $topic_id; ?>')">
                                                        <i class="fa  fa-user-times" title="Unpublish"></i>
                                                    </a>&nbsp;&nbsp;
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/4'; ?>', 'Approve', '<?php echo $topic_id; ?>')">
                                                        <i class="glyphicon glyphicon-ok" title="Approve topic"></i>
                                                    </a>&nbsp;&nbsp;
                                                    <?php
                                                    break;
                                                case 3:
                                                    ?>
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/4'; ?>', 'Approve', '<?php echo $topic_id; ?>')">
                                                        <i class="glyphicon glyphicon-ok" title="Approve topic"></i>
                                                    </a>&nbsp;&nbsp;
                                                    <?php
                                                    break;
                                                case 4:
                                                    ?>
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/1'; ?>', 'Publish', '<?php echo $topic_id; ?>')">
                                                        <i class="fa  fa-user-plus" title="Publish"></i>
                                                    </a>&nbsp;&nbsp;
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/3'; ?>', 'Pending', '<?php echo $topic_id; ?>')">
                                                        <i class="fa fa-refresh fa-spin" title="Pending topic"></i>
                                                    </a>&nbsp;&nbsp;
                                                    <?php
                                                    break;
                                                default:
                                                    break;
                                            }
                                            ?>
                                            <?php if (!$topic_quizes[$topic_id]) { ?>
                                                <a  href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/delete-topic/' . $url; ?>', 'Delete', '<?php echo $topic_id; ?>')">
                                                    <i class="fa fa-trash-o" title="Delete"></i>
                                                </a>&nbsp;&nbsp;
                                            <?php } ?>
                                            <?php
                                            break;
                                        case 6:
                                            ?>
                                            <?php
                                            switch ($status) {
                                                case 0:
                                                    ?>
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/1'; ?>', 'Publish', '<?php echo $topic_id; ?>')">
                                                        <i class="fa  fa-user-plus" title="Publish"></i>
                                                    </a>&nbsp;&nbsp;

                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/3'; ?>', 'Pending', '<?php echo $topic_id; ?>')">
                                                        <i class="fa fa-refresh fa-spin" title="Pending topic"></i>
                                                    </a>&nbsp;&nbsp;

                                                    <?php
                                                    break;
                                                case 1:
                                                    ?>
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/0'; ?>', 'Unpublish', '<?php echo $topic_id; ?>')">
                                                        <i class="fa  fa-user-times" title="Unpublish"></i>
                                                    </a>&nbsp;&nbsp;

                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/3'; ?>', 'Pending', '<?php echo $topic_id; ?>')">
                                                        <i class="fa fa-refresh fa-spin" title="Pending topic"></i>
                                                    </a>&nbsp;&nbsp;
                                                    <?php
                                                    break;
                                                case 2:
                                                    ?>
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/1'; ?>', 'Publish', '<?php echo $topic_id; ?>')">
                                                        <i class="fa  fa-user-plus" title="Publish"></i>
                                                    </a>&nbsp;&nbsp;
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/0'; ?>', 'Unpublish', '<?php echo $topic_id; ?>')">
                                                        <i class="fa  fa-user-times" title="Unpublish"></i>
                                                    </a>&nbsp;&nbsp;
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/4'; ?>', 'Approve', '<?php echo $topic_id; ?>')">
                                                        <i class="glyphicon glyphicon-ok" title="Approve topic"></i>
                                                    </a>&nbsp;&nbsp;
                                                    <?php
                                                    break;
                                                case 3:
                                                    ?>
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/4'; ?>', 'Approve', '<?php echo $topic_id; ?>')">
                                                        <i class="glyphicon glyphicon-ok" title="Approve topic"></i>
                                                    </a>&nbsp;&nbsp;
                                                    <?php
                                                    break;
                                                case 4:
                                                    ?>
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/1'; ?>', 'Publish', '<?php echo $topic_id; ?>')">
                                                        <i class="fa  fa-user-plus" title="Publish"></i>
                                                    </a>&nbsp;&nbsp;
                                                    <a href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/change-status/' . $url . '/3'; ?>', 'Pending', '<?php echo $topic_id; ?>')">
                                                        <i class="fa fa-refresh fa-spin" title="Pending topic"></i>
                                                    </a>&nbsp;&nbsp;
                                                    <?php
                                                    break;
                                                default:
                                                    break;
                                            }
                                            ?>

                                            <a  href="javascript:void(0)" onclick="return checkForAction('<?php echo base_url() . 'super-admin/delete-topic/' . $url; ?>', 'Delete', '<?php echo $topic_id; ?>')">
                                                <i class="fa fa-trash-o" title="Delete"></i>
                                            </a>&nbsp;&nbsp;
                                            <?php
                                            break;
                                    }
                                    ?>
                                    <a  href="<?php echo base_url() . 'super-admin/view-topic-quiz/' . $url; ?>" >
                                        <i class="fa fa-th" title="View quiz"></i>
                                    </a>&nbsp;&nbsp;
                                </div>
                            </div>
                            <div class="col-sm-12"><br></div> 
                        </div>
                        <?php
                    }
                } else {
                    echo 'You do not have any topic.';
                }
                ?>

                <!--------------------start Pagination---------------------------->
                <div class="col-sm-12">
                    <?php $total_page = ceil($total_data / $per_page_view); ?>
                    <div class="col-sm-12 form-group">
                        <?php
                        if ($total_page > 1) {
                            ?>
                            <nav class="course-pagination">
                                <ul class="pagination">
                                    <?php
                                    if ($page > 2) {
                                        ?>
                                        <li>
                                            <a href='javascript:;' onclick="pagination('<?php echo base_url() . 'super-admin/manage-topic/1'; ?>', 'topic')"
                                               aria-label="Previous">
                                                <span aria-hidden="true" title="First Page">&laquo;&laquo;</span>
                                            </a>
                                        </li>
                                        <li>
                                            <a href='javascript:;' onclick="pagination('<?php echo base_url() . 'super-admin/manage-topic/' . ($page - 1); ?>', 'topic')"
                                               aria-label="Previous">
                                                <span aria-hidden="true" title="Previous Page">&laquo;</span>
                                            </a>
                                        </li>
                                    <?php } elseif ($page > 1) { ?>
                                        <li>
                                            <a href='javascript:;' onclick="pagination('<?php echo base_url() . 'super-admin/manage-topic/' . ($page - 1); ?>', 'topic')"
                                               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;"';
                                                }
                                                ?>
                                                    href='javascript:;' onclick="pagination('<?php echo base_url() . 'super-admin/manage-topic/' . $i; ?>', 'topic')">
                                                        <?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;"';
                                                }
                                                ?>
                                                    href='javascript:;' onclick="pagination('<?php echo base_url() . 'super-admin/manage-topic/' . $i; ?>', 'topic')">
                                                        <?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;"';
                                                }
                                                ?>
                                                    href="javascript:;" onclick="pagination('<?php echo base_url() . 'super-admin/manage-topic/' . ($i + 1); ?>', 'topic')"> 
                                                        <?php echo ($i + 1) ?>
                                                </a>
                                            </li>
                                            <?php
                                        }
                                    }

                                    if ($total_page > $page + 10) {
                                        ?>
                                        <li>
                                            <a href="javascript:;" onclick="pagination('<?php echo base_url() . 'super-admin/manage-topic/' . ($page + 1); ?>', 'topic')"
                                               aria-label="Next">
                                                <span aria-hidden="true" title="Next Page">&raquo;</span>
                                            </a>
                                        </li>
                                        <li>
                                            <a href="javascript:;" onclick="pagination('<?php echo base_url() . 'super-admin/manage-topic/' . ($page + 1); ?>', 'topic')" 
                                               aria-label="Next">
                                                <span aria-hidden="true" title="Last Page">&raquo;&raquo;</span>
                                            </a>
                                        </li>
                                    <?php } ?>
                                </ul>
                            </nav>
                        <?php } ?>
                    </div>
                </div>
                <!--------------------END Pagination---------------------------->
            </div>
        </div><!-- /.row gray -->
    </div>
</div>

<script>

    function checkForAction(url, action, quizID) { // check for Active Inactives
        swal({
            title: "",
            text: "Are you sure you want to " + action + " this topic?",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: '#DD6B55',
            confirmButtonText: 'Yes',
            cancelButtonText: "No",
            closeOnConfirm: true,
            closeOnCancel: true
        },
        function (isConfirm) {
            if (isConfirm) {
                var xmlhttp = create_xmlhttp();
                serverPage = url;
                xmlhttp.open("GET", serverPage);
                xmlhttp.onreadystatechange = function ()
                {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                    {
//                        if (action == 'Delete') {
//                            location.reload();
//                        } else {
                        _("actions" + quizID).innerHTML = xmlhttp.responseText;
//                        }
                    }
                };
                xmlhttp.send(null);
            } else {
                return;
            }
        });
    }

    function search(search_value) {
        var s_xhr = create_xmlhttp();
        var formdata = new FormData();
        formdata.append("search_value", search_value);
        s_xhr.addEventListener("load", function (event) {
            _('topic_data').innerHTML = s_xhr.responseText;
        }, false);
        s_xhr.open("POST", base_url + 'super-admin/search-quiz-topic');
        s_xhr.send(formdata);
    }

    function pagination(url, objID) {
        var search_value = _('search_value').value;
        var p_xhr = create_xmlhttp();
        var formdata = new FormData();
        formdata.append("search_value", search_value);
        p_xhr.addEventListener("load", function (event) {
            _('topic_data').innerHTML = p_xhr.responseText;
        }, false);
        p_xhr.open("POST", url);
        p_xhr.send(formdata);
    }

</script>